如何让用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;
}
} 展开
2个回答
展开全部
你的 paintComponent 中用的都是常数, 没用到 myhero 的数据, 当然不会动!
我稍为改一下就会动了:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x=myhero.x, y=myhero.y;
g.fillRect(x+50, y+80, 7, 40);
g.fillRect(x+57,y+87,20,24);
g.fillRect(x+77,y+80,7,40);
g.setColor(Color.red);
g.fillOval(x+59,y+92,15,15);
g.setColor(Color.black);
g.fillRect(x+65,y+68,5,24);
}
但这种画法永远向著上方, 还不会转向.
你必须再调整写法.
我稍为改一下就会动了:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int x=myhero.x, y=myhero.y;
g.fillRect(x+50, y+80, 7, 40);
g.fillRect(x+57,y+87,20,24);
g.fillRect(x+77,y+80,7,40);
g.setColor(Color.red);
g.fillOval(x+59,y+92,15,15);
g.setColor(Color.black);
g.fillRect(x+65,y+68,5,24);
}
但这种画法永远向著上方, 还不会转向.
你必须再调整写法.
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询