Java Applet:彩绘球不会出现在硬编码位置。 Applet 不保持恒定的大小

发布于 2025-01-07 03:24:58 字数 2809 浏览 0 评论 0原文

我正在制作一个由在屏幕上移动的球组成的小程序。 我将球的初始 X 和 Y 坐标设置为 0,但椭圆形是在距这些点一定距离处绘制的(参见图片)。 另一个问题是,有时当我运行应用程序时,我会得到一个具有所需宽度和高度的小程序,但有时它会较小。我不知道为什么会发生这种情况。

画球不会出现在硬编码位置

我粘贴了下面的代码:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


public class StartingPoint extends Applet implements ActionListener
{
    final int DELAY             = 10;
    final int APPLET_WIDTH      = 400; 
    final int APPLET_HEIGHT     = 300;

    int x=0;
    int y=0;
    int ballRadius  = 20;
    int movementX   = 10;
    int movementY   = 10;

    private Image appletImage;
    private Graphics appletGraphics;

    public void init()
    {
        super.init();
        //StartingPoint.this.setSize( APPLET_WIDTH, APPLET_HEIGHT );
    }


    public void start() 
    {
        Timer animationTimer = new Timer(60, this);
        animationTimer.start();
    }

    public void stop()
    {
        super.stop();
    }

    public void destroy()
    {
        super.destroy();
    }


    public void update(Graphics g)
    {
        if(appletImage == null)
        {
            appletImage = createImage( this.getSize().width, this.getSize().height );
            appletGraphics = appletImage.getGraphics();
        }

        //draw applet background
        appletGraphics.setColor(getBackground());
        appletGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);

        //draw applet foreground
        appletGraphics.setColor(getForeground());
        this.paint( appletGraphics );

        //draw images on the applet
        g.drawImage(appletImage, 0, 0, this);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        this.setBackground(Color.BLUE);
        g.setColor(Color.RED);
        g.fillOval(this.x, this.y, ballRadius, ballRadius);
    }


    @Override
    public void actionPerformed(ActionEvent e)
    {
        if( (x+movementX) > (this.getWidth()-ballRadius))
        {
            this.x = this.getWidth()-ballRadius;
            this.movementX = -movementX;
        }else if( x+movementX < ballRadius)
        {
            this.x = ballRadius;
            this.movementX = -movementX;
        }
        else
            this.x += movementX;

        if( (y+movementY) > this.getHeight()-ballRadius)
        {
            this.y = this.getHeight()-ballRadius;
            this.movementY = -movementY;
        }else if( y+movementY < ballRadius)
        {
            this.y = ballRadius;
            this.movementY = -movementY;
        }
        else
            this.y += movementY;


        this.repaint();
    }
}

I'm making an applet consisting of a ball that moves across the screen.
I set the ball's initial X and Y co-ordinates to be 0, however the oval is drawn at some distance from these points (See image).
The other problem is that sometimes when I run the application, I get an applet with the desired width and height but at other times, it is smaller. I'm not sure why this happens.

painted ball does not appear in hard coded position

I've pasted the code below:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;


public class StartingPoint extends Applet implements ActionListener
{
    final int DELAY             = 10;
    final int APPLET_WIDTH      = 400; 
    final int APPLET_HEIGHT     = 300;

    int x=0;
    int y=0;
    int ballRadius  = 20;
    int movementX   = 10;
    int movementY   = 10;

    private Image appletImage;
    private Graphics appletGraphics;

    public void init()
    {
        super.init();
        //StartingPoint.this.setSize( APPLET_WIDTH, APPLET_HEIGHT );
    }


    public void start() 
    {
        Timer animationTimer = new Timer(60, this);
        animationTimer.start();
    }

    public void stop()
    {
        super.stop();
    }

    public void destroy()
    {
        super.destroy();
    }


    public void update(Graphics g)
    {
        if(appletImage == null)
        {
            appletImage = createImage( this.getSize().width, this.getSize().height );
            appletGraphics = appletImage.getGraphics();
        }

        //draw applet background
        appletGraphics.setColor(getBackground());
        appletGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);

        //draw applet foreground
        appletGraphics.setColor(getForeground());
        this.paint( appletGraphics );

        //draw images on the applet
        g.drawImage(appletImage, 0, 0, this);
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        this.setBackground(Color.BLUE);
        g.setColor(Color.RED);
        g.fillOval(this.x, this.y, ballRadius, ballRadius);
    }


    @Override
    public void actionPerformed(ActionEvent e)
    {
        if( (x+movementX) > (this.getWidth()-ballRadius))
        {
            this.x = this.getWidth()-ballRadius;
            this.movementX = -movementX;
        }else if( x+movementX < ballRadius)
        {
            this.x = ballRadius;
            this.movementX = -movementX;
        }
        else
            this.x += movementX;

        if( (y+movementY) > this.getHeight()-ballRadius)
        {
            this.y = this.getHeight()-ballRadius;
            this.movementY = -movementY;
        }else if( y+movementY < ballRadius)
        {
            this.y = ballRadius;
            this.movementY = -movementY;
        }
        else
            this.y += movementY;


        this.repaint();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2025-01-14 03:24:58

我发现我错在哪里了,这确实有点愚蠢。

actionPerformed() 方法中的这些代码行偏移了 x 和 y 位置:

else
            this.x += movementX;

//....
else
            this.y += movementY;

I figured out where I went wrong and it really is kind of stupid.

These lines of code in the actionPerformed() method offset the x and y positions:

else
            this.x += movementX;

//....
else
            this.y += movementY;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文