Sign in to follow this  
JJ Awad

Why is my JFrame or my JPanel not appearing?

Recommended Posts

JJ Awad    121
 

I am making a game. There are 3 different files in the project, there is a file called Dude, Frame and Board. So can anyone help me? The code: The code for Board:

package Ourgame;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener{
   Dude p;
   Image img;
   Timer time;

   public Board() {
      p = new Dude();
      addKeyListener(new AL());
      setFocusable(true);
            ImageIcon i = new ImageIcon("/Users/appleuser/Desktop");
            img = i.getImage();
            time = new Timer(5, this);
            time.start();
   }

   public void actionPerformed(ActionEvent e) {
      p.move();
      repaint();
   }

      public void paint(Graphics g) {
         super.paint(g);
         Graphics2D g2d = (Graphics2D) g;

         g2d.drawImage(img, 0, 0, null);
         g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
}

   private class AL extends KeyAdapter {
      public void keyReleased(KeyEvent e) {
         p.keyReleased(e);
      }
      public void keyPressed(KeyEvent e) {
         p.keyPressed(e);
      }
   }
} 

The code for Dude:

package Ourgame;

import java.awt.Image;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Dude {
int x, dx, y;
Image still;

public Dude() {
ImageIcon i = new ImageIcon("/Users/appleuser/Desktop/the man.bmp");
still = i.getImage();
x = 10;
y = 172;
}

public void move() {
x = x + dx;

}
public int getX() {
return x;
}
public int getY() {
return y;
}

public Image getImage() {
return still;
}

public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();

if(key == KeyEvent.VK_LEFT);
dx = -1;

if(key == KeyEvent.VK_RIGHT)
dx = 1;

}

public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();

if(key == KeyEvent.VK_LEFT);
dx = 0;

if(key == KeyEvent.VK_RIGHT)
dx = 0;

}



} 

The code for Frame:

package Ourgame;

import javax.swing.JFrame;

public class Frame {

public static void main(String[] args){
JFrame frame = new JFrame("2D game");
frame.add(new Board());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1200, 365);
frame.setVisible(true);
}
}

Thanks:)

Edited by Glass_Knife

Share this post


Link to post
Share on other sites
nerdboy64    718

First, the Board never gets resized, so it defaults to 0px by 0px. Call setPreferredSize(new Dimension(1200, 365)) in the constructor of Board. Then, replace setSize(1200, 365) in Frame with pack(), which automatically scales the window to fit the Board.

Share this post


Link to post
Share on other sites
Glass_Knife    8636

Two things.  First, put long code like this in the code window so it doesn't take up a lot of space.  The code button looks like this '<>'.  I went ahead and fixed that.

Second, I tried your code, and it worked.  I didn't need to do anything with the sizing. 

 

But you didn't really ask a question.  Did you have questions?  "Here's the code.  Thanks." isn't enough.

 

And these might help:

 

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-active-rendering-r2418

http://www.gamedev.net/page/resources/_/technical/general-programming/java-games-keyboard-and-mouse-r2439

Share this post


Link to post
Share on other sites
Glass_Knife    8636

Also worth noting that some of your if statements have semi-colons directly after them so probably aren't doing what you want....

 

Good catch.  I didn't even see that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this