Java Applet:彩绘球不会出现在硬编码位置。 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();
}
}
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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现我错在哪里了,这确实有点愚蠢。
actionPerformed() 方法中的这些代码行偏移了 x 和 y 位置:
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: