2014-03-12 71 views
0

幫我找到一個代碼來改變顏色,改變球的類型,並從畫布上移除球..我嘗試了很多次,但總是錯誤..我如何刪除,改變顏色,並在java中改變球的類型

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

我創建3個文件1包,BounceTugas,Ball和BallComponent .. 我希望任何人都可以解決這個問題

+0

你什麼錯誤?從查看代碼並不明顯。另外,請嘗試根據球類中的某個字段設置球的顏色,並添加一個吸氣/吸氣器? – turbo

回答

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

謝謝!它的幫助 –

+0

但是,如果它有可能改變橢圓的大小,該方法使用什麼來製作一個矩形對象? –

+0

我已根據您的要求更新了我的答案。請再試一次,並讓我知道是否有任何問題。先前的方法更改爲'changeBallSize'再多一個按鈕來更改大小。現在我添加了一個新的方法'changeBallType'來在圓形和矩形之間進行交換。 – Braj