Why is my JFrame or my JPanel not appearing?

Started by
3 comments, last by Glass_Knife 10 years, 2 months ago

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:)

Advertisement

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.

My website: Brass Watch Games

Come check out Shipyard, my first real game project (Very WIP): Game Website Development Journal

Shipyard is a 2D turn-based strategy with a sci-fi theme, in which you build ships from individual parts rather than being given a selection of predefined models.

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

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

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

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.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

This topic is closed to new replies.

Advertisement