gpt4 book ai didi

java - 如何动态更新java Canvas ?

转载 作者:行者123 更新时间:2023-12-01 08:15:09 25 4
gpt4 key购买 nike

问题:我正在尝试根据用户操作使用新的绘画对象更新 Canvas 。 Canvas 没有更新。

我所做的:用户与 DnD 操作交互,可传输对象到达 Canvas ,调用我创建的更新图形方法。该方法只是使用已经创建的图形 2d 对象并使用它绘制图像。我已经检查了 DnD 操作,该对象在 Canvas 类中正确接收,并且我能够使用 System.out.println 打印它们。

示例代码,与我的功能类似,

绘画类:

    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();
}

}

大型机类:

       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!

不要尝试缓存Graphics(或Graphics2D)实例!相反:

  1. 将新对象添加到列表
  2. 调用repaint()
  3. paintComponent(Graphics) 中绘制对象列表。

另一种方法是使用 BufferedImage 作为绘图对象。请参阅this answer举个例子。

<小时/>

更新 - 基于最新代码的 SSCCE。

MainPaint after button press

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();
}
}

注意

该代码仍有许多地方需要更改。我决定停止在最早的变体上,该变体可以在单击按钮时显示矩形。

关于java - 如何动态更新java Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039682/

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