import java.awt.*;
import java.awt.event.*;
import javax.swing.JPanel;
import java.util.ArrayList;
/**
* This code was originally by the user "UniBrain" from the website:
* http://forum.codecall.net/topic/74377-moving-graphics-across-up-and-down-a-jpanel/?p=652384
*
* but has been modified by me for my own purposes
*/
public class Canvas2 extends JPanel
{
//attributes
private Rectangle player1;
private Rectangle player2;
private ArrayList<KeyEvent> log;
//constructor
public Canvas2()
{
//initialize object
player1 = new Rectangle (50, 50, 50, 50);
player2 = new Rectangle (50, 50, 50, 50);
log = new ArrayList<KeyEvent>();
//set canavs background colour
setBackground (Color.white);
//add the key listener in the constructor of your canavas/panel
addKeyListener(new myKeyListener());
//ensure focus is on this canavas/panel for key operations.
setFocusable(true);
}
//painting
public void paintComponent(Graphics graphics)
{
super.paintComponent(graphics);
Graphics2D graphics2d =(Graphics2D)graphics;
graphics.setColor(Color.blue);
graphics2d.fill(player1);
graphics2d.fill(player2);
}
//function which essentially re-creates rectangle with varying x orientations. (x-movement)
public void moveX(int mutationDistance, Rectangle sampleObject)
{
sampleObject.setBounds(sampleObject.x + mutationDistance, sampleObject.y, sampleObject.width, sampleObject.height);
}
//function which essentially re-creates rectangle with varying y orientations. (y-movement)
public void moveY(int mutationDistance, Rectangle sampleObject)
{
sampleObject.setBounds(sampleObject.x, sampleObject.y + mutationDistance, sampleObject.width, sampleObject.height);
}
public void move(){
/*
* This method of keeping track of key events and using a loop is taken from the user Michael Meyers at
* http://stackoverflow.com/questions/752999/how-do-i-handle-multiple-key-presses-in-java
*/
Thread thread1 = new Thread()
{
public void run() {
for(KeyEvent keyEvent: log){
if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){moveX(5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){moveX (-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){moveY (-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){moveY (5, player1);}
}
}
};
Thread thread2 = new Thread()
{
public void run() {
for(KeyEvent keyEvent: log){
if(keyEvent.getKeyCode() == KeyEvent.VK_D){moveX(5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_A){moveX(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_W){moveY(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_S){moveY(5, player2);}
}
}
};
//Starts the threads
thread1.start();
thread2.start();
//Waits for them to finish
try{
thread1.join();
thread2.join();
log = new ArrayList<KeyEvent>();
}
catch (Exception e){System.out.println(e);}
}
//listener
private class myKeyListener implements KeyListener
{
//implement all the possible actions on keys
public void keyPressed(KeyEvent keyEvent)
{
if(keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE){System.exit (0);}
Thread thread3 = new Thread()
{
public void run() {
if(keyEvent.getKeyCode() == KeyEvent.VK_RIGHT){moveX(5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_LEFT){moveX(-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_UP){moveY(-5, player1);}
if(keyEvent.getKeyCode() == KeyEvent.VK_DOWN){moveY (5, player1);}
}
};
Thread thread4 = new Thread()
{
public void run() {
if(keyEvent.getKeyCode() == KeyEvent.VK_D){moveX(5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_A){moveX(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_W){moveY(-5, player2);}
if(keyEvent.getKeyCode() == KeyEvent.VK_S){ moveY(5, player2);}
}
};
Thread thread5 = new Thread()
{
public void run() {
repaint();
}
};
thread3.start();
thread4.start();
thread5.start();
try{
thread3.join();
thread4.join();
thread5.join();
}
catch (Exception e){System.out.println(e);}
repaint();
}
public void keyReleased(KeyEvent keyEvent)
{
}
public void keyTyped(KeyEvent keyEvent)
{
}
}
public static void pause(int secs){
try{Thread.sleep(secs*0);} catch(Exception e){}
}
}
然後由這個類
import javax.swing.JFrame;
import java.awt.Dimension;
/**
* This code was taken from the user "UniBrain" from the website:
* http://forum.codecall.net/topic/74377-moving-graphics-across-up-and-down-a-jpanel/?p=652384
*/
public class Display
{
public static void main (String [ ] arguments)
{
JFrame frame = new JFrame("key listener demo");
Canvas2 panel = new Canvas2();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setContentPane(panel);
frame.setPreferredSize(new Dimension(800, 600));
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.pack();
}
}
我不明白你想要做什麼。您無法同時使用此代碼移動兩個玩家,因爲您無法同時按兩個鍵。每次按一個鍵時,方法keyPressed()都會被調用一次。 – Daniel
你的意思是兩個人使用同一個鍵盤,每個人控制一個玩家? – Daniel
@丹尼爾是的,我做到了。例如,在相同的鍵盤上,一個人將使用箭頭鍵來控制矩形玩家1,另一個人將使用WASD來控制矩形玩家2 – ADC2000