需要在同一框架/面板中的预先存在的图纸上添加图纸

时间:2013-01-13 15:15:25

标签: java swing drawing java-2d paintcomponent

我基本上有一个矩形矩阵,并且想要分别绘制它们,但每个绘制的都会删除任何前一个矩阵,最后我会得到一个最后一个孤独的矩形。我一直在谷歌搜索几个小时和搜索,我发现的唯一的建议是立即绘制所有,我试过,但似乎完全毁了我的听众,每个听众都有独立的组件。

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Color;
import javax.swing.JComponent;


@SuppressWarnings("serial")
public class GraphicEdge extends JComponent
{

    public Rectangle box;
    private Edge edge;
    /**
     * Creates a graphical box corresponding to the given edge at the given 
     * position
     * @param x x coordinate 
     * @param y y coordinate
     * @param e edge represented
     */
    public GraphicEdge(int x, int y, int width, int length, Edge e)
    {
        this.edge = e;
        this.box = new Rectangle(x, y, width, length);
    }
    /**
     * Paints said edge. Will be recalled whenever the edge switches from 
     * active to inactive.
     * @param g graphics.
     */

    public void paintComponent(Graphics g)
    {

        Graphics2D g2 = (Graphics2D)g;

        if (this.edge.getActive()==0)
        {
            g2.setColor(Color.red);
        }
        else
        {
            g2.setColor(Color.green);
        }
        g2.fill(this.box);
        g2.draw(this.box);

    }
    /**
     * Calls for the redrawing of the component.
     */
    public void redrawComponent()
    {
        repaint();
    }

    /**
     * Gets edge.
     */
    public Edge getEdge()
    {
        return this.edge;
    }
    /**
     * Returns the edge's rectangle.
     * @return
     */
    public Rectangle getBox()
    {
        return this.box;
    }
}

1 个答案:

答案 0 :(得分:2)

如果我理解正确,你基本上想要在paintComponent持续制作你的图画吗?

默认情况下,paintComponent中完成的所有绘图都不会持续存在。

因此,repaint() paintComponent将再次被调用,并绘制方法告诉它的任何内容。

所以要解决你的问题:

1)在您的课程中创建一个List,其中JPanel(不是JComponent除非有原因)。

2)制作public方法以允许添加到列表中(如果需要,可以删除)。

3)在paintComponent中迭代List并绘制每个对象。

以下是一个示例(在容器上的任意位置单击以绘制Rectangle):

enter image description here

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class Test {

    private final JFrame frame = new JFrame();
    private final MyPanel panel = new MyPanel();

    private void createAndShowUI() {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test().createAndShowUI();
            }
        });
    }
}

class MyPanel extends JPanel {

    private List<Rectangle> recs = new ArrayList<>();

    public MyPanel() {
        initComponents();
    }

    private void initComponents() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                super.mouseClicked(me);
                addRec(new Rectangle(me.getPoint().x, me.getPoint().y, 100, 50));
                repaint();
            }
        });
    }

    public void addRec(Rectangle rec) {
        recs.add(rec);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;

        for (Rectangle rec : recs) {
            g2d.drawRect(rec.x, rec.y, rec.width, rec.height);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(400, 400);
    }
}

注意,正如@HFOE所说的那样,请记得在覆盖super.paintComponent(g)的第一个电话中调用paintComponent来尊重画颜链,否则可能会出现视觉异常。