2015-10-07 53 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以了解更多详细信息