Making a Test game, however the JFrame just doesnt load. I think it's an easy fix.
Hello.
I am making a test game to try out some things I learned, it had worked previously but after I mixed up similar file names I thought I fixed it, however now it runs with no errors at all, but doesn't display the JFrame... I setVisible(true). But still nothing shows up.
Any guidance or help would be greatly appreciated! I tried looking at tutorials and carefully analyzing the JFrame constructor portion of my class declaration...
Code java:
package magi;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
/**
*
* @author Thomas
*/
public class Magi extends JFrame implements Runnable{
int xDirection, yDirection;
private Image dbImage;
private Graphics dbg;
Image hero;
Image enemy;
Image background;
int x = 250;
int y = 250;
@Override
public void run(){
try{
while(true){
move();
Thread.sleep(5);
}
}
catch(Exception e){
System.out.println("Error");
}
}
public void move(){
x += xDirection;
y += yDirection;
if( x<=0)
x=0;
if(x >=500)
x =500;
if(y <=0)
y=0;
if(y >=500)
y=500;
}
public void setXDirection(int xdir){
xDirection = xdir;
}
public void setYDirection(int ydir){
yDirection = ydir;
}
public class AL extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();
if(keyCode == e.VK_UP){
setYDirection(-1);
}
if(keyCode == e.VK_RIGHT){
setXDirection(+1);
}
if(keyCode == e.VK_DOWN){
setYDirection(+1);
}
if(keyCode == e.VK_LEFT){
setXDirection(-1);
}
}
@Override
public void keyReleased(KeyEvent e){
int keyCode =e.getKeyCode();
if(keyCode == e.VK_UP){
setYDirection(0);
}
if(keyCode == e.VK_RIGHT){
setXDirection(0);
}
if(keyCode == e.VK_DOWN){
setYDirection(0);
}
if(keyCode == e.VK_LEFT){
setXDirection(0);
}
}
}
public void Magi(){
ImageIcon h = new ImageIcon("C:/Users/Thomas/Documents/NetBeansProjects/Magi/src/magi/hero.gif");
ImageIcon e = new ImageIcon("C:/Users/Thomas/Documents/NetBeansProjects/Magi/src/magi/enemy.gif");
ImageIcon b = new ImageIcon("C:/Users/Thomas/Documents/NetBeansProjects/Magi/src/magi/background.gif");
hero = h.getImage();
enemy = e.getImage();
background = b.getImage();
addKeyListener(new AL());
setTitle("Magi Hunt");
setSize(500, 500);
setResizable(false);
setVisible(true);
setBackground(Color.GREEN);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
x=250;
y=250;
}
@Override
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, this);
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.drawImage(background, 0, 0, this);
g.drawImage(hero, x, y, this);
g.drawImage(enemy, 320, 240, this);
g.drawImage(enemy, 46, 70, this);
g.drawImage(enemy, 430, 397, this);
repaint();
}
public static void main(String[] args) {
Magi mg = new Magi();
//Threads
Thread t1 = new Thread(mg);
t1.start();
}
}
Re: Making a Test game, however the JFrame just doesnt load. I think it's an easy fix
public void Magi()
I'm assuming by that you're referring to the constructor of the Magi class.
In that case, remove the return type VOID, and just have it as public Magi(), then it will display your JFrame.
Re: Making a Test game, however the JFrame just doesnt load. I think it's an easy fix
Quote:
Originally Posted by
newbie
public void Magi()
I'm assuming by that you're referring to the constructor of the Magi class.
In that case, remove the return type VOID, and just have it as public Magi(), then it will display your JFrame.
lol awesome thanks! I knew it was something simple!