Jump to content

  • Log In with Google      Sign In   
  • Create Account

Why is my JFrame or my JPanel not appearing?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 GUIboy10   Members   -  Reputation: 121

Like
0Likes
Like

Posted 08 February 2014 - 11:21 PM

 

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, 12 February 2014 - 12:52 PM.


Sponsor:

#2 nerdboy64   Members   -  Reputation: 579

Like
0Likes
Like

Posted 09 February 2014 - 01:36 AM

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.


#3 Glass_Knife   Moderators   -  Reputation: 5403

Like
0Likes
Like

Posted 12 February 2014 - 12:46 PM

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"
Indie Game Programming

#4 xexuxjy   Members   -  Reputation: 671

Like
2Likes
Like

Posted 13 February 2014 - 04:13 AM

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



#5 Glass_Knife   Moderators   -  Reputation: 5403

Like
0Likes
Like

Posted 13 February 2014 - 10:54 AM

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"
Indie Game Programming




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS