package tankewar; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.KeyEvent; public class PlayerOne extends Tank{ public PlayerOne(String img, int x, int y, GamePanel gamePanel, String upImg, String leftImg, String rightImg, String downImg) { super(img, x, y, gamePanel, upImg, leftImg, rightImg, downImg); // TODO Auto-generated constructor stub } public void keyPressedl(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_A: left = true; break; case KeyEvent.VK_S: down = true; break; case KeyEvent.VK_D: right = true; break; case KeyEvent.VK_W: up = true; break; default: break; } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_A: left = false; break; case KeyEvent.VK_S: down = false; break; case KeyEvent.VK_D: right = false; break; case KeyEvent.VK_W: up = false; break; default: break; } } public void move() { if(left) { leftward(); } else if(right) { rightward(); } else if(up) { upward(); } else if(down) { downward(); } } @Override public void paintSelf(Graphics g) { // TODO Auto-generated method stub g.drawImage(img,x,y,null); } @Override public Rectangle getRec() { // TODO Auto-generated method stub return new Rectangle(x,y,width,height); } @Override public void paintSelft(Graphics g) { // TODO Auto-generated method stub } @Override public Rectangle gerRec() { // TODO Auto-generated method stub return null; } }
时间: 2024-02-15 11:27:03 浏览: 114
这是一个 Java 语言编写的坦克大战游戏中的玩家一的坦克类。它继承了坦克类,并重写了 keyPressed 和 keyReleased 方法来处理用户输入的方向键事件。在 move 方法中,根据用户输入的方向键来控制坦克的移动方向。在 paintSelf 方法中,使用 Graphics 类的 drawImage 方法来绘制坦克的图片。getRec 和 gerRec 方法分别返回坦克的矩形区域。
相关问题
package test; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.Random; public class Test3{ public static void main (String args[]) { Win win=new Win(); win.setTitle("输入序列号"); win.setBounds(10,10,460,360); } } class Police implements KeyListener,FocusListener{ public void keyPressed(KeyEvent e) { JTextField t=(JTextField)e.getSource(); if (t.getCarePosition()>=6) t.transferFocus(); } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void focusGained(FocusEvent e) { JTextField text=(JTextField)e.getSource(); text.setText(null); } public void focusLost(FocusEvent e){} } class Win extends JFrame { JTextField text[]=new JTextField[3]; Police police; JButton b; Win() { setLayout(new FlowLayout()); police = new Police(); for(int i=0;i<3;i++) { text[i]=new JTextField(7); text[i].addKeyListener(police); //监视键盘事件 text[i].addFocusListener(police); add(text[i]); } b=new JButton("确定"); add(b); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); text[0].requestFocusInWindow(); } }哪里有错误
代码的缩进格式不够规范,需要进行调整。此外,代码中的一些注释也需要进行调整,以保证清晰易读。以下是修改后的代码:
```
package test;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
public class Test3{
public static void main (String args[]) {
Win win=new Win();
win.setTitle("输入序列号");
win.setBounds(10,10,460,360);
}
}
class Police implements KeyListener,FocusListener{
public void keyPressed(KeyEvent e) {
JTextField t=(JTextField)e.getSource();
if (t.getCarePosition()>=6)
t.transferFocus();
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
public void focusGained(FocusEvent e) {
JTextField text=(JTextField)e.getSource();
text.setText(null);
}
public void focusLost(FocusEvent e){}
}
class Win extends JFrame {
JTextField text[]=new JTextField[3];
Police police;
JButton b;
Win() {
setLayout(new FlowLayout());
police = new Police();
for(int i=0;i<3;i++) {
text[i]=new JTextField(7);
text[i].addKeyListener(police); //监视键盘事件
text[i].addFocusListener(police);
add(text[i]);
}
b=new JButton("确定");
add(b);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
text[0].requestFocusInWindow();
}
}
```
修正代码package text5_2; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.DefaultTableModel; public class table2 extends JFrame implements ActionListener{ public static void main(String[] args) { new table();
根据代码中的类名,应该将 `new table()` 改为 `new table2()`,即将 `table` 改为 `table2`。如下所示:
```
package text5_2;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class table2 extends JFrame implements ActionListener {
public static void main(String[] args) {
new table2();
}
}
```
阅读全文