JApplet不使用paintComponent(Graphics g)方法绘制圆

时间:2012-09-04 06:52:31

标签: java swing graphics paintcomponent japplet

问题'所有标题:)。我不知道我的代码有什么问题,以及为什么它不能将圆圈绘制到Japplet上。你可以帮帮我吗?

这是我的代码:

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Event;

public class BouncingBall extends JApplet {
    private static final long serialVersionUID = 1L;
    boolean b = true;
    long speed = 50;
    int pos = 250;

    public void init(){
        setSize(500,500);
    }
    public boolean mouseDown(Event e, int x, int y)
    {


        if(y>250)
        {
            speed = speed - 10;
        }
        else
        {
            speed = speed + 10;
        }

        repaint();
        return true;
    }
    public void paintComponents(Graphics g)
    {
        g.drawOval(250,pos,100,100);
        if(speed <= 20)
        {
            speed++;
            repaint();
        }
        try
        {
            Thread.sleep(speed);
        }
        catch(InterruptedException e){e.printStackTrace();}
        if(pos>=400)
        {
            b = false;
        }
        if(pos<=100)
        {
            b = true;
        }
        if(b==true)
        {
            pos = pos +5;
        }
        else
        {
            pos = pos -5;
        }
        repaint();
    }
}

Imulsion

3 个答案:

答案 0 :(得分:3)

该方法名为paintComponents,而不是paintComponent。这是复数。要发现此类错误,我建议您将注释@Override添加到方法中,然后覆盖。 这将是

@Override
public void paintComponents(Graphics g)

如果没有要覆盖的方法,编译器会给你一个错误。

答案 1 :(得分:3)

在准备我的回复时,请仔细阅读

好。关于你做的唯一正确的事情是从JApplet

延伸

你的“油漆”方法完全混乱......

public void paintComponents(Graphics g) {
   // Where's the super call???  All paint methods have a super
   // if you don't call it, expect really bad things to happen...
   if(speed <= 20)
    {
        speed++;
        // Don't do this
        repaint();
    }
    try
    {
        // NEVER, EVER do this, EVER
        Thread.sleep(speed);
    }
    catch(InterruptedException e){e.printStackTrace();}

    // These choices should be made else where.
    if(pos>=400)
    {
        b = false;
    }
    if(pos<=100)
    {
        b = true;
    }
    if(b==true)
    {
        pos = pos +5;
    }
    else
    {
        pos = pos -5;
    }
    // NEVER DO THIS IN A PAINT METHOD...
    repaint();

正如已经指出的那样,不要使用mouseDown方法,而是使用MouseListener代替

正如已经指出的那样,不要在顶层容器(JApplet或任何类型的窗口或框架)上绘画,而是使用自定义组件。

public class BouncingBall extends JApplet {

    private static final long serialVersionUID = 1L;

    public void init() {
        setSize(500, 500);
        setLayout(new BorderLayout());
        add(new BouncyPane());
    }

    protected class BouncyPane extends JPanel {

        private boolean b = true;
        private int speed = 50;
        private int pos = 250;
        private Timer timer;
        private int amount = 10;

        public BouncyPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {

                    if (speed > 250) {
                        amount = -10;
                    } else if (speed <= 0) {
                        amount = 10;
                    }

                    speed += amount;
                    timer.stop();
                    timer.setDelay(speed);
                    timer.restart();

                    repaint();
                }
            });

            timer = new Timer(speed, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (pos >= 400) {
                        b = false;
                    }
                    if (pos <= 100) {
                        b = true;
                    }
                    if (b == true) {
                        pos = pos + 5;
                    } else {
                        pos = pos - 5;
                    }

                    repaint();

                }
            });

            timer.setRepeats(true);
            timer.setCoalesce(true);
            timer.start();
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
            g.setColor(Color.RED);
            g.drawOval(250, pos, 100, 100);
        }
    }
}

请努力阅读以上所有链接,他们会突出显示您代码中的问题区域

答案 2 :(得分:2)

不要画到顶级容器!

相反,添加JPanel(或JComponent)并覆盖小程序中的paintComponent(Graphics)方法。如果直接在applet中完成,则覆盖的方法为paint(Graphics)