JPanel custom drawing using Graphics(JPanel 使用 Graphics 自定义绘图)
问题描述
我有一个自定义的 JPanel,有时在我的程序中,我需要调用一个将屏幕涂黑的方法,就是这样.
I have a custom JPanel and sometimes throughout my program, I need to call a method which paints the screen black, that's it.
public void clearScreen() {
Graphics g = getGraphics();
g.setColor(Color.black);
g.fillRect(0,0,getWidth(),getHeight());
}
当我启动程序时,我调用了这个方法.
When I launch the program, I call this method.
但是,我发现有时它有效,有时则无效.这很奇怪.我还发现当它不起作用时,图形对象不为空,并且宽度和高度也被正确定义(来自getWidth()和getHeight()).
However, I find that sometimes it works, and sometimes it doesn't. It's very odd. I also found out that when it doesn't work, the graphics object is NOT null, and the width and height are also correctly defined (from getWidth() and getHeight()).
为什么有时会起作用,有时却不起作用?
Why would this sometimes work and sometimes not work?
在程序中的某个时间点在我的 JPanel 上进行自定义绘图的正确方法是什么?像我一样使用 getGraphics() 是否正确?我的 JPanel(在某些时候)有 JComponents,但后来我删除了这些 JComponents 并进行了一些自定义图形绘制.为什么这有时只会起作用?
What is the correct way to make a custom drawing on my JPanel at some point in the program? Is it correct to use getGraphics() as I am doing? My JPanel (at some point) has JComponents, but later on I remove those JComponents and do some custom graphics drawing. Why would this sometimes only work?
推荐答案
不要通过在 JPanel 等组件上调用 getGraphics 来获取您的 Graphics 对象,因为获得的 Graphics 对象不会在下一次重绘时持续存在(这很可能问题的根源).
Don't get your Graphics object by calling getGraphics on a component such as a JPanel since the Graphics object obtained will not persist on the next repaint (which is likely the source of your problems).
相反,请考虑在 BufferedImage 中进行所有绘图,然后然后您可以使用 getGraphics() 来满足您的需求.如果您这样做,请不要忘记在完成绘制后处理 Graphics 对象.
Instead, consider doing all of your drawing in a BufferedImage, and then you can use getGraphics() to your heart's content. If you do this, don't forget to dispose of the Graphics object when you're done painting with it.
例如,
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MyPaint extends JPanel {
public static final int IMG_WIDTH = 400;
public static final int IMG_HEIGHT = IMG_WIDTH;
private BufferedImage image = new BufferedImage(IMG_WIDTH, IMG_HEIGHT,
BufferedImage.TYPE_INT_ARGB);
public MyPaint() {
MyMouseAdapter myMouseAdapter = new MyMouseAdapter();
addMouseListener(myMouseAdapter);
addMouseMotionListener(myMouseAdapter);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, null);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(IMG_WIDTH, IMG_HEIGHT);
}
public void clearScreen() {
Graphics g = image.getGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.dispose();
repaint();
}
private class MyMouseAdapter extends MouseAdapter {
// code to draw on the buffered image.
// Don't forget to call repaint() on the "this" JPanel
}
}
这篇关于JPanel 使用 Graphics 自定义绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JPanel 使用 Graphics 自定义绘图
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 获取数字的最后一位 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01