2014-05-06 30 views
0

我正在使用Java awt,Swing和線程編寫高爾夫板在Java中的實現。我的程序有三個文本區域可同時選擇插槽數量,球數量和球滴數量,兩個按鈕之一用於顯示,另一個用於啓動程序。我試圖讓它工作,就像我可以選擇球的數量,然後點擊開始和球自動落下煙囪。目前,我的程序能夠丟下一個球並且運行良好,但是我不知道如何實現那個能夠丟下多個球。任何建議或幫助表示讚賞,謝謝。這是Main.Class在java中實現高爾頓板

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.util.Random; 
import javax.swing.*; 

public class Main extends JFrame { 
    private String num_slots; 
    private String num_balls; 
    private String ball_free; 
    private JButton Display; 
    private JButton Start; 
    private JPanel textpanel; 
    private JPanel mainpanel; 
    private JPanel graphpanel; 

    public Main() { 
     textpanel = new JPanel(); 
     textpanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 20)); 
     textpanel.add(new JLabel("Number of Slots")); 
     final JTextField text1 = new JTextField(10); 
     textpanel.add(text1); 
     textpanel.add(new JLabel("Number of Balls")); 
     final JTextField text2 = new JTextField(10); 
     textpanel.add(text2); 
     textpanel.add(new JLabel("How many balls can be freed")); 
     final JTextField text3 = new JTextField(10); 
     textpanel.add(text3); 
     Display = new JButton("Display"); 
     textpanel.add(Display); 
     Start = new JButton("Start"); 
     textpanel.add(Start); 
     // Create panel p2 to hold a text field and p1 
     mainpanel = new JPanel(new BorderLayout()); 
     mainpanel.add(textpanel, BorderLayout.NORTH); 
     /* 
     * graphpanel = new JPanel(); graphpanel.setLayout(new 
     * BoxLayout(graphpanel, BoxLayout.Y_AXIS)); 
     */ 
     add(mainpanel, BorderLayout.CENTER); 
     Display.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (e.getSource() == Display) { 
       num_slots = text1.getText(); 
       int slots = Integer.parseInt(num_slots); 
       num_balls = text2.getText(); 
       int balls = Integer.parseInt(num_balls); 
       MainPanel pa = new MainPanel(slots, balls); 
       mainpanel.add(pa); 
       mainpanel.revalidate(); 
      } 
     } 
     }); 
     Start.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      if (e.getSource() == Start) { 
       num_slots = text1.getText(); 
       int slots = Integer.parseInt(num_slots); 
       num_balls = text2.getText(); 
       int balls = Integer.parseInt(num_balls); 
       MainPanel pa = new MainPanel(slots, balls); 
       mainpanel.add(pa, BorderLayout.CENTER); 
       pa.start(); 
       mainpanel.revalidate(); 
       mainpanel.repaint(); 
      } 
     } 
     }); 
    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     Main frame = new Main(); 
     frame.setTitle("The Galton board"); 
     frame.setSize(1000, 800); 
     frame.setLocationRelativeTo(null); // Center the frame 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 
     frame.setAutoRequestFocus(true); 
    } 
} 

主面板類包含煙囪和球

import java.awt.Color; 
import java.awt.Graphics; 
import java.util.Random; 

import javax.swing.JPanel; 

class MainPanel extends JPanel implements Runnable { 
    private int num; 
    private int number_ball; 
    public static int start_y = 100; 
    private float ball_x = 385; 
    private float ball_y = 50; 
    private float radius = 15; 
    private static int panel_x = 300; 
    private static int panel_y = 100; 
    private int diameter = 20; 
    private int last_x = 0; 
    private final static Random generator = new Random(); 
    ArrayList<Balls> list_ball = new ArrayList<Balls>(); 
    private int m_interval = 100; 
    private Timer m_timer; 
    public MainPanel() { 
    } 

    public MainPanel(int number) { 
     num = number; 
    } 

    public MainPanel(int number, int ball) { 
     num = number; 
     number_ball = ball; 
     for (int i = 1; i <= number_ball; i++) 
     { 
      list_ball.add(new Balls()); 

     } 
     m_timer = new Timer(m_interval, new TimerAction()); 
    } 

    public int getPanel_y() { 
     return panel_y; 
    } 
    public void start() 
    { 
     m_timer.setInitialDelay(250); 
     m_timer.start(); 

    } 
    @Override 
    protected void paintComponent(Graphics g) { 
     int start_y = 100; 
     panel_x = 300; 
     panel_y = 100; 
     diameter = 20; 
     last_x = 0; 
     super.paintComponent(g); 
     if (num % 2 == 0) { 
     for (int i = 1; i <= num; i++) { 
      if ((i % 2) != 0) { 
       for (int k = 1; k <= num; k++) { 
        g.setColor(Color.BLUE); 
        g.fillOval(panel_x, panel_y, diameter, diameter); 
        panel_x = panel_x + 40; 
       } 
      } else if ((i % 2) == 0) { 
       for (int k = 1; k <= num + 1; k++) { 
        g.setColor(Color.BLUE); 
        g.fillOval(panel_x - 20, panel_y, diameter, diameter); 
        panel_x = panel_x + 40; 
       } 
      } 
      panel_y = panel_y + 40; 
      panel_x = 300; 
     } 
     } else if (num % 2 != 0) { 
     for (int i = 1; i <= num; i++) { 
      if ((i % 2) != 0) { 
       for (int k = 1; k <= num; k++) { 
        g.setColor(Color.BLUE); 
        g.fillOval(panel_x, panel_y, diameter, diameter); 
        panel_x = panel_x + 40; 
       } 
      } else if ((i % 2) == 0) { 
       for (int k = 1; k <= num + 1; k++) { 
        g.setColor(Color.BLUE); 
        g.fillOval(panel_x - 20, panel_y, diameter, diameter); 
        panel_x = panel_x + 40; 
       } 
      } 
      panel_y = panel_y + 40; 
      panel_x = 300; 
     } 
     } 
     for (int n = 40; n < panel_y - 40; n = n + 40) { 
     if (num % 2 == 0) { 
      g.drawLine(panel_x - 50 + n, panel_y - 10, panel_x - 50 + n, 
        panel_y + 80); 
      g.drawLine(panel_x, panel_y + 80, panel_x - 50 + n, panel_y + 80); 
      last_x = panel_x - 50 + n; 
     } else if (num % 2 != 0) { 
      g.drawLine(panel_x - 30 + n, panel_y - 10, panel_x - 30 + n, 
        panel_y + 80); 
      g.drawLine(panel_x, panel_y + 80, panel_x - 30 + n, panel_y + 80); 
      last_x = panel_x - 30 + n; 
     } 
     } 
     for (int i = 0; i< list_ball.size(); i++) 
     { 
     list_ball.get(i).draw(g); 

     } 
    } 
    class TimerAction implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
      for (int i = 0; i< list_ball.size(); i++) 
      { 

        list_ball.get(i).move(); 
        //return; 

        //m_timer.stop(); 
        repaint(); 


      } 
} 

球類

import java.awt.geom.Ellipse2D; 
    import java.util.Random; 
    import java.awt.*; 

    public class Balls { 
    private Ellipse2D.Double thisBall; 
    private int Ball_x; 
    private int Ball_y; 
    public int radius; 
    public int start_y; 
    private final static Random generator = new Random(); 
    Mainpanel pa = new Mainpanel(); 
    public Balls() 
    { 
    start_y = 100; 
    Ball_x = 385; 
    Ball_y = 50; 
    radius = 15; 

    } 
    public void draw(Graphics g) 
    { 
    g.setColor(Color.RED); 
    g.fillOval(Ball_x, Ball_y, radius, radius); 
    } 
    public void move() 
    { 
    if (Ball_y < pa.getPanel_y() + 65) 
      { 
       int direction = generator.nextInt(2); 
       Ball_y = Ball_y + 5; 
       if (Ball_y == start_y - 10 && start_y < pa.getPanel_y()) 
       { 
        if (direction == 0) 
        { 
        Ball_x = Ball_x - 20; 
        } 
       else Ball_x = Ball_x + 20; 
       start_y = start_y + 40; 
      } 

     System.out.println(Ball_y); 
     System.out.println(pa.getPanel_y());  
    } 
    // Ball_x = Ball_x + 5; 


} 

}

回答

3

創建新的邏輯類,而不是一個GUI類,對於你的Ball來說,它不會擴展一個JPanel或任何Swing組件,而是一個擁有邏輯背後的邏輯e Ball以及接受Graphics或Graphics2D對象的渲染方法。然後給你的繪圖JPanel類一個這些Ball對象的ArrayList,將它們移動到你的遊戲循環中 - 注意我更喜歡使用Swing Timer而不是後臺線程,然後在JPanel的paintComponent方法中遍歷BallList的ArrayList,通過調用其渲染方法來繪製每個Ball。另外:所有的類名應該以大寫字母開頭,除常量以外的所有標識符都應該使用駱駝大小寫,因此您的mainpanel類應該被命名爲MainPanel。我編輯了你的代碼,對代碼格式進行了優化,並且爲你做了這個改變。

另外數字2:您當前的代碼具有paintComponent內部的代碼邏輯。不要這樣做,因爲這會攪亂你。您無法完全控制何時或即使調用paintComponent。

+0

非常感謝,它似乎工作,但現在球同時下降,你知道如何讓每個球落下相互?我在這裏更新了我的當前代碼。再次感謝 – Daniel

+0

@丹尼爾:這是一個新問題,可能應該單獨提出。 –