- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想掌握做图形的窍门(画线,等)在 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 中绘画是破坏性的。也就是说,无论何时运行新的绘制周期,您都应该根据正在绘制的对象的状态完全重建输出。
所以当你打电话的时候
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 运行的东西都会阻止它处理(除其他外)绘制请求。
这意味着当你 sleep
在 p.draw
之间电话,您正在停止运行 EDT,这意味着它无法处理您的绘画请求...
看看Concurrency in Swing了解更多详情
更新了例子
我只想指出那真的很低效。每次都重新创建缓冲区 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/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!