如何让用java中paintComponent()画出的图形动起来。还缺什么代码吗?
importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassTankGameextend...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TankGame extends JFrame {
public static void main(String[] args) {
TankGame mtg=new TankGame();
}
public TankGame()
{
this.setSize(400,300);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
MyPanel mp=new MyPanel();
add(mp);
mp.setBackground(Color.cyan);
this.addKeyListener(mp);
}
}
class MyPanel extends JPanel implements KeyListener
{
Tank myhero=null;
public MyPanel()
{
myhero=new Tank(100,100);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(50, 80, 7, 40);
g.fillRect(57,87,20,24);
g.fillRect(77,80,7,40);
g.setColor(Color.red);
g.fillOval(59,92,15,15);
g.setColor(Color.black);
g.fillRect(65,68,5,24);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP){
this.myhero.setDirect(0);
this.myhero.y--;
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
this.myhero.setDirect(1);
this.myhero.x--;
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
this.myhero.setDirect(2);
this.myhero.y++;
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
this.myhero.setDirect(3);
this.myhero.y--;
}else{System.out.println("什么也不做");}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class Tank
{
int speed=1;
int x=0;
int y=0;
int direct=0;
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
} 展开
import java.awt.event.*;
import javax.swing.*;
public class TankGame extends JFrame {
public static void main(String[] args) {
TankGame mtg=new TankGame();
}
public TankGame()
{
this.setSize(400,300);
this.setLocation(200,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
MyPanel mp=new MyPanel();
add(mp);
mp.setBackground(Color.cyan);
this.addKeyListener(mp);
}
}
class MyPanel extends JPanel implements KeyListener
{
Tank myhero=null;
public MyPanel()
{
myhero=new Tank(100,100);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.fillRect(50, 80, 7, 40);
g.fillRect(57,87,20,24);
g.fillRect(77,80,7,40);
g.setColor(Color.red);
g.fillOval(59,92,15,15);
g.setColor(Color.black);
g.fillRect(65,68,5,24);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP){
this.myhero.setDirect(0);
this.myhero.y--;
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
this.myhero.setDirect(1);
this.myhero.x--;
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
this.myhero.setDirect(2);
this.myhero.y++;
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
this.myhero.setDirect(3);
this.myhero.y--;
}else{System.out.println("什么也不做");}
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
class Tank
{
int speed=1;
int x=0;
int y=0;
int direct=0;
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public Tank(int x,int y)
{
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询