简体   繁体   English

将多个矩形绘制到一个JPanel

[英]Painting multiple rectangles to a JPanel

How can I paint multiple rectangles to a JPanel? 如何在一个JPanel上绘制多个矩形?

This code here only allows me paint one rectangle at a time. 此代码仅允许我一次绘制一个矩形。 It deletes the previously made rectangle once I start a new one. 一旦我开始创建新矩形,它将删除先前制作的矩形。 I need to write a program that paints more than 1 rectangle so it would probably look like a collage of rectangles of different colors. 我需要编写一个绘制多个矩形的程序,因此它看起来像是不同颜色矩形的拼贴。

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

This class paints the rectangle. 此类绘制矩形。

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

The paint operations we do are 'transient' and will be destroyed the next time the component is painted. 我们所做的绘制操作是“瞬态”的,下次对组件进行绘制时将被销毁。 To get around that, there are two common techniques. 为了解决这个问题,有两种常用技术。

  1. Store a list of shapes (images, gradients and other painting operations), add the current drawing to the list, and when requested, paint them all. 存储形状列表(图像,渐变和其他绘制操作),将当前图形添加到列表中,并在请求时绘制所有形状。
  2. Draw to a BufferedImage that is painted when requested/needed. 绘制到在需要/需要时绘制的BufferedImage See this answer for an example. 有关示例,请参见此答案

Your canvas only paints the most recent rectangle, swing painting works like that, once a paint is requested the previous buffer is cleared. 您的画布仅绘制最近的矩形,摆动绘制的工作原理是这样的,一旦请求绘制,就会清除先前的缓冲区。 What you need is a List<Rectangle> , every time you select a rectangle with the mouse, add it to the list and in the canvas draw every rectangle in the list. 您需要的是List<Rectangle> ,每次您用鼠标选择一个矩形时,将其添加到列表中,然后在画布中绘制列表中的每个矩形。 You will also need to save the color of the previous rectangles, either by making aa wrapper class for the rectangle that has color as a field or save that in a list too. 您还需要保存前一个矩形的颜色,方法是为具有颜色作为字段的矩形创建一个包装类,或者也将其保存在列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM