2015-11-08 99 views
2

3類之間comunicate我做了一個簡單的orogram,使用擺動使球在屏幕上,然後我不得不按鈕讓它動:leftrightupdownchange velocity有的標貼顯示座標和速度。如何在Java

現在,我想使它與鍵盤上的按鍵一起工作,但爲了讓我想再做一次課。

這裏有3類:

Punto.java(顯示在屏幕上的點):

public class Punto { 
    int x , y, v; 

    public Punto(int a, int b) { 
     this.x=a; 
     this.y=b; 
     this.v=1; 
    } 

    public void moveLeft(){ 
     this.x-=v; 
    } 

    public void moveRight(){ 
     this.x+=v; 
    } 

    public void moveUp(){ 
     this.y-=v; 
    } 

    public void moveDown(){ 
     this.y+=v; 
    } 

    public void cambiaVelocita(){ 
     switch(v){ 
      case 1: v = 2; 
        break; 
      case 2: v = 4; 
        break; 
      case 4: v = 8; 
        break; 
      case 8: v = 1; 
        break; 
     } 
    } 

    public int getX(){ 
     return this.x; 
    } 

    public int getY(){ 
     return this.y; 
    } 

    public int getV(){ 
     return this.v; 
    } 
} 

這裏是用於面板的代碼:

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

public class MyPanel extends JPanel implements ActionListener { 

    private Punto p; 

    ActionEvent Event; 

    String vkLeft = "VK_LEFT"; 
    String vkRight = "VK_RIGHT"; 
    String vkup = "VK_UP"; 
    String vkdown = "VK_DOWN"; 

    private JLabel l; 
    private JButton b; 
    private JButton bb; 
    private JButton bbb; 
    private JButton bbbb; 
    private JButton bbbbb; 

    public MyPanel(){ 
     p = new Punto(50,50); 
     l = new JLabel("Coordinate"); 

     setKeyBindings(); 

     b = new JButton("Left"); 
     b.addActionListener(this); 
     bb = new JButton("Right"); 
     bb.addActionListener(this); 
     bbb = new JButton("Up"); 
     bbb.addActionListener(this); 
     bbbb = new JButton("Down"); 
     bbbb.addActionListener(this); 
     bbbbb = new JButton("Velocita"); 
     bbbbb.addActionListener(this); 

     this.add(l); 
     this.add(b); 
     this.add(bb); 
     this.add(bbb); 
     this.add(bbbb); 
     this.add(bbbbb); 

    } 

    private void setKeyBindings() { 
     ActionMap actionMap = getActionMap(); 
     int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; 
     InputMap inputMap = getInputMap(condition); 


     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), vkLeft); 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), vkRight); 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), vkup); 
     inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), vkdown); 

     actionMap.put("VK_LEFT", new KeyAction(vkLeft)); 
     actionMap.put("VK_RIGHT", new KeyAction(vkRight)); 
     actionMap.put("VK_UP", new KeyAction(vkup)); 
     actionMap.put("VK_DOWN", new KeyAction(vkdown)); 

    } 



    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     this.l.setText(" Coordinate x:" + Integer.toString(p.getX()) + " y:" + Integer.toString(p.getY())+ " Velocita:" + Integer.toString(p.getV())) ; 
     g.setColor(Color.red); 
     g.fillOval(p.getX(),p.getY(),10,10); 
//  g.drawString(Integer.toString(p.getX()) + " " + Integer.toString(p.getY()),50,60); 
    } 





    public void actionPerformed(ActionEvent e){ 
     Object action = e.getSource(); 
     if(action == b) 
      p.moveLeft(); 
     if(action == bb) 
      p.moveRight(); 
     if(action == bbb) 
      p.moveUp(); 
     if(action == bbbb) 
      p.moveDown(); 
     if(action == bbbbb) 
      p.cambiaVelocita(); 
     this.repaint(); 

    } 

} 

而這是鍵盤排序器使用的keyAction

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

public class KeyAction extends AbstractAction { 
     public KeyAction(String actionCommand) { 
     putValue(ACTION_COMMAND_KEY, actionCommand); 
     } 

     @Override 
     public void actionPerformed(ActionEvent actionEvt) { 
     if(actionEvt.getActionCommand().equals("VK_LEFT")) 
      System.out.println("Left"); 
     if(actionEvt.getActionCommand().equals("VK_RIGHT")) 
      System.out.println("right"); 
     if(actionEvt.getActionCommand().equals("VK_UP")) 
      System.out.println("up"); 
     if(actionEvt.getActionCommand().equals("VK_DOWN")) 
      System.out.println("down"); 
     } 
    } 

現在我的問題是:如何從KeyAction類中編輯P的變量塊,面板類中的Punto Decleared實例,以便我可以使用鍵移動該點?

+0

通過實例及其成員。除非成員是靜態的,否則你直接通過課程訪問他們。 – Stultuske

+0

@Stultuske所以這意味着我應該使用'MyPanel.p.setX(x);'那樣的東西 – BRHSM

+0

或將該類傳遞給構造函數... –

回答

1

顯然,您的KeyAction需要對其周圍有一些知識。試想一下:

public class KeyAction extends AbstractAction { 
    Punto p; 
    Component c; 
    public KeyAction(Punto p, Component c, String actionCommand) { 
     this.p = p; 
     this.c = c; 
     putValue(ACTION_COMMAND_KEY, actionCommand); 
    } 

    @Override 
    public void actionPerformed(ActionEvent actionEvt) { 
     if(actionEvt.getActionCommand().equals("VK_LEFT")) 
      p.moveLeft(); 
     if(actionEvt.getActionCommand().equals("VK_RIGHT")) 
      p.moveRight(); 
     if(actionEvt.getActionCommand().equals("VK_UP")) 
      p.moveUp(); 
     if(actionEvt.getActionCommand().equals("VK_DOWN")) 
      p.moveDown(); 

     c.repaint(); 
    } 
} 

,然後更改代碼來創建自己的行爲是這樣的:

actionMap.put("VK_LEFT", new KeyAction(p, this, vkLeft)); 
    actionMap.put("VK_RIGHT", new KeyAction(p, this, vkRight)); 
    actionMap.put("VK_UP", new KeyAction(p, this, vkup)); 
    actionMap.put("VK_DOWN", new KeyAction(p, this, vkdown));