簡體   English   中英

JApplet不使用paintComponent(Graphics g)方法繪制圓

[英]JApplet does not draw circle with paintComponent(Graphics g) method

問題全在標題中:)。 我不知道我的代碼有什么問題,為什么它不會在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();
    }
}

乳化劑

該方法稱為paintComponents而不是paintComponent 它是復數的。 為了發現此類錯誤,我建議您將注解@Override添加到您可以重寫的方法中。 在這里

@Override
public void paintComponents(Graphics g)

如果沒有覆蓋方法,編譯器將給您一個錯誤。

在我准備回應時,請仔細閱讀

好的。 關於您唯一正確的事情,是從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);
        }
    }
}

請努力閱讀以上所有鏈接,它們將突出顯示您代碼中的問題區域

不要在頂級容器上繪畫!

而是添加一個JPanel (或JComponent )並重寫applet中的paintComponent(Graphics)方法。 如果直接在applet中完成,則重寫的方法將是paint(Graphics)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM