Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Making a Test game, however the JFrame just doesnt load. I think it's an easy fix.

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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...

     
     
    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();
     
     
        }
    }


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. The Following User Says Thank You to newbie For This Useful Post:

    Harrald (September 27th, 2012)

  4. #3
    Member
    Join Date
    Jul 2012
    Posts
    56
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Re: Making a Test game, however the JFrame just doesnt load. I think it's an easy fix

    Quote Originally Posted by newbie View Post
    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!

Similar Threads

  1. Replies: 4
    Last Post: September 3rd, 2012, 02:47 AM
  2. Easy Tic Tac Toe game and Applet Assignment
    By Jmastersam in forum Java Theory & Questions
    Replies: 2
    Last Post: April 28th, 2012, 10:06 AM
  3. Exception in JUnit test (easy question)
    By opium in forum Exceptions
    Replies: 1
    Last Post: February 14th, 2012, 09:09 AM
  4. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 09:21 PM
  5. JFrame, frame's title doesnt appear.
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 26th, 2009, 03:38 PM