JAVA编程。为何小球能在窗口内弹跳,而由键盘上下左右键控制的矩形方框在窗口内没有反应?求大神解答
importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.RenderingHints;impor...
import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.RenderingHints;import javax.swing.JFrame;import javax.swing.JPanel;import java.awt.event.*;import java.awt.*;@SuppressWarnings("serial")public class Game extends JPanel {int direction=1,NE=0,SE=1,SW=2,NW=3;//int x = 0;int y = 0;int a=xmax-300,b=ymax-200;//定义矩形方块位置int ballsize=30;static int xmax=600;static int ymax=800;//private static TextField tf;private void moveBall() { switch(direction){ case 0: x=x+1; y=y-1; if(y<=0) direction=SE;//1 if(x>=xmax-ballsize-22) direction=NW;//3 break; case 1: x=x+1; y=y+1; if(y>=ymax-ballsize-55) direction=NE;//0 if(x>=xmax-ballsize-22) direction=SW;//2 break; case 2: x=x-1; y=y+1; if(y>=ymax-ballsize-55) direction=NW;//3 if(x<=0) direction=SE;//1 break; case 3: x=x-1; y=y-1; if(y<=0) direction=SW;//2 if(x<=0-ballsize) direction=NE;//0 break; } }public void moveRect(){ addKeyListener(new KeyAdapter(){ /*********注意这里***********/ public void keyPressed(KeyEvent e){ if(e.getKeyCode()== KeyEvent.VK_UP){ //System.out.println("\n Go Up"); b=b-5;/*********注意这里***********/ } else if(e.getKeyCode()== KeyEvent.VK_DOWN){ // System.out.println("\n Go Down"); b=b+5;/*********注意这里***********/ } else if(e.getKeyCode()== KeyEvent.VK_LEFT){ //System.out.println("\n Go Left"); a=a-5;/*********注意这里***********/ } else if(e.getKeyCode()== KeyEvent.VK_RIGHT){ //System.out.println("\n Go Right"); a=a+5;/*********注意这里***********/ } //else // System.out.println(e.getKeyChar()); // repaint();/*********注意这里***********/ } } );} @Overridepublic void paint(Graphics g) {super.paint(g); Graphics2D g2d = (Graphics2D) g;g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.BLUE); g.fillOval(x, y, 30, 30); g.fillRect(a, b , 100, 10);//绘制一个长100宽10的黑色矩形 }//public interface ActionListener extends MouseMotionListener//{ //public void mouseMoved(MouseEvent m) //{ //}//} public static void main(String[] args) throws InterruptedException { JFrame frame = new JFrame("Sample Frame"); Game game = new Game(); frame.add(game); frame.setSize(600, 800); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //tf=new TextField(30); //frame.add("North",tf); while (true) {game.moveRect();game.moveBall();game.repaint();Thread.sleep(3); } }}
展开
展开全部
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.RenderingHints;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.*;
@SuppressWarnings("serial")
public class Game extends JPanel {
int direction = 1, NE = 0, SE = 1, SW = 2, NW = 3;//
int x = 0;
int y = 0;
int a = xmax - 300, b = ymax - 200;//定义矩形方块位置
int ballsize = 30;
static int xmax = 600;
static int ymax = 800;
//private static TextField tf;
private void moveBall() {
switch (direction) {
case 0 :
x = x + 1;
y = y - 1;
if (y <= 0)
direction = SE;//1
if (x >= xmax - ballsize - 22)
direction = NW;//3
break;
case 1 :
x = x + 1;
y = y + 1;
if (y >= ymax - ballsize - 55)
direction = NE;//0
if (x >= xmax - ballsize - 22)
direction = SW;//2
break;
case 2 :
x = x - 1;
y = y + 1;
if (y >= ymax - ballsize - 55)
direction = NW;//3
if (x <= 0)
direction = SE;//1
break;
case 3 :
x = x - 1;
y = y - 1;
if (y <= 0)
direction = SW;//2
if (x <= 0 - ballsize)
direction = NE;//0
break;
}
}
public void moveRect() {
KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
manager.addKeyEventPostProcessor(new KeyEventPostProcessor() {
@Override
public boolean postProcessKeyEvent(KeyEvent e) {
if (e.getID() == KeyEvent.KEY_PRESSED) {
if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("\n Go Up");
b = b - 5;
/*********注意这里***********/
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
System.out.println("\n Go Down");
b = b + 5;
/*********注意这里***********/
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("\n Go Left");
a = a - 5;
/*********注意这里***********/
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("\n Go Right");
a = a + 5;
System.out.println(a);
/*********注意这里***********/
}
//else
// System.out.println(e.getKeyChar());
// repaint();/*********注意这里***********/
}
return false;
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.BLUE);
g.fillOval(x, y, 30, 30);
g.fillRect(a, b, 100, 10);//绘制一个长100宽10的黑色矩形
}
//public interface ActionListener extends MouseMotionListener
//{
//public void mouseMoved(MouseEvent m)
//{
//}
//}
public static void main(String[] args) throws InterruptedException {
JFrame frame = new JFrame("Sample Frame");
Game game = new Game();
game.moveRect();
frame.add(game);
frame.setSize(600, 800);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//tf=new TextField(30);
//frame.add("North",tf);
while (true) {
game.moveBall();
game.repaint();
Thread.sleep(3);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询