gpt4 book ai didi

java - Swing 中的增量图形

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:48:15 26 4
gpt4 key购买 nike

我想掌握做图形的窍门(画线,等)在 Swing 中。到目前为止,我见过的所有教程都声明了一个类覆盖 paintComponent 和所有 paintComponent 方法做一些固定的、具体的事情,比如画一个红色方 block (尽管也许他们每次都在不同的位置绘制它)。或者,他们可能绘制了许多线条和形状,但 paintComponent 方法一次完成所有操作。

我想弄清楚:假设我想在组件,然后在它上面绘制其他东西而不擦掉我之前画的东西。我的第一个想法是让我的paintComponent 覆盖调用回调。

import java.awt.*;
import javax.swing.*;
public class DrawTest {

private interface GraphicsAction {
public void action (Graphics g);
}

private static class TestPanel extends JPanel {

private GraphicsAction paintAction;

public void draw (GraphicsAction action) {
paintAction = action;
repaint();
}

@Override
public void paintComponent (Graphics g) {
super.paintComponent (g);
if (paintAction != null)
paintAction.action(g);
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame ("DrawTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500,500));

TestPanel p = new TestPanel ();
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
p.repaint();

p.draw (new GraphicsAction () {
public void action (Graphics g) {
g.setColor(Color.RED);
g.drawLine(5, 30, 100, 50);
}
});

// in real life, we would do some other stuff and then
// later something would want to add a blue line to the
// diagram

p.draw (new GraphicsAction () {
public void action (Graphics g) {
g.setColor(Color.BLUE);
g.drawLine(5, 30, 150, 40);
}
});

}

public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGui();
}
});
}
}

这行不通。出现一条蓝线,但没有红线。我是猜测这是因为 draw 中的 repaint() 导致了一切当我画蓝线时重新开始,但我不确定;无论如何,我不知道还有什么办法paintComponent 被调用。

此外,如果我在两个 p.draw 调用之间放置一个 Thread.sleep(1000),我什至一秒钟都看不到红线。所以我一点都不清楚关于如何让我的图形在我希望的时候显示。

我在 Swing 中搜索了“增量图形”,但是没有什么可以帮助找到解决方案。我找到了一篇 Oracle 文章讨论覆盖 update() 的“AWT 和 Swing 中的绘画”完成增量图形的方法,但我还没有找到任何这样做的实际例子。

那么我怎样才能让它做我想做的事呢?好像很常见足够多的任务应该有一个简单的方法来完成,但我还没有找到一个。我假设它应该在不打电话的情况下可行getGraphics,根据其他 StackOverflow 响应,它是,充其量,有点笨拙。

最佳答案

在 Swing 中绘画是破坏性的。也就是说,无论何时运行新的绘制周期,您都应该根据正在绘制的对象的状态完全重建输出。

看看Painting in AWT and Swing

所以当你打电话的时候

p.draw (new GraphicsAction () {
public void action (Graphics g) {
g.setColor(Color.RED);
g.drawLine(5, 30, 100, 50);
}
});

紧随其后

p.draw (new GraphicsAction () {
public void action (Graphics g) {
g.setColor(Color.BLUE);
g.drawLine(5, 30, 150, 40);
}
});

您基本上是在丢弃第一个 Action 。忽略目前如何安排重绘。第一个请求说“画一条红线”,第二个请求说“画一条蓝线”,但在执行这些操作之前,Graphics上下文已清理,准备更新。

这非常重要,因为 Graphics您提供的上下文是共享资源。之前绘制的所有组件都使用相同的上下文,之后绘制的所有组件将使用相同的上下文。这意味着,如果您在绘制之前不“清理”上下文,您最终可能会得到不需要的绘制伪影。

但是你怎么能绕过它呢??

这里有几个选择。

你可以绘制到一个有自己的 BufferedImage 的后备缓冲区(或 Graphics)上下文,你可以添加到它并且只需要在你的 paintComponent 中“绘制”方法。

这意味着,每次您调用 p.draw(...) , 你实际上会先画到这个缓冲区然后调用 repaint .

问题是您需要保持缓冲区的大小。每次组件大小更改时,您都需要根据组件的新大小将此缓冲区复制到新缓冲区。这有点困惑,但可行。

另一种解决方案是将每个操作放在 List 中在需要时,只需循环遍历 List并在需要时重新应用该操作。

这可能是最简单的方法,但随着操作数量的增加,可能会降低绘制过程的效率,从而减慢绘制过程。

您也可以结合使用两者。缓冲区不存在时生成缓冲区,循环遍历List Action 并将它们渲染到缓冲区并简单地在 paintComponent 中绘制缓冲区方法。每当调整组件大小时,只需 null缓冲区并允许 paintComponent重新生成它...例如...

Also, if I put a Thread.sleep(1000) between the two p.draw calls

Swing 是一个单线程框架。这意味着所有更新和修改都应在事件调度线程的上下文中完成。

同样,任何阻止 EDT 运行的东西都会阻止它处理(除其他外)绘制请求。

这意味着当你 sleepp.draw 之间电话,您正在停止运行 EDT,这意味着它无法处理您的绘画请求...

看看Concurrency in Swing了解更多详情

更新了例子

enter image description here

我只想指出那真的很低效。每次都重新创建缓冲区 invalidate被调用将创建大量短暂存在的对象,并可能显着降低性能。

通常,我会使用 javax.swing.Timer , 设置为不重复,每次都会重新启动 invalidate被称为。这将被设置为较短的延迟(介于 125-250 毫秒之间)。当定时器被触发时,我会在此时简单地重新构造缓冲区,但这只是一个例子;)

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class DrawTest {

private interface GraphicsAction {

public void action(Graphics g);
}

private static class TestPanel extends JPanel {

private GraphicsAction paintAction;
private BufferedImage buffer;

@Override
public void invalidate() {
BufferedImage img = new BufferedImage(
Math.max(1, getWidth()),
Math.max(1, getHeight()), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
if (buffer != null) {
g2d.drawImage(buffer, 0, 0, this);
}
g2d.dispose();
buffer = img;
super.invalidate();
}

protected BufferedImage getBuffer() {
if (buffer == null) {
buffer = new BufferedImage(
Math.max(1, getWidth()),
Math.max(1, getHeight()), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = buffer.createGraphics();
g2d.setColor(getBackground());
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
return buffer;
}

public void draw(GraphicsAction action) {
BufferedImage buffer = getBuffer();
Graphics2D g2d = buffer.createGraphics();
action.action(g2d);
g2d.dispose();
repaint();
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(getBuffer(), 0, 0, this);
}
}

private static void createAndShowGui() {
JFrame frame = new JFrame("DrawTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 500));

TestPanel p = new TestPanel();
frame.getContentPane().add(p);
frame.pack();
frame.setVisible(true);
p.repaint();

p.draw(new GraphicsAction() {
public void action(Graphics g) {
g.setColor(Color.RED);
g.drawLine(5, 30, 100, 50);
}
});

// in real life, we would do some other stuff and then
// later something would want to add a blue line to the
// diagram
p.draw(new GraphicsAction() {
public void action(Graphics g) {
g.setColor(Color.BLUE);
g.drawLine(5, 30, 150, 40);
}
});

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGui();
}
});
}
}

关于java - Swing 中的增量图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18839821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com