JAVA程序弹跳球,用布尔值改变大小(跳动).如何做到这一点



我浏览了所有的互联网和我的课本,但我似乎无法解决我的问题。

在我的程序"弹跳球"(从老师那里得到代码)中,我需要将球的大小从小变大并反转。我知道我需要一个布尔值来做这件事,也许还需要一个if语句。这就是我在Ball课堂上关于尺寸变化的内容:

private布尔值changeSize=true;

int maxSize = 10;
int minSize = 1;
public void changeSize(boolean size ){
    if(size == maxSize ){
        return minSize;
    }
    else return maxSize;
}

public void changeBallSize(int d, int f){
    diameter = d*f;

这是Ball:类的全部代码

类球{

static int        defaultDiameter  = 10;
static Color      defaultColor     = Color.yellow;
static Rectangle  defaultBox       = new Rectangle(0,0,100,100);
// Position
private int x, y;
// Speen and angel 
private int dx, dy;
// Size 
private int diameter;
// Color 
private Color color;
// Bouncing area
private Rectangle box;
// New Ball
public Ball( int x0, int y0, int dx0, int dy0 ) {
    x = x0;
    y = y0;
    dx = dx0;
    dy = dy0;
    color = defaultColor;
    diameter = defaultDiameter;
}
// New color
public void setColor( Color c ) {
    color = c;
}

public void setBoundingBox( Rectangle r ) {
    box = r;
}
// ball  
public void paint( Graphics g ) {
    // Byt till bollens färg 
    g.setColor( color );

    g.fillOval( x, y, diameter, diameter );
}

void constrain() {
    // Ge absoluta koordinater för det rektangulära området
    int x0 = box.x;
    int y0 = box.y;
    int x1 = x0 + box.width - diameter;
    int y1 = y0 + box.height - diameter;
    // Setting speed and angels
    if (x < x0)
        dx = Math.abs(dx);
    if (x > x1)
        dx = -Math.abs(dx);
    if (y < y0)
        dy = Math.abs(dy);
    if (y > y1)
        dy = -Math.abs(dy);
}
// movingt the ball
    x = x + dx;
    y = y + dy;
    constrain();
}

}

我是java的新手!谢谢你的帮助!

在Ball类中添加以下内容:

private int changeFlag=-1;

在constrain()函数中,就在最后一行之前,在移动球之后:

if(diameter==maxSize) {
   changeFlag=-1;
}
else if (diameter==minSize) {
   changeFlag=1;
}
diameter=diameter+changeFlag;

将此代码添加到您的Ball class

private minSize = 1;
private maxSize = 10;
public void setDiameter(int newDiameter) {
  this.diameter = newDiameter;
}
public int getMinSize() {
  return minSize; 
}
public int getMinSize() {
  return maxSize; 
}

当你使用球时使用这个

Ball ball = new Ball(1,1,1,1);
int newDiameter = 10;
if(newDiameter == ball.getMinSize()) {
    ball.setDiameter(ball.getMaxSize());
} 
id(newDiameter == ball.getMaxSize()) {
    ball.setDiameter (ball.getMinSize());
}

我已经编辑过了,这就是你的意思吗?

主要类别:

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Main extends JFrame {
    public static void main(String[] args) {
        new Main();
    }
    public Main() {
        // configure JFrame
        setSize(640, 360);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // create ball
        Ball ball = new Ball(this.getWidth()/2, this.getHeight()/2, 50, 100);
        add(ball);
        Thread t = new Thread(ball);
        t.start();
    }
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
    }
}

球类:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JComponent;

public class Ball extends JComponent implements Runnable {
private int x, y, minDiameter, maxDiameter, currentDiameter, growRate = -1;
private Color color = Color.BLUE;
public Ball(int x, int y, int minDiameter, int maxDiameter) {
    this.x = x;
    this.y = y;
    this.minDiameter = minDiameter;
    this.maxDiameter = maxDiameter;
    this.currentDiameter = minDiameter;
    setVisible(true);
}
@Override
public void run() {
    while (true) {
        // coerce max and min size          
        if (this.currentDiameter + growRate > maxDiameter) {
            this.currentDiameter = maxDiameter;     
            this.growRate = -1;
        }
        if (this.currentDiameter + growRate < minDiameter) {
            this.currentDiameter = minDiameter; 
            this.growRate = 1;
        }
        this.currentDiameter += this.growRate;
        repaint();
        try {
            Thread.sleep(10);
        }
        catch(Exception e) {
            System.out.println(e.toString());
        }
    }
}
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
    g2.setColor(this.color);
    g2.fillOval(this.x, this.y, this.currentDiameter, this.currentDiameter);
}

}

相关内容

最新更新