0

I am making a flappy bird like plane game, and in one class I have the background that moves, and then I add it to the jPanel in the main which is in its own class, in another class I have the player and I add that to the main Jpanel. But whichever class I add first dissapears when I add the second class to the JPanel. Here is my background class:

     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 --;


}

}

And here is my plane class: 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--;
}
}

And lastly my main: 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);
}


}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217

2 Answers2

1

You should have a single JPanel that paints itself by overriding paintComponent(). That JPanel class should have a reference to anything that needs to be drawn. From the paintComponent() method, you just iterate over each game object that needs to be drawn and draw it. Better yet, each game object should have a drawMe(Graphics g) method that draws itself, and your paintComponent() method can just pass its Graphics instance into each of those methods.

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);
   }
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Please explain further, I tried what you said but got a little confused as to how I would put it , and it still doesnt work? Could you please elaborate on the subject? – michael mathews Feb 12 '14 at 15:55
  • What do you mean when you say "it doesn't work"? What does your updated code look like? You need to have a single JPanel that draws itself, not a bunch of different JPanels on top of each other. – Kevin Workman Feb 12 '14 at 16:07
  • What I'm really asking is how can I create a sinle JPanel that draws itself? I have been stuck on this problem for two days and can't seem to figure it out, thanks! – michael mathews Feb 13 '14 at 15:48
0

First off in your main class you are extending JFrame but then instantiating it as well which is redundant. You should do either of the two, not both! Ex:

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

Secondly answering your question, you should always have a single class that does all the drawing for your game. Lets call this the Board class. You could have other classes that have functions to draw, but these functions should be called in your Board class within the paintComponent() function. Note: Also make sure to do this -

void paintComponent(Graphics g){
    super.paintComponent(g);
    ......
    g.dispose();
}
Shrey
  • 671
  • 7
  • 14