Drawing circle and setting it visible on canvas

Ted :

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

Daniel Williams :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=163251&siteId=1