'Drawing multiple objects in Java [duplicate]
I would like to draw multiple objects like ovals, rectangles and lines together. But somehow I can draw only an object. When I try to draw another shape more, the object I drew is deleted and I can only draw new thing. I've tried to use while loop in the paintComponent Method. But it seems while loop makes the program freeze. I would like to know why it works like that too. How can I solve this?
package package22;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
public class LecGUI2 {
// ? 응용 프레임을 생성하고 디스플레이 한다.
public static void main(String[] args) {
JFrame frame = new JFrame("Rubber Lines");
frame.setPreferredSize(new Dimension(500, 500));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new RubberLinesPanel2());
frame.pack();
frame.setVisible(true);
}
}
class RubberLinesPanel2 extends JPanel {
final int CIRCLE = 0;
final int LINE = 1;
final int RECTANGLE = 2;
private int polygon = LINE;
private JRadioButton b1, b2, b3;
private boolean clear = false;
public RubberLinesPanel2() {
// 추가하시오
DrawButtonListener buttonListener = new DrawButtonListener();
JButton clearButton = new JButton("Clear");
b1 = new JRadioButton("Circle");
b2 = new JRadioButton("Line");
b3 = new JRadioButton("Rectangle");
b1.addActionListener(buttonListener);
b2.addActionListener(buttonListener);
b3.addActionListener(buttonListener);
clearButton.addActionListener(buttonListener);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(b1);
buttonGroup.add(b2);
buttonGroup.add(b3);
buttonGroup.add(clearButton);
add(b1);
add(b2);
add(b3);
DrawPanel drawPanel = new DrawPanel();
add(drawPanel);
add(clearButton);
}
private class DrawButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
// 추가하시오
if (b1.isSelected())
polygon = CIRCLE;
else if (b2.isSelected())
polygon = LINE;
else if (b3.isSelected())
polygon = RECTANGLE;
else
clear = true;
}
}
class DrawPanel extends JPanel {
private Point point1 = null;
private Point point2 = null;
// ? 구성자: 이 패널이 마우스 이벤트들을 감청하도록 설정한다.
public DrawPanel() {
LineListener listener = new LineListener();
addMouseListener(listener);
addMouseMotionListener(listener);
setBackground(Color.black);
setPreferredSize(new Dimension(500, 400));
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
// 구현하시오
page.setColor(Color.yellow);
if (point1 != null && point2 != null && polygon == 1)
page.drawLine(point1.x, point1.y, point2.x, point2.y);
else if (point1 != null && point2 != null && polygon == 2)
page.drawRect(point1.x, point1.y, Math.abs(point1.x - point2.x), Math.abs(point1.y - point2.y));
else if (point1 != null && point2 != null && polygon == 0)
page.drawOval(point1.x, point1.y, Math.abs(point1.x - point2.x), Math.abs(point1.y - point2.y));
}
private class LineListener implements MouseListener, MouseMotionListener {
public void mousePressed(MouseEvent event) {
point1 = event.getPoint();
}
public void mouseDragged(MouseEvent event) {
point2 = event.getPoint();
repaint();
}
public void mouseClicked(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
repaint();
};
public void mouseEntered(MouseEvent event) {
};
public void mouseExited(MouseEvent event) {
};
public void mouseMoved(MouseEvent event) {
};
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|