我想弄清楚Java如何知道從哪裏開始根據此圖形代碼,當我運行此代碼它顯示球從頂部向下移動,但我不明白爲什麼從頂部和爲什麼從這個地方,我知道java使用math.random()設置值,但它如何設置x和y當我試圖把任何隨機數由我自己它給出的數字是比寬它的自我(我以準確瞭解pos.x和pos.y)根據此代碼如何java知道從哪裏開始
這第一類大
package movingball;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
/**
*
* @author isslam
*/
public class Ball {
private final int RADIUS = 10;
private final Point pos;
private final Color ballColor = Color.red;
private final int CHANGE = 3;
private final int height,
width;
public Ball(int frameWidth, int frameHight) {
width = frameWidth;
hight = frameHeight;
pos = new Point();
pos.x = (int)(Math.random() * (width - RADIUS)) + RADIUS;
pos.y = (int)(Math.random() * (height/2 - RADIUS)) + RADIUS;
}
public void paint(Graphics g) {
g.setColor(ballColor);
g.fillOval(pos.x - RADIUS, pos.y - RADIUS, 2 * RADIUS, 2 * RADIUS);
}
public void move() {
if (pos.y < height - RADIUS) {
pos.translate(0, CHANGE);
}
}
}
這是第二類
package movingball;
import java.awt. * ;
import javax.swing. * ;
/**
*
* @author isslam
*/
public class ClassOfMoving extends JFrame {
protected final int FRAME_WIDTH = 240;
protected final int FRAME_HIGHT = 320;
private final Ball myBall = new Ball(FRAME_WIDTH, FRAME_HEIGHT);
public ClassOfMoving(String title) {
super(title);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(ClassOfMoving.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
myBall.paint(g);
}
public void move() {
while (true) {
myBall.move();
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
System.exit(0);
}
}
}
}
主類
package movingball;
/**
*
* @author isslam
*/
public class MovingBall {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ClassOfMoving is = new ClassOfMoving("isslam");
is.setVisible(true);
is.move();
}
我將主要方法添加到代碼 –
我需要解釋代碼而不是如何修復代碼抱歉 –