0

I have the following code, I am trying to add a circle to my frame when the button is clicked, I tried calling circle class from my main function, but do not know how to add a circle after that. please help me!

public static void main(String[] args) {
    // Create a frame and put a scribble pane in it
    JFrame frame = new JFrame("FrameFormula");
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    final FrameFormula scribblePane = new FrameFormula();
    JPanel shapePanel = new JPanel();
    JButton horGap = new JButton("Add a circle");
    horGap.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                int[] circleValues = generateRandomValues(300, 300, 50, 150);
                int x = circleValues[0];
                int y = circleValues[1];
                int width = circleValues[2];
                int height = width;
                Circle circle = new Circle(x, y, width, height);
                //scribblePane.addCircle(circle);
            }
        });
shapePanel.add(horGap);
frame.add(shapePanel, BorderLayout.SOUTH);
frame.getContentPane().add(scribblePane, BorderLayout.CENTER);
}

I have created separate classes for creating circle with x and y points.

private static int[] generateRandomValues(int maxX, int maxY, 
                                       int minSize, int maxSize) {
        Random random = new Random();
        int[] values = new int[3];
        values[0] = random.nextInt(maxX);
        values[1] = random.nextInt(maxY);
        values[2] = Math.min(random.nextInt(maxSize) + minSize, maxSize);
        return values;
    }

    static class Circle {

        int x, y, width, height;

        public Circle(int x, int y, int width, int height) {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
        }

        public void draw(Graphics g) {
            g.drawOval(x, y, width, height);
        }
    }
Mitesh
  • 145
  • 1
  • 13
  • 1
    Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Oct 02 '15 at 12:20
  • i have edited the code again, please check now. – Mitesh Oct 02 '15 at 12:25

2 Answers2

1

it remains for a second and gets removed if something else we do on the panel

Check out Custom Painting Approaches for the two common ways to do custom painting:

  1. Add objects to an ArrayList and then paint all the objects in the list
  2. Paint the objects to a BufferedImage and then paint the BufferedImage

The demo code shows how to randomly add Rectangles using the mouse. Your code would obviously be slightly different because you would add the Rectangles with a button.

So start with the working code to get it working with a button. Then change the code to work for circles instead of rectangles.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • how to make it for a circle? – Mitesh Oct 02 '15 at 16:20
  • @Mitesh, Change the code to use an `Ellipse2D.Double` instead of a `Rectangle`. – camickr Oct 02 '15 at 16:33
  • I have created a jpanel with paintcomponent and i have a scribblePane which has draw functionality, i want the jpanel component to overwrite my scribblePane. How to do it? How to add an extra panel and overwrite the scribbledraganddrop panel? Here is link to my code: http://pastebin.com/QS3uhJeV – Mitesh Oct 02 '15 at 17:04
  • You are welcome for the help. I gave you working examples that you can start with and modify. I'm not going to debug your code for you. I have absolutely no ideas why you think you need two panels. – camickr Oct 02 '15 at 17:20
  • Ok, actually i am stuck in this, not able to add panel item to scribblePane, it adds separately but it doesnot work on scribblePane. Would appreciate your help. – Mitesh Oct 02 '15 at 17:29
  • @Mitesh, I have no idea what a "panel item" is. I showed you how to add a "shape" to a panel and paint the shape. Start with the working code and customize it for your exact requirement. – camickr Oct 02 '15 at 17:52
  • Here is link to my code: pastebin.com/QS3uhJeV, please debug and tell me where i am wrong, what should i do to include my drawPane panel in scribblePane. I want to click the Add a circle button and the circle should be added in my scribblePane not the drawPane panel. Please help! – Mitesh Oct 02 '15 at 17:54
  • @Mitesh So this question on "how to add a circle on a button click" has been answered. Then the answer should be "accepted" by clicking on the check mark so people know that question has been answered. `what should i do to include my drawPane panel in scribblePane.` - that is a different question. Update your other question to include a proper [SSCCE](http://sscce.org/) that demonstrates the problem. That is create a frame with a scribble panel and a draw panel. 95% of your other code is not related to your problem description. If you want help simplify the problem. – camickr Oct 02 '15 at 18:39
0

What you do is creating a circle but not calling the draw-Method. You would use something like:

Circle circle = new Circle(x, y, width, height);
Graphics g = shapepanel.getGraphics();
circle.draw(g);

But that leads to problems so I would suggest you take a look at this thread: Drawing an object using getGraphics() without extending JFrame

There is explained why and how to draw something consistently in a JPanel.

Community
  • 1
  • 1
SkryptX
  • 813
  • 1
  • 9
  • 24
  • but it remains for a second and gets removed if something else we do on the panel. :( – Mitesh Oct 02 '15 at 12:54
  • That's why you should click on the link :D That's exactely the problem with it. You will need to redefine the `paintComponent` from `JPanel` because you are just manipulating your panel once, but the panel gets repainted quite often and then the circle vanishes. – SkryptX Oct 02 '15 at 12:57
  • Don't use the getGraphics() method (as you have noticed painting is only temporary). Custom painting is done by overriding the paintComponent() method. – camickr Oct 02 '15 at 17:20