0

如何将多个矩形绘制到 JPanel?

这里的代码只允许我一次绘制一个矩形。一旦我开始一个新的矩形,它就会删除以前制作的矩形。我需要编写一个绘制超过 1 个矩形的程序,所以它可能看起来像不同颜色的矩形拼贴画。

public class Rectangles extends JFrame implements ActionListener {

int x1, y1, x2, y2;

JPanel main;
JPanel right;

JButton color1;
JButton color2;
JButton color3;
JButton color4;
JButton color5;
JButton color6;
JButton color7;
JButton color8;
JButton color9;
JButton color10;

Canvas left;

public Rectangles() {
    main = new JPanel(new BorderLayout(5, 5));
    main.setBorder(new EmptyBorder(20, 20, 20, 20));
    main.setBackground(new Color(0, 168, 165));

    right = new JPanel(new GridLayout(5, 2, 3, 3));
    right.setPreferredSize(new Dimension(150, 250));
    right.setBackground(new Color(0, 168, 165));

    color1 = new JButton();
    color1.setBackground(Color.LIGHT_GRAY);
    color1.addActionListener(this);
    color1.setBorder(new LineBorder(Color.BLACK, 5, false));

    color2 = new JButton();
    color2.setBackground(Color.BLUE);
    color2.addActionListener(this);
    color2.setBorder(new LineBorder(Color.BLACK, 5, false));

    color3 = new JButton();
    color3.setBackground(Color.CYAN);
    color3.addActionListener(this);
    color3.setBorder(new LineBorder(Color.BLACK, 5, false));

    color4 = new JButton();
    color4.setBackground(Color.DARK_GRAY);
    color4.addActionListener(this);
    color4.setBorder(new LineBorder(Color.BLACK, 5, false));

    color5 = new JButton();
    color5.setBackground(Color.GRAY);
    color5.addActionListener(this);
    color5.setBorder(new LineBorder(Color.BLACK, 5, false));

    color6 = new JButton();
    color6.setBackground(Color.GREEN);
    color6.addActionListener(this);
    color6.setBorder(new LineBorder(Color.BLACK, 5, false));

    color7 = new JButton();
    color7.setBackground(Color.YELLOW);
    color7.addActionListener(this);
    color7.setBorder(new LineBorder(Color.BLACK, 5, false));

    color8 = new JButton();
    color8.setBackground(Color.MAGENTA);
    color8.addActionListener(this);
    color8.setBorder(new LineBorder(Color.BLACK, 5, false));

    color9 = new JButton();
    color9.setBackground(Color.PINK);
    color9.addActionListener(this);
    color9.setBorder(new LineBorder(Color.BLACK, 5, false));

    color10 = new JButton();
    color10.setBackground(Color.RED);
    color10.addActionListener(this);
    color10.setBorder(new LineBorder(Color.BLACK, 5, false));

    right.add(color1);
    right.add(color2);
    right.add(color3);
    right.add(color4);
    right.add(color5);
    right.add(color6);
    right.add(color7);
    right.add(color8);
    right.add(color9);
    right.add(color10);

    left = new Canvas();
    left.setPreferredSize(new Dimension(500, 250));
    left.setBackground(Color.WHITE);
    left.setColor(Color.WHITE);
    left.setBorder(new LineBorder(Color.BLACK, 5, false));

    main.add(left, BorderLayout.CENTER);
    main.add(right, BorderLayout.EAST);

    this.add(main);
    this.pack();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLocation(450,75);
    this.setTitle("MARQUEZ Rectangles");
    this.setResizable(false);
    this.setVisible(true);
}

public static void main(String[] args) {
    Rectangles r = new Rectangles();
}

public void actionPerformed(ActionEvent e) {        
    if(e.getSource() == color1) {
        left.setColor(Color.LIGHT_GRAY);
    }

    else if(e.getSource() == color2) {
        left.setColor(Color.BLUE);
    }

    else if(e.getSource() == color3) {
        left.setColor(Color.CYAN);
    }

    else if(e.getSource() == color4) {
        left.setColor(Color.BLACK);
    }

    else if(e.getSource() == color5) {
        left.setColor(Color.GRAY);
    }

    else if(e.getSource() == color6) {
        left.setColor(Color.GREEN);
    }

    else if(e.getSource() == color7) {
        left.setColor(Color.YELLOW);
    }

    else if(e.getSource() == color8) {
        left.setColor(Color.MAGENTA);
    }

    else if(e.getSource() == color9) {
        left.setColor(Color.PINK);
    }

    else {
        left.setColor(Color.RED);
    }
}
}

此类绘制矩形。

class Canvas extends JPanel implements  MouseListener,MouseMotionListener {
private int x,y,x1,y1;
Color color;

public Canvas() {
    this.addMouseListener(this);
    this.addMouseMotionListener(this);
}

public void setColor(Color color) {
    this.color = color;
} 

public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
    x = e.getX();
    y = e.getY();
}

public void mouseDragged(MouseEvent e) {
    x1 = e.getX();
    y1 = e.getY();
    revalidate();
    repaint();
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(color);
    g.fillRect(x, y, Math.abs(x-x1), Math.abs(y-y1));
}
}
4

2 回答 2

2

我们所做的绘制操作是“瞬态的”,并且将在下次绘制组件时被销毁。为了解决这个问题,有两种常见的技术。

  1. 存储形状列表(图像、渐变和其他绘画操作),将当前绘图添加到列表中,并在请求时将它们全部绘制。
  2. 绘制到BufferedImage在请求/需要时绘制的。有关示例, 请参见此答案。

于 2013-09-16T15:34:18.927 回答
1

你的画布只绘制最近的矩形,摆动绘画就像这样,一旦请求绘画,前一个缓冲区就会被清除。您需要的是List<Rectangle>,每次用鼠标选择一个矩形时,将其添加到列表中,然后在画布中绘制列表中的每个矩形。您还需要保存先前矩形的颜色,方法是为将颜色作为字段的矩形创建一个包装类,或者也将其保存在列表中。

于 2013-09-16T15:31:05.160 回答