Would you like to react to this message? Create an account in a few clicks or log in to continue.

Help with simple java game/image drawing

2 posters

 :: Java :: Help!

Go down

Help with simple java game/image drawing Empty Help with simple java game/image drawing

Post  Nighthawk0973 Mon Sep 26, 2011 12:38 am

I've been trying to start learning Java game design and I've come across a logic error, I can't get the screen to draw anything. I've currently added a simple 'g.drawString(stringname, x, y);' command so that I can start with a text based game before I start drawing any images or working on keyinput. It's just a simple animation. Here's the code:

Code:

//A Basic TextBasedGame
//Nights Little Learning Experience XD
http://TextBasedGame.java - The one and only class
//Can nighthawk get the image to draw this time (cross fingers for yes!)

//import javax.swing for guis and java.awt for painting...
import javax.swing.*;
import java.awt.*;

public class TextBasedGame extends JFrame implements Runnable{ //create main class & extends JFrame
   
    //Base Variables Creation, values defined in constructor
    int x;
    int y;
    String spaceship;
    boolean gameRunning = true; //Must be true right away
    Thread gameFlow = new Thread(this); //Thread for timer
   
    public TextBasedGame(){ //default constructor, used for GUI setup and var init..
       
        setTitle("Text Based Game - By Night");
        setSize(800, 600);
        setLocation(110, 50);
        setResizable(false);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //This combo sets a GUI in the middle and makes it visible and not resizable
        x = 25;
        y = 25;
        spaceship = "<-*->";
        //Creates our basic spaceship and it's x and y values
        gameFlow.start();
        //starts our gameFlow Thread
    }
   
    public void paint(Graphics g){ //paint method
       
        super.paint(g);
        g.drawString(spaceship, x, y); //paints spaceship
    }
   
    public void updateGame(){ //update game method will update the games progress/values for painting
       
        x++; //this time it simply increases x for an animation
    }
   
    public void run(){ //run method
       
        while(gameRunning){ //tests if game is running...
           
            updateGame(); //runs updateGame method
            repaint(); //calls repaint
            try{
               
                gameFlow.sleep(30); //thread sleep
            }catch(Exception e){
               
                JOptionPane.showMessageDialog(null, e); //throws exception
            }
        }
    }
   
    public static void main(String[] beans){ //main method
       
        TextBasedGame TBG = new TextBasedGame(); //calls constructor
    }
}

I'm working pretty much 24/7 to fix this but still can't find anything via google, anybody mind helping me? Thanks, Nighthawk.
Nighthawk0973
Nighthawk0973
Moderator
Moderator

Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer

Back to top Go down

Help with simple java game/image drawing Empty Re: Help with simple java game/image drawing

Post  David B Mon Sep 26, 2011 4:43 pm

Nighthawk0973 wrote:I'm working pretty much 24/7 to fix this but still can't find anything via google, anybody mind helping me? Thanks, Nighthawk.
It's not that I would mind helping you, it's just that I haven't learned any Java yet. (Give me time...)



You might want to wait for legolizard. (You may also want to consider the Alice Community Forums.)
David B
David B
Administrator
Administrator

Posts : 618
Join date : 2011-07-20
Location : The Twilight Zone!

https://programmingforums.forumotion.com/

Back to top Go down

Back to top

- Similar topics

 :: Java :: Help!

 
Permissions in this forum:
You cannot reply to topics in this forum