2013-04-08 107 views
-2

我是一名初學者,在Java 2d圖形和一個新手來stackoverflow,所以原諒我,如果做錯了什麼。我正在嘗試做一個涉及連鎖反應的迷你遊戲。例如,我想將10個球彈跳到具有單獨線程的JPanel上。當我點擊JPanel上的一個球時,我希望它可以擴大,其他所有擊中它的球也會擴大。我已經設法在單個Java文件中執行此操作,但我無法使其在單獨的文件中工作。任何輸入讚賞。謝謝。Java連鎖反應遊戲

這是我的單個文件BounceBall.java

import javax.swing.*; 
import java.awt.*; 
import java.awt.geom.*; 
import java.awt.event.*; 
import java.util.*; 

public class BounceBall extends JFrame { 

    private ShapePanel drawPanel; 
    private Vector<NewBall> Balls; 
    private JTextField message; 

    // set up interface 
    public BounceBall() { 
     super("MultiThreading"); 
     drawPanel = new ShapePanel(400, 345); 
     message = new JTextField(); 
     message.setEditable(false); 

     Balls = new Vector<NewBall>(); 
     add(drawPanel, BorderLayout.NORTH); 
     add(message, BorderLayout.SOUTH); 

     setSize(400, 400); 
     setVisible(true); 
     createBalls(); 
    } // end Ex20 constructor 

    public void createBalls() { 
     for (int i = 1; i <= 20; i++) { 
      NewBall nextBall = new NewBall(); 
      Balls.addElement(nextBall); 
      nextBall.start(); 
      message.setText("Number of Balls: " + Balls.size()); 
     } 
    } 

    public static void main(String args[]) { 

     BounceBall application = new BounceBall(); 

     application.addWindowListener(
      new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
    } 

    // Class NewBall is used to create a ball and move its position 
    // A thread overrides the run method to define how the ball behaves 
    // Each ball is one instance of this class 
    private class NewBall extends Thread { 

     private Ellipse2D.Double thisBall; 
     private boolean ballStarted, growBall, growEnd; 
     private int size = 30, speed = 70;    // characteristics 
     private int deltax, deltay;   // of the ball 
     Random r = new Random(); 
     int red = r.nextInt(255), green = r.nextInt(255), blue = r.nextInt(255); 

     public NewBall() { 
      // Create new ball with random size, speed, start point, 
      // and direction. "Speed" is actually the amount of sleep 
      // between moves. 
      ballStarted = true; 
      // size = 10 + (int)(Math.random() * 60); 
      // speed = 10 + (int)(Math.random() * 100); 
      int startx = (int) (Math.random() * 300); 
      int starty = (int) (Math.random() * 300); 
      deltax = -10 + (int) (Math.random() * 21); 
      deltay = -10 + (int) (Math.random() * 21); 
      if ((deltax == 0) && (deltay == 0)) { 
       deltax = 1; 
      } 
      thisBall = new Ellipse2D.Double(startx, starty, size, size); 
     } 

     public void draw(Graphics2D g2d) { 
      if (thisBall != null) { 
       g2d.setColor(new Color(red, green, blue, 80)); 
       g2d.fill(thisBall); 
      } 
     } 

     public void grow() { 
      growBall = true; 
     } 

     void removeBall() { 
      for (int i = 0; i < Balls.size(); i++) { 
       Balls.remove(i); 
      } 

      repaint(); 
     } 

     public double getPositionX() { 
      return thisBall.getX(); 
     } 

     public double getPositionY() { 
      return thisBall.getY(); 
     } 

     public int radius() { 
      return size * 2; 
     } 

     public void run() { 
      while (ballStarted && !growBall) // Keeps ball moving 
      { 
       try { 
        // To free up processor time 
        Thread.sleep(speed); 

       } catch (InterruptedException e) { 
        System.out.println("Woke up prematurely"); 
       } 

       // calculate new position and move ball 
       int oldx = (int) thisBall.getX(); 
       int oldy = (int) thisBall.getY(); 
       int newx = oldx + deltax; 
       if (newx + size > drawPanel.getWidth() || newx < 0) { 
        deltax = -deltax; 
       } 
       int newy = oldy + deltay; 
       if (newy + size > drawPanel.getHeight() || newy < 0) { 
        deltay = -deltay; 
       } 
       thisBall.setFrame(newx, newy, size, size); 

       while (growBall && size < 80) { 
        try { 
         // To free up processor time 
         Thread.sleep(speed); 

        } catch (InterruptedException e) { 
         System.out.println("Woke up prematurely"); 
        } 
        size++; 
        thisBall.setFrame(newx, newy, size, size); 

        for (int i = 0; i < Balls.size(); i++) { 
         if (thisBall.contains(Balls.get(i).thisBall.x, Balls.get(i).thisBall.y)) { 
          Balls.get(i).grow(); 
         } 
        } 
       } 
       drawPanel.repaint(); 
      } 
     } 
    } // end NewBall 

    // Define a class to be a panel on which the balls are drawn 
    private class ShapePanel extends JPanel { 

     private int prefwid, prefht; 

     public ShapePanel(int pwid, int pht) { 
      prefwid = pwid; 
      prefht = pht; 


      // add ball when mouse is clicked 
      addMouseListener(
       new MouseAdapter() { 
       public void mousePressed(MouseEvent e) { 
        stopBall(e); 

       } 
      }); 
     } 

     public void stopBall(MouseEvent e) { 
      for (int i = 0; i < Balls.size(); i++) { 
       if (Balls.get(i).thisBall.contains(e.getPoint())) { 
        Balls.get(i).grow(); 
       } 
      } 
     } 

     public Dimension getPreferredSize() { 
      return new Dimension(prefwid, prefht); 
     } 

     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g; 
      for (int i = 0; i < Balls.size(); i++) { 
       (Balls.elementAt(i)).draw(g2d); 
      } 
     } 
    } // end ShapePanel inner class 
} // end BounceBall 

這些都是3類即時試圖做的事:Ball.java

import java.awt.Color; 
import java.awt.Graphics2D; 
import java.awt.geom.Ellipse2D; 
import java.util.Random; 


public class Ball extends Thread { 
    public Ellipse2D.Double thisBall; 
    public int size=30,xPoz,yPoz; 
    Random r=new Random(); 
    int red=r.nextInt(255),green=r.nextInt(255),blue=r.nextInt(255); 
    int deltax,deltay; 
    public boolean ballStarted; 

    public Ball() 
    {xPoz=(int)Math.random(); 
    yPoz=(int)Math.random(); 
    ballStarted = true; 
    deltax=-10+(int)(Math.random()*21); 
    deltay=-10+(int)(Math.random()*21); 
    if ((deltax == 0) && (deltay == 0)) { deltax = 1; } 
    thisBall=new Ellipse2D.Double(xPoz,yPoz,size,size); 
    super.start(); 
    } 

    public void draw(Graphics2D g2d){ 
    if(thisBall!=null) 
    {g2d.setColor(new Color(red,green,blue,80)); 
    g2d.fill(thisBall); 
    } 
    } 

    public int PozX(){return xPoz;} 
    public int PozY(){return yPoz;} 
    public int radius(){return size*2;} 

    public void grow(){ 
    size++; 
    thisBall.setFrame(xPoz,yPoz,size,size); 
    } 

    public void move(){ 
     int oldx=xPoz; 
     int oldy=yPoz; 

     int newx=oldx+deltax; 
     int newy=oldy+deltay; 

     if(newx+size>800 || newx<0) 
      deltax=-deltax; 
     if(newy+size>600 || newy<0) 
      deltay=-deltay; 

     thisBall.setFrame(newx,newy,size,size); 
    } 

    public void run(){ 
     try { 

       Thread.sleep(100); 

      } 
      catch (InterruptedException e) 
      { System.out.println("Thread Halted");} 
     while(ballStarted) 
     {move();} 

    } 

} 

ShapePanel.java:

public class ShapePanel extends JPanel{ 
    private int prefwid, prefht,nrBalls; 
    public Vector<Ball> Balls=new Vector<Ball>();; 

     public ShapePanel (int pwid, int pht) 
     { 
     prefwid = pwid; 
     prefht = pht; 



     addMouseListener(

     new MouseAdapter() { 
      public void mousePressed(MouseEvent e) 
        { 


        } 
       } 
     ); 
     } 

    public void createBalls(){ 
    for(int i=1;i<=nrBalls;i++) 
    { Ball movingBall=new Ball(); 
     Balls.addElement(movingBall); 
     //movingBall.start(); 

    } 

    } 


     public Dimension getPreferredSize() 
     { 
     return new Dimension(prefwid, prefht); 
     } 

     public void setNrBalls(int n){this.nrBalls=n;} 

     public void paintComponent (Graphics g) 
     { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
      for (Ball ball : Balls) { 
       ball.draw(g2d); 
      } 
      g2d.dispose(); 
     } 
    } 

而且BallBlaster.java:

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.util.Vector; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 


public class BallBlaster extends JFrame 
{ 
    private ShapePanel drawPanel; 

    private JTextField message; 

    // set up interface 
    public BallBlaster() 
    { 
     super("MultiThreading"); 
     drawPanel = new ShapePanel(800, 600); 
     message = new JTextField("Number of balls : "); 
     message.setEditable(false); 


     add(drawPanel, BorderLayout.NORTH); 
     add(message, BorderLayout.SOUTH); 

     setSize(800, 600); 
     setVisible(true); 

     drawPanel.setNrBalls(20); 
     drawPanel.repaint(); 
    } 


    public static void main(String args[]) { 

     BoomShine application = new BoomShine(); 

     application.addWindowListener(
     new WindowAdapter() 
     { 
      public void windowClosing(WindowEvent e) 
      { 
       System.exit(0); 
      } 
     } 
    ); 
    } 

} 
+6

什麼是問題?這也是很多代碼。考慮發佈縮短的[SCCE](http://sscce.org/)版本 – Reimeus 2013-04-08 13:44:39

+0

僅發佈相關部分的代碼。 – 2013-04-08 13:48:14

+0

對不起,轉儲所有代碼無助於我們找到解決方案 – 2013-04-08 13:48:49

回答

1

將內部類轉換爲公共類時,應將對象引用移動到適當的類。例如,Balls數組應該轉到ShapePanel,Ball類應該有一個對ShapePanel的引用。應該將創建和移除球的方法移動到ShapePanel。這樣你給每個班一個響應,ShapePanel適用於球,球移動等...

+0

@ user2257714:在IDE中,使內部類爲靜態可能有助於識別受影響的引用;你仍然可以修復初始化順序問題。 – trashgod 2013-04-10 15:23:54