简体   繁体   中英

How can i remove, change color, and change type of the ball in java

Help me to find a code to Change the color, change the type of the ball, and removing ball from the canvas..i have try it many times but always error..

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

Class 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>();
}

I create 3 files in 1 package, BounceTugas, Ball,and BallComponent.. i hope anyone can solve this

I have modified your classes.

Here is the implements of empty method.

  • change ball size (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(); } 
  • change ball Type (BouceTugas.java)

     public void changeBallType(){ Ball.setCircle(!Ball.isCircle()); comp.repaint(); } 
  • remove ball (BounceTugas.java)

     public void removeBall() { if(comp.getBalls().size()>0){ comp.getBalls().remove(0); } comp.repaint(); } 
  • change ball color (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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM