0

问题都在标题中:)。我不知道我的代码有什么问题以及为什么它不会将圆圈绘制到 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();
    }
}

乳液

4

3 回答 3

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而是使用 a

正如已经指出的那样,不要在顶级容器(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);
        }
    }
}

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

于 2012-09-04T09:35:08.377 回答
3

该方法被调用paintComponents而不是paintComponent. 是复数。要发现此类错误,我建议您将注释添加@Override到您重写的方法中。就在这里

@Override
public void paintComponents(Graphics g)

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

于 2012-09-04T06:58:14.780 回答
2

不要在顶级容器上绘画!

相反,添加一个JPanel(或JComponent)并覆盖paintComponent(Graphics)小程序中所做的方法。如果直接在小程序中完成,覆盖的方法将是paint(Graphics).

于 2012-09-04T07:18:36.073 回答