0

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

}

Here's the GUI

2 Answers2

2

First of all you might want to change your painting logic to make it more user friendly. That is you could:

  1. Use the mousePressed event to save the mouse point.

  2. Add logic in the mouseDragged event to paint the shape as the mouse is being dragged.

  3. Finally, in the mouseReleased event you would make the drawing shape permanent.

Check out Custom Painting Approaches for working example. It only paints rectangle, but it should give you a starting point.

but I can't figure out how to implement which button is activated

Use JRadioButton instead of JButton. The button will remain selected.

Then the logic in your MouseListener code becomes something like:

if (addLineButton.isSelected())
    // do something
else if (addRectangleButton.isSelected())
    //  do something
camickr
  • 321,443
  • 19
  • 166
  • 288
0

Am I correct that you want to get the button pressed on the mouse? If so, take a look at this link to specify actions done by a certain click. Then you can set a String variable to what button was clicked like String buttonclicked = “left click”.

Right click mouse event

LuckyBandit74
  • 417
  • 1
  • 4
  • 10
  • Not exactly. I've added a picture of my GUI if it helps. Basicly my only problem is that I can't get into if( ) fucntions that hold my drawing implentation inside mousePressed method. I would like to be able to get into these if() functions by pressing buttons that are shown (such as Add Point, Add Line etc.) – Marko Mrđa Mar 28 '21 at 22:08