gpt4 book ai didi

java - 我如何在java中删除球、改变颜色和改变球的类型

转载 作者:行者123 更新时间:2023-12-01 13:17:19 25 4
gpt4 key购买 nike

帮我找到一个代码来更改颜色,更改球的类型,以及从 Canvas 上删除球。我已经尝试了很多次,但总是出错。

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;
}

类球

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。我希望任何人都可以解决这个问题

最佳答案

我修改了你的类(class)。

这里是空方法的实现。

  • 更改球大小 (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;
    }

关于java - 我如何在java中删除球、改变颜色和改变球的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22361806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com