Idea of this app is to draw a shape on JPanel. First you would need to choose a shape by clicking a button and then click somewhere on the panel to draw it by using one MouseListener, but I can't figure out how to implement which button is activated after I click it. I tried using getSource(), but it doesn't seem to work. Here's the code:
public class Draw extends JFrame {
private JPanel contentPane;
private Point startPoint = null, endPoint = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Draw frame = new Draw();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Draw() {
getContentPane().setLayout(new BorderLayout(0, 0));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 800, 600);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
final PnlDrawing pnldrawing = new PnlDrawing();
contentPane.add(pnldrawing, BorderLayout.CENTER);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JSeparator separator = new JSeparator();
separator.setOrientation(SwingConstants.VERTICAL);
panel.add(separator);
JButton btnSelect = new JButton("Select");
panel.add(btnSelect);
JButton btnModify = new JButton("Modify");
panel.add(btnModify);
JButton btnRemove = new JButton("Remove");
panel.add(btnRemove);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.NORTH);
final JButton btnAddPoint = new JButton("Add Point");
panel_1.add(btnAddPoint);
final JButton btnAddLine = new JButton("Add Line");
panel_1.add(btnAddLine);
final JButton btnAddCircle = new JButton("Add Circle");
panel_1.add(btnAddCircle);
final JButton btnAddDonut = new JButton("Add Donut");
panel_1.add(btnAddDonut);
final JButton btnAddRectangle = new JButton("Add Rectangle");
panel_1.add(btnAddRectangle);
pnldrawing.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(e.getSource().equals(btnAddPoint)) {
pnldrawing.shapes.add(new Point(e.getX(), e.getY()));
pnldrawing.repaint();
}
if(e.getSource().equals(btnAddLine)) {
if (startPoint == null)
{
startPoint = new Point(e.getX(),e.getY());
}
else if(endPoint == null)
{
endPoint = new Point(e.getX(),e.getY());
}
if (startPoint != null && endPoint != null)
{
Line line = new Line(startPoint,endPoint);
pnldrawing.shapes.add(line);
pnldrawing.repaint();
startPoint = null;
endPoint = null;
}
}
if(e.getSource().equals(btnAddCircle)) {
DlgCircle drawCircle = new DlgCircle();
drawCircle.setVisible(true);
Point center = new Point(e.getX(),e.getY());
if (drawCircle.isOk)
{
pnldrawing.shapes.add(new Circle(center,Integer.parseInt(drawCircle.txtCircle.getText())));
pnldrawing.repaint();
}
}
if(e.getSource().equals(btnAddDonut)) {
DlgDonut drawDonut = new DlgDonut();
drawDonut.setVisible(true);
Point center = new Point(e.getX(),e.getY());
if (drawDonut.isOk)
{
pnldrawing.shapes.add(new Donut(center,Integer.parseInt(drawDonut.txtRadius.getText()),Integer.parseInt(drawDonut.txtInner.getText())));
pnldrawing.repaint();
}
}
if(e.getSource().equals(btnAddRectangle)) {
DlgRectangleSec drawRect = new DlgRectangleSec();
drawRect.setVisible(true);
Point upperLeft = new Point(e.getX(),e.getY());
if(drawRect.isOk) {
pnldrawing.shapes.add(new Rectangle(upperLeft,Integer.parseInt(drawRect.txtWidth.getText()),Integer.parseInt(drawRect.txtHeight.getText())));
pnldrawing.repaint();
}
}
}
});
}
}