创建钢笔工具,重画问题 - JAVA

发布于 2024-11-05 22:17:35 字数 719 浏览 4 评论 0原文

我正在尝试使用鼠标侦听器创建钢笔工具:

public void mouseDragged(MouseEvent e) {
            imageL.setCoordinates(originalPos, e.getPoint());
            imageL.repaint();
            originalPos = e.getPoint();
        }

JLabel (imageL) 中的绘制函数接收两组点,这些点允许根据鼠标拖动绘制一条线。唯一的问题是,每次执行拖动时,新图层都不包含上次鼠标拖动绘制的线条。 JLabel 的绘制函数如下:

public void paint(Graphics g) {
    super.paint(g); 
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(drawingColour);
    g2d.drawLine(originCors.x,originCors.y,endCors.x,endCors.y);
}

所以本质上我的问题是:如何将新行“添加”到当前图层?

任何帮助都会很棒, 提前致谢

I am trying to create a pen tool using mouse listeners:

public void mouseDragged(MouseEvent e) {
            imageL.setCoordinates(originalPos, e.getPoint());
            imageL.repaint();
            originalPos = e.getPoint();
        }

The paint function in the JLabel (imageL) receives two sets of points that allow drawing a line based upon the mouse drag. The only issue is that every time the drag is performed, the new layer does not contain the line drawn from the previous mouse drag. The paint function of the JLabel is as follows:

public void paint(Graphics g) {
    super.paint(g); 
    Graphics2D g2d = (Graphics2D)g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setColor(drawingColour);
    g2d.drawLine(originCors.x,originCors.y,endCors.x,endCors.y);
}

So essentially my question is: How do I "add" the new line to the current layer?

any help would be great,
Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等往事风中吹 2024-11-12 22:17:35

您想要在 JComponent(以及从 JComponent 扩展的 JLabel)paintComponent 方法中进行绘制,而不是在 Paint 方法中进行绘制,除非您计划处理组件边框和子组件的所有绘制。接下来,您可能希望让绘图组件保存 Point 的 ArrayList,并在 mouseDragged 方法中将 Point 添加到此数组列表中。然后,paintComponent 方法可以迭代列表并绘制所有线条。如果您只想绘制一条线,则让绘画 JLabel 将两个点保存在类字段中。

You want to draw in a JComponent's (and JLabel extends from JComponent) paintComponent method, not the paint method unless you plan to handle all the painting of the component's borders and children. Next, you may wish to have your drawing component hold an ArrayList of Point and add Points to this array list in the mouseDragged method. Then the paintComponent method can iterate through the list and paint all the lines. If all you want to do is paint one line, then have the painting JLabel hold both points in a class field.

横笛休吹塞上声 2024-11-12 22:17:35

Michael,

首先,在 swing 中(JLabel 是一个 swing 组件,与旧的 AWT 库不同),建议的做法是重写 paintComponent 方法(而不是“paint”方法)。请参阅 Java 教程:自定义绘画 了解操作方法和方法。这是为什么。

如果您想自定义绘制一系列线条,那么您将必须这样做......而不仅仅是“新”线条。解决这个问题的一种方法是用每条新线“更新”图像,然后自定义绘制......这稍微快一些,因为您只“绘制”每行一次,但代价是使用更多内存(以维护“背景图像”)...但是这种技术适合“双缓冲”,这可以避免直接绘制到屏幕时出现的“闪烁”。有关详细信息,请参阅Java 教程:使用图像。实际上非常简单。

我建议你用谷歌搜索“java 双缓冲示例”(避免使用 Rose India,它充满了废话)。

干杯。基思.

Michael,

For a start, in swing (a JLabel is a swing component, as apposed to the older AWT library) the recommended practice is to override the paintComponent method (not the "paint" method). See The Java Tutorials: Custom Painting for the hows & whys of it.

If you want to custom-paint a-list-of-lines then you're going to have to do just that... not just the "new" one. One way around this is to "update" an image with each new line, and then custom-paint that... this is slightly quicker as you only "paint" the each line once, at the cost of using more memory (to maintain the "backgroun image")... but this technique lends itself to "double buffering", which avoids "that flickering" you get when you draw directly to the screen. See The Java Tutorials: Working with Images for the details. It's actually pretty straight forward.

I suggest you google for "java double buffering example" (avoid Rose India, it's full of crap).

Cheers. Keith.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文