[英]How to update java canvas dynamically?
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. 我做了什么:用户与DnD动作进行交互,可传递对象到达画布,调用由我创建的更新图形方法。 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.
并且该方法仅使用aldready创建的图形2d对象并使用它绘制图像。我已经检查了DnD动作,该对象在canvas类中已正确接收,我能够使用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();
}
}
Graphics2D drawImage; //source of the problem!
Don't attempt to cache a Graphics
(or Graphics2D
) instance! 不要尝试缓存
Graphics
(或Graphics2D
)实例! Instead: 代替:
repaint()
. repaint()
。 paintComponent(Graphics)
draw the list of objects. paintComponent(Graphics)
绘制对象列表。 An alternative to that is to use a BufferedImage
as the drawing object. 替代方法是使用
BufferedImage
作为绘图对象。 See this answer for an example. 有关示例,请参见此答案 。
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();
}
}
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.
我决定停在最早用来在单击按钮时显示矩形的变体。
我认为您需要调用validate()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.