无法在画布上看到圆圈

时间:2016-03-29 01:08:02

标签: java canvas applet awt panel

我真的无法理解这一点,我需要你的帮助。我创造了一个简单的 小程序。我有一个面板和一个画布。我画在画布上。我添加画布 到小组。我将面板添加到applet。我试图画一个圆圈 画布,但我看不到它。如果我调整小程序的大小,我可以看到圆圈 在画布后面闪烁。请帮忙。

        import java.applet.*;
        import java.awt.*;


        public class TestCanvas extends Applet implements Runnable
        {
            Panel myPanel = new Panel();
            int x = 50;
            int y = 50;
            int width = 100;
            int height = 100;
            boolean startBall = false;

            Graphics bufferGraphics;
            Image offscreen;

            Thread t;

            public void init()
            {
                canvas.setSize(500,500);

                //canvas.setMaximumSize(new Dimension(500,500));
                //canvas.setMinimumSize(new Dimension(50,50));
                myPanel.add(canvas);
                add(myPanel);
                //For double buffering
                offscreen = createImage(canvas.getWidth(),canvas.getHeight());
                bufferGraphics = offscreen.getGraphics();

            }

            public void start()
            {
                t = new Thread();
                t.start();
                startBall = true;

            }

            public void stop()
            {
                t = null;
            }

            public void destroy()
            {
                t = null;
            }

            public void run()
            {
                for ( ; ; )
                {
                    try
                    {
                        canvas.repaint();
                        Thread.sleep(100);
                    }catch(InterruptedException e){}



                }
            }

            class myCanvas extends Canvas
            {
                private static final long serialVersionUID = 1L;

                public myCanvas()
                {
                    setBackground(Color.yellow);
                    setSize(500, 300);
                    //setBounds(0, 0, 500, 300);

                }   

            }

            myCanvas canvas = new myCanvas();

            public void paint(Graphics g)
            {
                g = canvas.getGraphics();
                bufferGraphics.clearRect(0,0,canvas.getWidth(), canvas.getHeight());
                bufferGraphics.setColor(Color.black);
                //x++;
                //y++;
                bufferGraphics.setColor(Color.black);
                bufferGraphics.fillOval(x, y, width, height);       
                g.drawImage(offscreen,0,0,null);        
            }

            public void update(Graphics g)
            {
                canvas.paint(g);
            }



        }

1 个答案:

答案 0 :(得分:1)

它已经到了我不会触及涉及applet或AWT的问题的阶段,直到提出问题的人对他们为什么使用这两个问题给出了合理的回答。 OTOH你已经部分完成了,所以这里有一个动画球的工作示例。

仔细查看代码与此代码之间的差异。

import java.applet.*;
import java.awt.*;

public class TestCanvas extends Applet implements Runnable {

    Panel myPanel = new Panel(new GridLayout());
    int x = 0;
    int y = 0;
    int width = 100;
    int height = 100;
    boolean startBall = false;
    myCanvas canvas = new myCanvas();
    Thread t;

    public void init() {
        myPanel.add(canvas);
        System.out.println("" + this.getLayout());
        /* A Single component added to a no args GridLayout will be stretched 
         to fill the avilable space. */
        this.setLayout(new GridLayout());
        add(myPanel);
        t = new Thread(this);
        t.start();
    }

    public void start() {
        t = new Thread();
        t.start();
        startBall = true;
    }

    public void stop() {
        t = null;
    }

    public void destroy() {
        t = null;
    }

    public void run() {
        for (;;) {
            try {
                canvas.repaint();
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
        }
    }

    class myCanvas extends Canvas {

        private static final long serialVersionUID = 1L;

        public myCanvas() {
            setBackground(Color.yellow);
        }

        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.clearRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.black);
            x++;
            y++;
            g.setColor(Color.black);
            g.fillOval(x, y, width, height);
        }
    }
}