0

I'm just trying to draw circle using the drawOval() method and it shows only small square when I run the program. I was trying to add the constructor to the Surface class but it didn't work as well. Here is the code that I've been made:

    package swing22;

    import java.awt.BorderLayout;
    import java.awt.Color;
    import javax.swing.*;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;


    public class MyFrame {

        JFrame frame = new JFrame(" Test Frame ");
        JPanel panel = new JPanel();

        JButton button = new JButton("CLICK");
        JLabel label = new JLabel(" 33 ");

        public MyFrame(){

            gui();

        }

        public void gui(){

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    

            panel.setBackground(Color.GREEN);
            panel.add(button);
            panel.add(label);
            panel.add(new Surface());

            frame.add(panel, BorderLayout.CENTER);

            button.addActionListener(new Action());

            frame.pack();
            frame.setSize(300,300);
            frame.setVisible(true);
            frame.setResizable(false);

        }


        class Action implements ActionListener{
            @Override
            public void actionPerformed(ActionEvent arg0) {
                label.setText("new value");
            }
        }

    }


    class Surface extends JPanel {


            private void doDrawing(Graphics g) {

                Graphics2D g2d = (Graphics2D) g;

                    g.setColor(Color.red);
                    g.drawOval(80, 80, 30, 30);
                    g.fillArc(140, 140, 30, 30, 0, 90);
            }

            @Override
            public void paintComponent(Graphics g) {

                super.paintComponent(g);
                doDrawing(g);
            }
        }
MyNet
  • 15
  • 1
  • 5
  • 1
    It's a layout and preferred size problem. Your Surface looks to be extremely small, say [0, 0] since that's it's preferred size. See [this similar question and answer](http://stackoverflow.com/questions/37449227/trouble-drawing-in-jpanel). – Hovercraft Full Of Eels May 26 '16 at 22:20
  • Man thanks a lot, I just simply added this line `setPreferredSize(new Dimension(200, 200));` to the Surface constructor and it works flawlessly! – MyNet May 26 '16 at 22:40
  • 1
    @MyNet, the better solution is to override the `getPreferredSize()` method to return the proper dimension. – camickr May 27 '16 at 01:37
  • For [example](http://stackoverflow.com/a/37063037/230513). – trashgod May 27 '16 at 10:48

2 Answers2

0

you didn't set the components size, hence the component is too small to display the circle.

adjust the components size and the circle will be shown properly

public void gui(){
    ....
    Surface s = new Surface();
    s.setPreferredSize(new Dimension(200, 200));
    panel.add(new Surface());
    ...
}

you can djust the compoinent's seize by either setting it or override its getPrefferedSize()-methode

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
-1
use this class (Graphiic) and it's method (Draw_Circle)

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Graphiic
{   
    public Graphics GClass;
    public Graphics2D G2D;
    public  void Draw_Circle(JFrame jf,int radius , int  xLocation, int             yLocation)
    {
        GClass = jf.getGraphics();
        GClass.setPaintMode();
        GClass.setColor(Color.MAGENTA);
        GClass.fillArc(xLocation, yLocation, radius, radius, 0, 360);           
    }
Farhad
  • 57
  • 1