英文:
Is there a way to make Java graphic shapes interact with each other?
问题
private void moveBall() {
if (balldown == true) {
y++;
}
if (balldown == false) {
y--;
}
if (y == getHeight() - border) {
balldown = false;
bounce++;
}
if (y == 0) {
balldown = true;
bounce++;
}
if (ballright == true) {
x++;
}
if (ballright == false) {
x--;
}
if (x == getWidth()) {
ballright = false;
bounce++;
}
if (x == 0) {
ballright = true;
bounce++;
}
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1080, 760);
g.setColor(Color.WHITE);
g.fillOval(x, y, 30, 30);
g.setColor(Color.WHITE);
g.fillRect(0, a, 30, 200);
g.setColor(Color.WHITE);
g.fillRect(980, b, 30, 200);
g.fillRect(520, 0, 10, 760);
}
Note: The provided code appears to be related to a Java program that involves moving a ball within a graphical context and bouncing it off the window edges and rectangles. If you have any questions or need further assistance with the code, feel free to ask.
英文:
private void moveBall(){
if (balldown==true){
y++;
}
if (balldown==false){
y--;
}
if(y==getHeight()-border){
balldown=false;
bounce++;
}
if(y==0){
balldown=true;
bounce++;
}
if (ballright==true){
x++;
}
if (ballright==false){
x--;
}
if(x==getWidth()){
ballright=false;
bounce++;
}
if(x==0){
ballright=true;
bounce++;
}
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1080, 760);
g.setColor(Color.WHITE);
g.fillOval(x, y, 30, 30);
g.setColor(Color.WHITE);
g.fillRect(0, a, 30, 200);
g.setColor(Color.WHITE);
g.fillRect(980, b, 30, 200);
g.fillRect(520, 0, 10, 760);
Im a java beginner
Is there a way to make my ball bounce off the graphic rectangles the same way it bounces off the window edges?If I add the width or height measures to an if statements it just treats it as the whole screens so I am at a loss on how to solve this issue.Is there some sort of way to tread only the rectangle part as a screen edge instead of the whole side?
专注分享java语言的经验与见解,让所有开发者获益!
评论