2015-10-07 102 views
0

Pong.Java的Java AWT和Swing的

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JFrame; 

public class Pong { 
    private static final int WIDTH = 900, HEIGHT = 700; 
    JFrame win = new JFrame(); 
    Paddle paddleOne = new Paddle(1); 
    Paddle paddleTwo = new Paddle(2); 
    Graphics g; 

    Pong(){ 
     init(); 
    } 

    void init(){ 
     win.setTitle("PONG"); 
     win.setSize(WIDTH, HEIGHT); 
     win.addKeyListener(keyListener); 
     win.setLocationRelativeTo(null); 
     win.getContentPane().setBackground(Color.black); 
     win.setVisible(true); 
     win.getContentPane().validate(); 
     win.repaint(); 
    } 


    public void paintComponent(Graphics g){ 
     g.setColor(Color.white); 
     g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH()); 

     System.out.println("drawn"); 
    } 

    KeyListener keyListener = new KeyListener() { 
     public void keyPressed(KeyEvent e) { 
      int key = e.getKeyCode(); 

      switch(key){ 
      case 87: 
       System.out.println("Player 1 Up"); 
       break; 
      case 83: 
       System.out.println("Player 1 Down"); 
       break; 
      case 38: 
       System.out.println("Player 2 Up"); 
       break; 
      case 40: 
       System.out.println("Player 2 Down"); 
       break; 
      } 

      win.getContentPane().validate(); 
      win.repaint(); 

     } 

     public void keyReleased(KeyEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void keyTyped(KeyEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

    }; 

    public static void main(String[] args) { 
     Pong p = new Pong(); 
    } 
} 

Paddle.Java

public class Paddle{ 
private int WIDTH = 50, HEIGHT = 150, X, Y; 


int playerNum; 

Paddle(int playerNum){ 
    if(playerNum == 1){ 
     X = 10; 
     Y = 10; 
    }else if (playerNum == 2){ 
     X = 500; 
     Y = 10; 
    } 
} 

public void setX(int x){ 
    X = x; 
} 

public void setY(int y){ 
    Y = y; 
} 

public int getWIDTH() { 
    return WIDTH; 
} 

public int getHEIGHT() { 
    return HEIGHT; 
} 

public int getX() { 
    return X; 
} 

public int getY() { 
    return Y; 
} 

} 

我是比較新的Java編程,或者更具體地說AWT &搖擺,我的問題是什麼,爲什麼ISN我的矩形圖畫?任何幫助表示讚賞。非常感謝!

+1

'public void paintComponent(Graphics g){'嘗試添加'@ Override'表示法.. –

+0

Pong類型的paintComponent(Graphics)方法必須重寫或實現超類型方法是它拋給我的東西。 – groenewoldr

+1

那麼'Pong'從什麼都沒有延伸到「神奇地」是可塗漆的?看看[在AWT和Swing中繪畫](http://www.oracle.com/technetwork/java/painting-140037.html)和[執行自定義繪畫](http://docs.oracle.com/javase/tutorial/uiswing/painting /)瞭解更多關於繪畫是如何在Swing中工作的細節 – MadProgrammer

回答

1

您需要重寫paintComponents來繪製。 這裏是你的Pong.java

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JFrame; 
import javax.swing.SwingUtilities; 

public class Pong { 
    private static final int WIDTH = 900, HEIGHT = 700; 
    JFrame win = new JFrame(); 
    Paddle paddleOne = new Paddle(1); 
    Paddle paddleTwo = new Paddle(2); 
    Graphics g; 

    Pong() { 
     init(); 
    } 

    void init() { 
     win.setTitle("PONG"); 
     win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     win.setSize(WIDTH, HEIGHT); 
     win.addKeyListener(keyListener); 
     win.setLocationRelativeTo(null); 
     win.add(new Panel(paddleOne)); 
     win.getContentPane().setBackground(Color.black); 
     win.setVisible(true); 
     win.getContentPane().validate(); 
     win.repaint(); 
    } 

    KeyListener keyListener = new KeyListener() { 
     public void keyPressed(KeyEvent e) { 
      int key = e.getKeyCode(); 

      switch (key) { 
      case 87: 
       System.out.println("Player 1 Up"); 
       break; 
      case 83: 
       System.out.println("Player 1 Down"); 
       break; 
      case 38: 
       System.out.println("Player 2 Up"); 
       break; 
      case 40: 
       System.out.println("Player 2 Down"); 
       break; 
      } 

      win.getContentPane().validate(); 
      win.repaint(); 

     } 

     public void keyReleased(KeyEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

     public void keyTyped(KeyEvent arg0) { 
      // TODO Auto-generated method stub 

     } 

    }; 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new Pong(); 

      } 
     }); 
    } 
} 

這裏Panel.java在paintComponent重寫。

import java.awt.Color; 
import java.awt.Graphics; 
import javax.swing.JPanel; 

public class Panel extends JPanel{ 
    private Paddle paddleOne; 
    public Panel(Paddle pdl) { 
     paddleOne = pdl; 
    } 

    @Override 
    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.setColor(Color.white); 
     g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH()); 

     //System.out.println("drawn"); //Should not put something here which may overhead. 
    } 

} 
+0

不,這不是(工作) – MadProgrammer

+0

你已經打破了塗料鏈的要求。你真的應該從EDT的背景下開始UI,但是我會質疑從面板的一個實例方法中創建一個框架,但這是一個設計問題,我懷疑關鍵事件會在可靠時觸發在它裏面捅破洞。雖然提供可運行的解決方案非常好,但您應該將馬引向水域,而不是將它推到水下;) – MadProgrammer

+0

@MadProgrammer,可以嗎? – ashiquzzaman33

1

爲了某事在Swing內畫,它首先必須從東西擺延長知道是怎麼畫,這通常意味着JComponent(或者更通常爲JPanel)。

然後你就可以覆蓋的油漆方法,它是由繪畫子系統調用,在這種情況下,一個,它通常優選覆蓋paintComponent,但不要忘記調用super.paintComponent你做任何自定義塗裝前,或者你正在爲自己設置一些奇怪的,通常無法預測的繪畫問題。

有關更多詳細信息,請參見Painting in AWT and SwingPerforming Custom Painting

import java.awt.Color; 
import java.awt.EventQueue; 
import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Pongo { 

    public static void main(String[] args) { 
     new Pongo(); 
    } 

    public Pongo() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
        ex.printStackTrace(); 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new PongPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class PongPane extends JPanel { 

     private static final int WIDTH = 900, HEIGHT = 700; 
     Paddle paddleOne = new Paddle(1); 
     Paddle paddleTwo = new Paddle(2); 

     public PongPane() { 
      setBackground(Color.BLACK); 
     } 

     @Override 
     public void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      g.setColor(Color.white); 
      g.fillRect(paddleOne.getX(), paddleOne.getY(), paddleOne.getHEIGHT(), paddleOne.getWIDTH()); 

      //System.out.println("drawn"); //Should not put something here which may overhead. 
     } 

    } 

    public class Paddle { 

     private int WIDTH = 50, HEIGHT = 150, X, Y; 

     int playerNum; 

     Paddle(int playerNum) { 
      if (playerNum == 1) { 
       X = 10; 
       Y = 10; 
      } else if (playerNum == 2) { 
       X = 500; 
       Y = 10; 
      } 
     } 

     public void setX(int x) { 
      X = x; 
     } 

     public void setY(int y) { 
      Y = y; 
     } 

     public int getWIDTH() { 
      return WIDTH; 
     } 

     public int getHEIGHT() { 
      return HEIGHT; 
     } 

     public int getX() { 
      return X; 
     } 

     public int getY() { 
      return Y; 
     } 
    } 
} 

我也將阻止這種情況下使用的鍵綁定API的內部和內部建議使用KeyListener,它不會從相同的焦點遭受相關的問題KeyListener一樣。請參閱How to Use Key Bindings以瞭解更多詳細信息