Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序:彩球不会出现在硬编码位置。小程序不保持恒定大小_Java_Graphics_Applet_Coordinates - Fatal编程技术网

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

Java小程序:彩球不会出现在硬编码位置。小程序不保持恒定大小,java,graphics,applet,coordinates,Java,Graphics,Applet,Coordinates,我正在制作一个小程序,由一个在屏幕上移动的球组成。 我将球的初始X和Y坐标设置为0,但椭圆是在距离这些点一定距离处绘制的(见图)。 另一个问题是,有时当我运行应用程序时,我会得到一个具有所需宽度和高度的小程序,但在其他时候,它会更小。我不知道为什么会这样 我已将代码粘贴到下面: import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import jav

我正在制作一个小程序,由一个在屏幕上移动的球组成。 我将球的初始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();
    }
}
import java.applet.applet;
导入java.awt.Color;
导入java.awt.Graphics;
导入java.awt.Image;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.Timer;
公共类StartingPoint扩展小程序实现ActionListener
{
最终整数延迟=10;
最终int小程序_宽度=400;
最终int APPLET_高度=300;
int x=0;
int y=0;
整数半径=20;
int movementX=10;
int movementY=10;
私有图像应用图像;
私有图形appletGraphics;
公共void init()
{
super.init();
//启动点。此。设置大小(小程序宽度、小程序高度);
}
公开作废开始()
{
Timer animationTimer=新计时器(60,此);
animationTimer.start();
}
公共停车场()
{
super.stop();
}
公共空间销毁()
{
super.destroy();
}
公共空间更新(图g)
{
if(appletImage==null)
{
appletImage=createImage(this.getSize().width,this.getSize().height);
appletGraphics=appletImage.getGraphics();
}
//绘制小程序背景
setColor(getBackground());
appletGraphics.fillRect(0,0,this.getSize().width,this.getSize().height);
//绘制小程序前景
setColor(get前台());
这个。绘画(appletGraphics);
//在小程序上绘制图像
g、 drawImage(appletImage,0,0,this);
}
公共空间涂料(图g)
{
超级油漆(g);
这个.背景(颜色.蓝色);
g、 setColor(Color.RED);
g、 圆角(这个.x,这个.y,球半径,球半径);
}
@凌驾
已执行的公共无效操作(操作事件e)
{
如果((x+movementX)>(this.getWidth()-ballRadius))
{
this.x=this.getWidth()-ballRadius;
this.movementX=-movementX;
}否则如果(x+移动xthis.getHeight()-ballRadius)
{
this.y=this.getHeight()-ballRadius;
this.movementY=-movementY;
}否则如果(y+移动y<球半径)
{
y=球半径;
this.movementY=-movementY;
}
其他的
这个.y+=movementY;
这个。重新绘制();
}
}

我发现自己哪里出了问题,这真的有点愚蠢

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

else
            this.x += movementX;

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

关于大小:由于您的程序是一个小程序,它的大小将由浏览器处理,而不是小程序本身。您正在使用和IDE吗?在Eclipse中,您可以在“运行配置”对话框中设置小程序的大小。我相信它在其他IDE中也是类似的。我认为用while循环和Sleep调用来处理动画不是一个好主意。你应该看看Swing Timer类,非常感谢你的提示:)你知道为什么球没有在0,0处被吸引吗?[使用Swing Timer而不是
while(true)
内线程更新代码。]