0

问题:我正在尝试根据用户操作使用新的绘画对象更新画布。画布剂量得到更新。

我所做的:用户与 DnD 动作交互,可传输对象到达画布,调用我创建的更新图形方法。该方法仅使用已经创建的图形 2d 对象并使用它绘制图像。我检查了 DnD 操作,该对象在画布类中正确接收,我能够使用 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();
    }

}
4

2 回答 2

9
Graphics2D drawImage;  //source of the problem!

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

  1. 将新对象添加到列表
  2. 打电话repaint()
  3. paintComponent(Graphics)绘制对象列表。

另一种方法是使用 aBufferedImage作为绘图对象。有关 示例,请参见此答案。


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

按下按钮后的 MainPaint

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

笔记

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

于 2012-12-26T11:17:27.273 回答
0

我认为您需要调用该validate()方法。

于 2012-12-26T11:10:46.397 回答