java 如何动态更新java画布?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14039682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update java canvas dynamically?
提问by Balaram26
Problem: I am trying to update the canvas with new painting objects based on user action. The canvas dosent get updated.
问题:我正在尝试根据用户操作使用新的绘画对象更新画布。画布不会更新。
What i have done: The user interacts with the DnD action,The transferrable object reaches the canvas, Calls an update graphics method created by me. And the method simply uses the aldready created graphics 2d object and draws images using it.I have checkd the DnD action,the object is properly recived at canvas class and i was able to print them out using System.out.println.
我所做的:用户与 DnD 操作交互,可传输对象到达画布,调用我创建的更新图形方法。该方法仅使用已经创建的图形 2d 对象并使用它绘制图像。我检查了 DnD 操作,该对象在画布类中正确接收,我能够使用 System.out.println 将它们打印出来。
A sample code,that has a similar function to that of mine,
一个示例代码,与我的功能相似,
Paint class:
油漆类:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class PaintPanel extends JPanel{
private Graphics2D drawImage;
public PaintPanel()
{
}
@Override
public void paint(Graphics g) {
drawImage = (Graphics2D) g;
drawImage.setColor(Color.WHITE);
drawImage.fillRect(0, 0, getWidth(), getHeight());
}
public void updateGraphics(int length,int width)
{
drawImage.setColor(Color.black);
drawImage.drawRect(100, 150, length, width);
repaint();
}
}
mainframe class:
主机类:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainPaint extends JFrame{
public MainPaint()
{
setTitle("test paint");
setSize(400,400);
setLayout(new BorderLayout());
final PaintPanel paintPan = new PaintPanel();
JButton testButon = new JButton("Display shape");
add(paintPan,BorderLayout.CENTER);
add(testButon,BorderLayout.PAGE_END);
testButon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
paintPan.updateGraphics(50,50);
repaint();
}
});
setVisible(true);
}
public static void main(String[] args)
{
new MainPaint();
}
}
回答by Andrew Thompson
Graphics2D drawImage; //source of the problem!
Don't attempt to cache a Graphics
(or Graphics2D
) instance! Instead:
不要尝试缓存Graphics
(或Graphics2D
)实例!反而:
- Add the new objects to a list
- Call
repaint()
. - In
paintComponent(Graphics)
draw the list of objects.
- 将新对象添加到列表中
- 打电话
repaint()
。 - 在
paintComponent(Graphics)
绘制对象列表中。
An alternative to that is to use a BufferedImage
as the drawing object. See this answerfor an example.
另一种方法是使用 aBufferedImage
作为绘图对象。有关 示例,请参阅此答案。
Update - SSCCE based on latest code.
更新 - 基于最新代码的 SSCCE。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainPaint extends JFrame {
public MainPaint() {
setTitle("test paint");
setSize(400, 400);
setLayout(new BorderLayout());
final PaintPanel paintPan = new PaintPanel();
JButton testButon = new JButton("Display shape");
add(paintPan, BorderLayout.CENTER);
add(testButon, BorderLayout.PAGE_END);
testButon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
paintPan.updateGraphics(50, 50);
repaint();
}
});
setVisible(true);
}
public static void main(String[] args) {
new MainPaint();
}
}
class PaintPanel extends JPanel {
private int x, y;
private Color color = null;
public PaintPanel() {
setBackground(Color.ORANGE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D drawImage = (Graphics2D) g;
if (color != null) {
drawImage.setColor(color);
drawImage.drawRect(100, 150, x, y);
}
}
public void updateGraphics(int length, int width) {
color = Color.RED;
x = length;
y = width;
repaint();
}
}
Note
笔记
There are still a number of things about that code that need changing. I decided to stop at the earliest variant that worked to display the rectangle on button click.
该代码仍有许多需要更改的地方。我决定停在最早的变体上,该变体用于在按钮单击时显示矩形。
回答by UVM
I think you need to call the validate()
method.
我认为您需要调用该validate()
方法。