帮我找一个代码来改变颜色,改变球的类型,并从画布中移除球..我已经多次尝试但总是错误..
package bouncetugas;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BounceTugas {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame = new BounceFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
/** A runnable animate a bouncing ball*/
class BallRunnable implements Runnable
{
public BallRunnable(Ball aBall, Component aComponent)
{
ball = aBall;
component = aComponent;
}
public void run()
{
try {
for (int i = 1; i <= STEPS; i++)
{
ball.move(component.getBounds());
component.repaint();
Thread.sleep(DELAY);
}
}
catch (InterruptedException e)
{
}
}
private Ball ball;
private Component component;
public static final int STEPS = 1000;
public static final int DELAY = 5;
}
//The frame with panel and buttons.
class BounceFrame extends JFrame
{
/*Constructs the frame with the component for showing
the bouncing ball and Start and Close buttons*/
public BounceFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setTitle("Bounce Latihan");
comp = new BallComponent();
add(comp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
addButton(buttonPanel, "Add Ball", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
addBall();
}
});
addButton(buttonPanel, "Remove Ball", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
removeBall();
}
});
addButton(buttonPanel, "Change Color", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
changeColor();
}
});
addButton(buttonPanel, "Change Type", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
changeBall();
}
});
addButton(buttonPanel, "Start", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
startBall();
}
});
addButton(buttonPanel, "Pause", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
pauseBall();
}
});
addButton(buttonPanel, "Close", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
}
//Adds a button to a container.
public void addButton(Container c, String title, ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
//Adds a bouncing ball to the canvas and starts a thread to make it bounce
public void addBall()
{
Ball b = new Ball();
comp.add(b);
Runnable r = new BallRunnable(b, comp);
Thread t = new Thread(r);
t.start();
}
public void removeBall()
{
//Ball.remove(Ball.size() - 1);
}
public void changeColor()
{
if (g2.getColor() = Color.RED){
.g2.setColor(Color.GREEN);
}
else{
g2.setColor(Color.RED);
}
this.repaint();
}
public void startBall()
{
Thread r = new Thread();
r.start();
synchronized(r) {
r.notify();
}
}
public void pauseBall() {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
private BallComponent comp;
public static final int DEFAULT_WIDTH = 650;
public static final int DEFAULT_HEIGHT = 450;
public static final int STEPS = 1000;
public static final int DELAY = 3;
}
Class Ball
import java.awt.geom.*;
class Ball {
//Moves the ball to the next position,
//reversing direction if it hits one of the edges
public void move(Rectangle2D bounds)
{
x += dx;
y += dy;
if (x < bounds.getMinX())
{
x = bounds.getMinX();
dx = -dx;
}
if (x + XSIZE >= bounds.getMaxX())
{
x = bounds.getMaxX() - XSIZE;
dx = -dx;
}
if (y < bounds.getMinY())
{
y = bounds.getMinY();
dy = -dy;
}
if (y + YSIZE >= bounds.getMaxY())
{
y = bounds.getMaxY() - YSIZE;
dy = -dy;
}
}
//Gets the shape of the ball at its current position.
public Ellipse2D getShape()
{
return new Ellipse2D.Double(x,y, XSIZE, YSIZE);
}
private static final int XSIZE = 15;
private static final int YSIZE = 15;
private double x = 0;
private double y = 0;
private double dx = 1;
private double dy = 1;
}
类BallComponent
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author Fahmi
*/
//The component that draws the balls.
class BallComponent extends JPanel {
//Add a ball to the component
public void add(Ball b)
{
balls.add(b);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g); //erase back
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls)
{
g2.setColor(Color.RED);
g2.fill(b.getShape());
}
}
private ArrayList<Ball> balls = new ArrayList<Ball>();
}
我在1个包中创建3个文件,BounceTugas,Ball和BallComponent .. 我希望任何人都能解决这个问题
答案 0 :(得分:1)
我修改了你的课程。
这是空方法的实现。
更改球尺寸(BounceTugas.java)
public void changeBallSize(){
if(Ball.getXSIZE()==15){
Ball.setXSIZE(45);
}else{
Ball.setXSIZE(15);
}
if(Ball.getYSIZE()==15){
Ball.setYSIZE(45);
}else{
Ball.setYSIZE(15);
}
Ball.setCircle(!Ball.isCircle());
comp.repaint();
}
更改球类型(BouceTugas.java)
public void changeBallType(){
Ball.setCircle(!Ball.isCircle());
comp.repaint();
}
删除球(BounceTugas.java)
public void removeBall() {
if(comp.getBalls().size()>0){
comp.getBalls().remove(0);
}
comp.repaint();
}
更改球颜色(BounceTugas.java)
public void changeColor() {
if (comp.getColor() == Color.RED) {
comp.setColor(Color.GREEN);
} else {
comp.setColor(Color.RED);
}
comp.repaint();
}
BallComponent.java
public void paintComponent(Graphics g) {
super.paintComponent(g); // erase back
Graphics2D g2 = (Graphics2D) g;
for (Ball b : balls) {
g2.setColor(color);
g2.fill(b.getShape());
}
}
private ArrayList<Ball> balls = new ArrayList<Ball>();
private Color color = Color.RED;
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public ArrayList<Ball> getBalls() {
return balls;
}
Ball.java
public Shape getShape() {
if(circle){
return new Ellipse2D.Double(x, y, XSIZE, YSIZE);
}else{
return new Rectangle((int)x,(int)y,XSIZE,YSIZE);
}
}
private static boolean circle=true;
private static int XSIZE = 15;
private static int YSIZE = 15;
public static int getXsize() {
return XSIZE;
}
public static int getYsize() {
return YSIZE;
}
public static int getXSIZE() {
return XSIZE;
}
public static void setXSIZE(int xSIZE) {
XSIZE = xSIZE;
}
public static int getYSIZE() {
return YSIZE;
}
public static void setYSIZE(int ySIZE) {
YSIZE = ySIZE;
}
public static boolean isCircle() {
return circle;
}
public static void setCircle(boolean circle) {
Ball.circle = circle;
}