Search code examples
javaswinggraphics2d

Java drawing application


I need to develop an application which has 3 buttons for drawing a line,a rectangle and a circle. The application should behave like this: user clicks on a button to draw a wanted shape, mouse cursor changes to a dot, user moves the mouse down to some container, draws two dots by clicking the mouse twice at desired locations and then the wanted shape is drawn using those two dots. From the information I have already gathered, I know I should use a MouseClickListener for drawing the dots and then call a shape class with parameters passed from the dot class to draw the shape. What I need to know is what container to use for the shapes, where to put the MouseClickListener in order to allow drawing only in that container and how to restrict the user from drawing any more points until a button is pressed again. So far this is my code: `public class MyPanel {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MyPanel window = new MyPanel();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MyPanel() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setResizable(false);
    frame.setBounds(100, 100, 500, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JPanel panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setBounds(10, 25, 474, 336);
    frame.getContentPane().add(panel);

    JButton btnLine = new JButton("Line");
    btnLine.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            drawPoint draw = new drawPoint();
        }
    });
    btnLine.setBounds(10, 0, 110, 23);
    frame.getContentPane().add(btnLine);

    JButton btnRectangle = new JButton("Rectangle");
    btnRectangle.setBounds(196, 0, 110, 23);
    frame.getContentPane().add(btnRectangle);

    JButton btnCircle = new JButton("Circle");
    btnCircle.setBounds(374, 0, 110, 23);
    frame.getContentPane().add(btnCircle);
 }
}
public class drawPoint implements MouseListener {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub
    getCoordinates
    drawAPoint
    drawLine(coordinates)
}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

 }

}

Solution

  • What I need to know is what container to use for the shapes

    Typically, components with custom draw are done by subclassing JPanel and overriding the paintComponent method. Less customary, and arguably more clean from an OO perspective, one may subclass JComponent. But you will find far fewer example code on the web via this route.

    where to put the MouseClickListener

    On the JPanel subclass would probably work.

    in order to allow drawing only in that container

    You can't really prevent a user from clicking in the JPanel and dragging outside it. But, you can try to detect this condition, and make sure the code ignores this kind of input.

    and how to restrict the user from drawing any more points until a button is pressed again.

    Use variables. E.g., a boolean variable ready which is true initially, gets set to false when drawing starts and is reset to true only by a button press. And have your drawing points handler check the ready value and only initiate drawing if it is true. You may need other variables to keep track of how many additional clicks are allowed as part of the current drawing operation.