複数の長方形を 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));
}
}