【问题标题】:JApplet does not draw circle with paintComponent(Graphics g) methodJApplet 不使用paintComponent(Graphics g) 方法绘制圆
【发布时间】:2012-09-05 14:05:09
【问题描述】:

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

乳液

【问题讨论】:

  • 为第一条线索添加@Override 符号。
  • public boolean mouseDown(Event e, int x, int y) OMG 你从哪里得到这个代码的?在上个千年的某个地方? -> ref. 已弃用。 从 JDK 版本 1.1, 替换为 processMouseEvent(MouseEvent)。
  • 不要在paint方法中调用repaint,你最终会把你的CPU运行到100%
  • 使用定时器,不要停止 EDT

标签: java swing graphics paintcomponent japplet


【解决方案1】:

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

好的。你唯一做对的事情就是从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);
        }
    }
}

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

【讨论】:

  • 非常感谢 :) 我写了很多 JFrames,但我第一次写了一个 japplet。一件事 - 如果你实现 mouselistener,你必须包含所有继承的抽象方法:)
  • 不,如果你不使用它们就不行(在我的例子中,我使用了MouseAdapter,它提供了来自MosueListener 接口的所有方法的空实现。这样你可以选择忽略你不想使用的那些;))
【解决方案2】:

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

@Override
public void paintComponents(Graphics g)

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

【讨论】:

  • @imulsion 如果你覆盖void paint(Graphics g)而不是paintComponents,它会起作用吗?
【解决方案3】:

不要绘制到顶级容器!

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

【讨论】:

    猜你喜欢
    • 2014-05-23
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2015-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多