繁体   English   中英

Java Applet:彩绘球未出现在硬编码位置。 Applet无法保持恒定大小

[英]Java Applet: painted ball does not appear in the hard-coded position. Applet does not maintain constant size

我正在制作一个由在屏幕上移动的球组成的applet。 我将球的初始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();
    }
}

我弄清楚哪里出了问题,这确实有点愚蠢。

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

else
            this.x += movementX;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM