📄 ball.java
字号:
import java.awt.*;
import javax.swing.*;
public class Ball
{
private int xpos;
private int ypos;
private int diameter=30;
private int dx=10;
private int dy=10;
private JPanel panel;
private Color color=Color.black;
public Ball()
{
}
public Ball(int x,int y,JPanel panel)
{
xpos=x;
ypos=y;
this.panel=panel;
dx=(int)(Math.random()*31);
dy=(int)(Math.random()*31);
int sgnx=(int)(Math.random()*2);
int sgny=(int)(Math.random()*2);
if(sgnx==0)
dx=-dx;
if(sgny==0)
dy=-dy;
}
public void drawBall(Graphics g)
{
g.fillOval(xpos,ypos,diameter,diameter);
}
public void motion()
{
xpos+=dx;
ypos+=dy;
if(xpos<0)
{
xpos=0;
dx=-dx;
}
if(xpos>panel.getWidth()-diameter)
{
xpos=panel.getWidth()-diameter;
dx=-dx;
}
if(ypos<0)
{
ypos=0;
dy=-dy;
}
if(ypos>panel.getHeight()-diameter)
{
ypos=panel.getHeight()-diameter;
dy=-dy;
}
panel.repaint();
}
public void setXpos(int xpos) {
this.xpos = xpos;
}
public void setYpos(int ypos) {
this.ypos = ypos;
}
public void setDiameter(int diameter) {
this.diameter = diameter;
}
public void setDx(int dx) {
this.dx = dx;
}
public void setDy(int dy) {
this.dy = dy;
}
public void setPanel(JPanel panel) {
this.panel = panel;
}
public void setColor(Color color) {
this.color = color;
}
public int getXpos() {
return (this.xpos);
}
public int getYpos() {
return (this.ypos);
}
public int getDiameter() {
return (this.diameter);
}
public int getDx() {
return (this.dx);
}
public int getDy() {
return (this.dy);
}
public JPanel getPanel() {
return (this.panel);
}
public Color getColor() {
return (this.color);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -