2

I am working on a simple object drawing program using Swing in Java. My program simply should draw shapes according to buttons when clicked, and move any shapes with the mouse. I have four buttons which draw rectangle, circle and square on screen. So far I did managed to draw to shapes when you click on buttons. but i want to move the shapes on screen which it did not work out.

The problem is this: When I click on circle shape to drag it around with mouse, it clears all the screen and noting is on the screen.

And, is there a way to clean all the screen when I click on clear button?

Thank you?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class PaintProject extends JComponent implements ActionListener, 
    MouseMotionListener {

    private  int CircleX=0;
    private  int CircleY=0;
    private  int RectX=100;
    private  int RectY=100;
    private  int SquareX=300;
    private  int SquareY=200;


    public static void main(String[] args) {
        JFrame frame = new JFrame("NEW PAINT PROGRAME!");

        JButton CircleButton = new JButton("Circle");
        CircleButton.setActionCommand("Circle");

        JButton RectangleButton = new JButton("Rectangle");
        RectangleButton.setActionCommand("Rectangle");

        JButton SquareButton = new JButton("Square");
        SquareButton.setActionCommand("Square");

        PaintProject paint = new PaintProject();

        CircleButton.addActionListener(paint);
        RectangleButton.addActionListener(paint);
        SquareButton.addActionListener(paint);


        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(paint);
        frame.add(CircleButton);
        frame.add(RectangleButton);
        frame.add(SquareButton);


        frame.addMouseMotionListener(paint);

        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(500, 500);

    }

    private void drawCircle() {
        Graphics g = this.getGraphics();
        g.setColor(Color.red);
        g.fillOval(CircleX, CircleY, 100, 100);
    }

    private void drawRectangle() {
        Graphics g = this.getGraphics();
        g.setColor(Color.green);
        g.fillRect(RectX, RectY, 100, 300);
    }
    private void drawSquare() {
        Graphics g = this.getGraphics();
        g.setColor(Color.blue);
        g.fillRect(SquareX, SquareY, 100, 100);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Circle")) {
        drawCircle();
    }
    else if (command.equals("Rectangle")) {
        drawRectangle();
    }
    else if (command.equals("Square")) {
        drawSquare();
    }

    }

    @Override
    public void mouseDragged(MouseEvent e) {
        CircleX=e.getX();
        CircleY=e.getY();
        repaint();
    }

}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 2
    `Graphics g = this.getGraphics();` See the [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/) lesson for the correct way to do it. – Andrew Thompson Jul 10 '16 at 14:25
  • 1
    As to a general approach to this, I would keep an `ArrayList` to store all the shapes the user asked for (by clicking on buttons). When the user clicks a button, add a [`Shape`](http://docs.oracle.com/javase/8/docs/api/java/awt/Shape.html) to the array list and call `repaint()`. That will end up calling `paintComponent(Graphics)` with a valid `Graphics` instance. In the paint component method, iterate the list and draw them all. – Andrew Thompson Jul 10 '16 at 14:27
  • I did not get your idea! i want to go forward with this code, i dont know where i am making mistake. –  Jul 10 '16 at 14:37
  • 1
    *"I did not get your idea!"* ***What*** don't you understand? BTW - you went through the entire 'Performing Custom Painting' lesson in 12 minutes? Somehow I think you ignored that advice to try and get me (or someone else) to write your code for you. If that is not the case, prove it by going through the tutorial, and being specific about anything in it that you don't understand. Note that SO is not a help desk, it is not a code generation machine. Specific questions get specific answers, but at the moment you're being very vague & showing little sign of effort at helping yourself. – Andrew Thompson Jul 10 '16 at 14:48
  • Dear Andrew! What i have seen in "Performing Custom Painting" was something similer to me.. I can move the shape like it was done there.But my problem was with several shapes in one screen.To be more specific, i can move the square if i only have square there but when i draw all shapes and click on one of them ,the screen clears everything. –  Jul 10 '16 at 14:53
  • I've outlined a way forward below. If you have problems, please edit your question to include a [mcve] that shows your current approach. – trashgod Jul 10 '16 at 15:26
  • I couldnt run [link](http://stackoverflow.com/questions/11942961/resizing-issue-with-canvas-within-jscrollpane-within-jsplitpane/11944233#11944233) –  Jul 10 '16 at 15:56
  • You forgot to download the linked JAR and add it to your classpath. – trashgod Jul 10 '16 at 16:13
  • The link opens another Stackoverflow page which contains a part of the code not the complete code. –  Jul 10 '16 at 16:34
  • That page contains a link to the complete code. Any trouble with the simpler example? – trashgod Jul 10 '16 at 17:18
  • Couldnt find the whole code Sir! Can you post the link here please? –  Jul 10 '16 at 18:40
  • The link is named `draw.GraphPanel` on the page you mentioned. Any trouble with the "Drag me!" example? – trashgod Jul 10 '16 at 19:13

1 Answers1

3

As noted here and here, getGraphics() is not how to perform custom painting in Swing. Instead, override paintComponent() to render the desired content. It looks like you want to drag shapes using the mouse. A basic approach to moving a selected object is shown here; substitute your fillXxx() invocation for the drawString() shown there. For multiple shapes, use a List<Shape>; the clear command then becomes simply List::clear. A complete example is cited here; it features moveable, selectable, resizable, colored nodes connected by edges.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045