- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是可运行的java代码。如果您想测试它,请复制下面的两个代码摘录。编译并运行Triangleframe.java
我正在 JPanel 上绘制两个三角形(可能稍后我会添加更多)。我点击三角形之一并将其拖动。在我决定遵循 Oracle 在本课中推荐的剪辑区域之前,这曾经(在某种程度上)有效: Custom painting
我从 repaint()
切换到 repaint(x,y,with,height)
的原因是,当我尝试拖动其中一个三角形时,它是重新绘制非常缓慢,而且它也不太擅长跟随鼠标指针(滞后?)。我推断,保持在界限内并仅重新绘制我正在使用的屏幕部分可以解决问题。它确实修复了延迟,但现在只要按下鼠标按钮,我正在重新绘制的边界框似乎就不会移动。三角形仅在边界框内移动。直到我释放鼠标按钮(此时创建了一个新三角形)。最好我应该只重画三角形而不是边界框,但为了方便起见,我尝试首先解决这个问题。希望三角形能够重叠。
请参阅代码中的注释以获取更深入的解释。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
//********************************************************************
//*** This is a stripped down version of the code which shows ***
//*** the problem. Just create an instance of the class by running ***
//*** TriangleFrame.java. Then try to drag either triangle with ***
//*** the mouse. ***
//********************************************************************
public class TrianglePanel extends JPanel implements MouseListener,
MouseMotionListener {
Triangle triangle1 = null;
Triangle triangle2 = null;
Rectangle boundingBox = null;
int lastXPos = 0;
int lastYPos = 0;
boolean draggable = false;
public TrianglePanel() {
triangle1 = new Triangle(new Point(100, 10), new Point(50, 100),
new Point(150, 100));
triangle2 = new Triangle(new Point(250, 10), new Point(150, 100),
new Point(350, 100));
lastXPos = this.getX();
lastYPos = this.getY();
addMouseListener(this);
addMouseMotionListener(this);
}
@Override
public void mouseReleased(MouseEvent e) {
triangle1.createNewTriangle();
triangle2.createNewTriangle();
}
@Override
public void mousePressed(MouseEvent e) {
draggable = false;
if (triangle1.getPos().contains(e.getPoint())
|| triangle2.getPos().contains(e.getPoint())) {
draggable = true;
lastXPos = e.getX();
lastYPos = e.getY();
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (draggable) {
isInside(triangle1, e);
isInside(triangle2, e);
}
}
private void isInside(Triangle t, MouseEvent e) {
if (t.getPos().contains(e.getPoint()))
updatePos(t, e);
}
//*****************************************************************
//*** Next I try to do the right thing by only repainting the ***
//*** portion of the panel that I use. ***
//*** Well, almost. To make it as simple as possible for now ***
//*** I use a boundingbox rectangle and repaint within those ***
//*** bounds. The problem seem to be that the rest of the panel ***
//*** does not want to repaint anything outside the bounding ***
//*** box, until I release the mousebutton(after click+dragging)***
//*** When the mousebutton is released, a new triangle is created**
//*** in the same spot. Se mousereleased method. Otherwise ***
//*** I would only be able to drag the triangle once ***
//*****************************************************************
private void updatePos(Triangle t, MouseEvent event) {
boundingBox = t.getPos().getBounds();
// stored as final variables to avoid repeat invocations to methods.
// is this a problem? Anybody care to explain?
final int CURR_X = boundingBox.x;
final int CURR_Y = boundingBox.y;
final int CURR_W = boundingBox.width;
final int CURR_H = boundingBox.height;
final int OFFSET = 1;
if ((CURR_X != event.getX()) || (CURR_Y != event.getY())) {
// paint over the bounding-box of the old triangle
repaint(CURR_X, CURR_Y, CURR_W + OFFSET, CURR_H + OFFSET);
// update x-coordinates
int xPos = event.getX();
int[] xPoints = t.getPos().xpoints; // get old x coordinates
for (int i = 0; i < xPoints.length; i++) {
xPoints[i] = xPoints[i] - (lastXPos - xPos); // create new x
// coordinates
}
lastXPos = xPos;
// update y-coordinates
int yPos = event.getY();
int[] yPoints = t.getPos().ypoints; // get old y coordinates
for (int i = 0; i < yPoints.length; i++) {
yPoints[i] = yPoints[i] - (lastYPos - yPos); // create new y
// coordinates
}
lastYPos = yPos;
// paint inside bounding box of the new triangle
repaint(boundingBox.x, boundingBox.y, boundingBox.width + OFFSET,
boundingBox.height + OFFSET);
// repaint the whole panel (not recommended).
// repaint(); //-> makes dragging the triangle sluggish.
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
triangle1.draw(g);
triangle2.draw(g);
}
// not used
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
/**
*
* Inner Triangle Class.
* A polygon object is used for convenience to
* create the Triangle. Otherwise I would
* have to deal with Points all through the program. Which means
* 3 coordinate pairs = 6 coordinates, which
* means more code.
*
*/
private class Triangle {
private Polygon polygon;
private Triangle(Point p1, Point p2, Point p3) {
polygon = new Polygon();
polygon.addPoint(p1.x, p1.y);
polygon.addPoint(p2.x, p2.y);
polygon.addPoint(p3.x, p3.y);
}
public Polygon getPos() {
return polygon;
}
public void createNewTriangle() {
polygon = new Polygon(polygon.xpoints, polygon.ypoints,
polygon.npoints);
}
public void draw(Graphics g) {
g.fillPolygon(polygon);
}
} // end inner class Triangle
} // end outer class TrianglePanel
为了您的方便,我提供了包含主方法的类(可从此处运行):
import java.awt.Dimension;
import javax.swing.JFrame;
public class TriangleFrame extends JFrame {
public TriangleFrame() {
this.setTitle("Draggable triangles. Click one and drag it with the mouse.");
TrianglePanel panel = new TrianglePanel();
panel.setPreferredSize(new Dimension(500, 500));
this.add(panel);
pack();
setVisible(true);
}
public static void main(String[] args) {
new TriangleFrame();
}
}
最佳答案
多边形缓存边界框。如果直接修改坐标,必须调用Polygon.invalidate() :
// paint inside bounding box of the new triangle
t.getPos().invalidate();
boundingBox = t.getPos().getBounds();
repaint(boundingBox);
但是,使用 Polygon.translate(int deltaX, int deltaY) 会更容易,它会为您完成所有工作。 (修改坐标并确保下次调用时边界框正确)。我还使用了repaint(Rectangle)
。
关于java - 自定义绘画代码无法正确更新,我尝试尊重剪辑区域。运行一下看看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437906/
你好,我试图在java中创建一个网格并绘制每个单元格,但我不知道我做错了什么。每个单元格都是一个 JPanel,我将每个单元格添加到 mazePanel。之后 mazePanel 添加到框架中。但只能
我正在开发一个应用程序(适用于 iOS 和 Android),其中我需要允许用户从图库中选择照片并通过在图像上绘图来圈出某些项目。一旦用户完成绘制,系统就会提示他在弹出窗口中为该项目命名。命名项目后,
当我使用绘画/绘图应用程序时,我从未注意到在按下手指和能够看到显示屏上绘制的内容之间存在延迟。但是,对于我的应用程序,接收到的初始触摸非常缓慢。第二个、第三个和第四个手势每秒接收 60 个,但第一个手
到目前为止,在我的尝试中,我能够在普通图像上画线,比如创建我自己的 CGRect 大小的普通上下文并在其上画线。我看到的所有教程都是如何在创建的 x*y 大小的图像上下文上绘制。但是我想在已经存在的图
我在 android 中开发,我必须为 android 做一个 Paint。 我正在使用下面的代码,当我执行代码时,绘制工作正常,但是,似乎有 2 个表面要绘制,当你绘制一个时,另一个消失了。 我一直
我使用QGraphicsView、QGraphicsScene 和QGraphicsItem 来绘制一些图表。我已经实现了用于绘制文本(图表的值)的 QGraphicsItem::paint 函数,但
Canvas { id: canvas onPaint: { if (personalInfo.count === 0) { return
好的,这就是问题所在:在 C# 表单中,我创建了一个新的私有(private) void: private void NewBtn(string Name, int x, int y) 它的目的是创建
任何人都可以详细建议我如何使用 QDirectPainter 类直接在帧缓冲区上绘制小部件。如果您能提供一个工作示例,我会更有帮助。 最佳答案 QDirectPainter 不会也不能绘制任何东西。它
我正在使用 cardLayout 更改“ View ”(此类有一个 JFrame 变量)。当用户点击新游戏按钮时,会发生这种情况: public class Views extends JFrame
目标: 我想在 pdf 位图上绘制/书写/绘画并将它们保存在一起,以便我可以通过电子邮件发送给他们。 详细信息: 我有多个 Pdf包含 5-20 的文件每个页面,现在我正在从 pdfs 中提取位图并将
我最近开始学习Qt。 我不太清楚如何使用 QPainter 类进行绘画。假设我只想在窗口中放置几个点: class PointDrawer: public QWidget { Q_OBJE
我必须为我的 CS 类(class)期末项目创建一个带有 GUI 的 connect 5 游戏。我在最后一个项目中使用 Graphics2D - 迷宫 - 使用 Graphics2D 是一场噩梦。 c
我已经删除了 CS_HREDRAW、CS_VREDRAW。 消息 WM_PAINT 和 WM_ERASEBKGND 什么都不做,但是一旦窗口重新调整大小,它就会用背景颜色重新绘制它。有什么办法可以完全
首先大家好! 我遇到了一个大问题:我需要构建一个程序,该程序包括构建一个包含 5 个方 block 和一个按钮的 java swing 界面。按钮功能是在方 block 内画一个圆。我有这段代码,但我
我正在尝试从显示幻灯片的 Qt fluidLauncher 演示中运行稍微修改过的代码。代码贴在下面。当处理 paintEvent 时,屏幕上会显示一个黑色矩形,因为导入的图像大小为 0。 单步执行Q
我有以下代码来激活/停用橡皮擦: public PorterDuffXfermode clear = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
我正在绘制一个未被清除的 Canvas ,并使 Canvas 随着时间的推移逐渐变成纯色,或者在 alpha 中逐渐消失以显示后面的图层。 我的第一直觉是简单地用每帧的低 alpha 在绘图上填充一个
我正在尝试在每幅画上检测并绘制一个矩形轮廓,例如这张图片: 我遵循了一些指南并执行了以下操作: 灰度转换 应用中值模糊 锐化图像 应用自适应阈值 应用形态梯度 寻找轮廓 绘制轮廓 得到如下结果: 我知
创建 JFrame、在其上添加 JPanel 并在 JPanel 上绘制矩形的类 class Frame { JFrame frame; myPanel panel; void draw() {
我是一名优秀的程序员,十分优秀!