Ted
Ted

Reputation: 23

Drawing circle and setting it visible on canvas

I have some codes making jFrame and canvas. They are visible. But I don't know how to set a circle visible on the canvas

    package unitcirclevisulaization;


    import java.awt.Canvas;
    import java.awt.Graphics;
    import javax.swing.JFrame;

    public class UnitCircleVisulaization extends Canvas {

        public static void main(String[] args) {
            JFrame frame = new JFrame("Unit Circle");
            Canvas canvas = new Canvas();
            canvas.setSize(800,800);
            frame.add(canvas);
            frame.pack();
            frame.setVisible(true);
        }

        public void paint(Graphics g)
        {
            g.fillOval(400, 400, 400, 400);
        }

    }

I just want a circle to appear on the canvas, and be able to set color of the circle

Upvotes: 2

Views: 566

Answers (1)

Daniel Williams
Daniel Williams

Reputation: 665

Your paint method is inside the UnitCircleVisulaization class which is never instantiated, so the paint method is never called.

To fix this, just replace the line

Canvas canvas = new Canvas();

with

Canvas canvas = new UnitCircleVisulaization();

This works because UnitCircleVisulaization extends Canvas which contains the paint method. Then the paint method is automatically called when the frame is made visible.

Upvotes: 1

Related Questions