从多个类中绘制图形?

时间:2014-02-12 13:55:17

标签: java graphics drawing screen flappy-bird-clone

我正在制作像平面游戏一样的飞鸟,并且在一个班级中我有移动的背景,然后我将它添加到主要的jPanel中,它在自己的类中,在另一个类中我有玩家和我将其添加到主Jpanel。但是当我将第二个类添加到JPanel时,无论我添加哪个类,首先都会消失。这是我的背景课程:

     import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.Timer;


public class map1 extends JPanel implements ActionListener{
Timer t = new Timer(5, this);
int rect1x = 1000, rect1y = 0;
int rect2x = 1250, rect2y = 0;
int rect3x = 1500, rect3y = 0;
int rect4x = 1750, rect4y = 0;
int rect5x = 2000, rect5y = 0;
int rect1dx = 1000, rect1dy = 300;
int rect2dx = 1250, rect2dy = 350;
int rect3dx = 1500, rect3dy = 400;
int rect4dx = 1750, rect4dy = 350;
int rect5dx = 2000, rect5dy = 300;
public map1(){t.start();}
public void rectset1(Graphics g){
g.drawRect(rect1x, rect1y, 50, 100);
g.drawRect(rect1dx, rect1dy, 50, 300);
}
public void rect2(Graphics g){
g.drawRect(rect2x, rect2y, 50, 150);
g.drawRect(rect2dx, rect2dy, 50, 300);
}
public void rect3(Graphics g){
g.drawRect(rect3x, rect3y, 50, 200);
g.drawRect(rect3dx, rect3dy, 50, 300);
}
public void rect4(Graphics g){
g.drawRect(rect4x, rect4y, 50, 150);
g.drawRect(rect4dx, rect4dy, 50, 300);
}
public void rect5(Graphics g){
g.drawRect(rect5x, rect5y, 50, 100);
g.drawRect(rect5dx, rect5dy, 50, 300);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
rectset1(g);
rect2(g);
rect3(g);
rect4(g);
rect5(g);

}

@Override
public void actionPerformed(ActionEvent e) {
repaint();
 rect1x--;
 rect2x --;
 rect3x --;
rect4x --;
 rect5x --;
 rect1dx --;
 rect2dx --;
 rect3dx --;
 rect4dx --;
 rect5dx --;


}

}

这是我的飞机课:     import java.awt.Graphics;     import java.awt.event.ActionEvent;     import java.awt.event.ActionListener;     import java.awt.event.KeyEvent;     import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;


public class player extends JPanel implements ActionListener, KeyListener   {
int x = 200, y = 300;
Timer t = new Timer(5, this);
public void space(){
    y+=20;
}
public void paintComponent(Graphics g){
    super.paintComponent(g);
    g.drawOval(x, y, 50, 50);

}
@Override
public void keyTyped(KeyEvent e) {


}

@Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
    if(code == KeyEvent.VK_SPACE){space();}
}

@Override
public void keyReleased(KeyEvent e) {


}

@Override
public void actionPerformed(ActionEvent e) {
    repaint();
    y--;
}
}

最后我的主要人物:     import javax.swing.JFrame;

@SuppressWarnings("serial")
public class testMain extends JFrame{

public testMain(){}
public static void main(String args[]){
map1 m = new map1();
player q = new player();
JFrame j = new JFrame();
j.setSize(800,600);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.add(m);
j.add(q);
j.setVisible(true);
}


}

2 个答案:

答案 0 :(得分:1)

你应该有一个JPanel通过覆盖paintComponent()来绘制自己。 JPanel类应该引用需要绘制的任何内容。从paintComponent()方法中,您只需遍历需要绘制的每个游戏对象并绘制它。更好的是,每个游戏对象都应该有一个drawMe(Graphics g)方法来绘制自己,而paintComponent()方法只能将其Graphics实例传递给每个方法。

public class GamePanel extends JPanel{

   //example GameObjects, these could be in a list
   GameObject bird = new Bird();
   GameObject cloud = new Cloud();

   public void paintComponent(Graphics g){
      bird.drawMe(g);
      cloud.drawMe(g);
   }
}

答案 1 :(得分:0)

首先在你的主类中扩展JFrame,然后实例化它,这是多余的。你应该做两个中的任何一个,而不是两个! 例如:

public class main extends JFrame{
    public main(){
        setSize(800,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        add(m);
        add(q);
        setVisible(true);
    }
}

其次回答你的问题,你应该总是有一个单独的课程来完成游戏的所有绘图。让我们称之为 Board 类。您可以使用其他具有绘制功能的类,但应在paintComponent()函数的 Board 类中调用这些函数。 注意:还要确保这样做 -

void paintComponent(Graphics g){
    super.paintComponent(g);
    ......
    g.dispose();
}