Results 1 to 6 of 6
  1. #1
    Nighthawk0973 is offline Game Programmer
    Join Date
    Sep 2011
    Location
    In front of my computer
    Posts
    4
    Rep Power
    0

    Default Help with simple java game/image drawing

    I can't get the string to draw. Right now it's just a textbased game with g.drawString that's supposed to play an animation. There's plenty of comments to explain my thinking if you're confused by my code, here can anybody get this to work?

    Java Code:
    //A Basic TextBasedGame
    //Nights Little Learning Experience XD
    //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
    	}
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Help with simple java game/image drawing

    y = 25; <- maybe thats too small? Try y = 50;

  3. #3
    Nighthawk0973 is offline Game Programmer
    Join Date
    Sep 2011
    Location
    In front of my computer
    Posts
    4
    Rep Power
    0

    Default Re: Help with simple java game/image drawing

    nope. Still nothing. Thanks for trying.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Help with simple java game/image drawing

    y = 200; ?? :D

    Debugg your code...write System.outs in the paint method, in loops and so on...
    Normally in swing programs you have to override paintComponent of jpanel (or similar) and add it to your frame instead of override the paint method of your frame class and call repaint on that jpanel

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: Help with simple java game/image drawing

    What is the value of x? I see you're incrementing it but never bounds checking it. Are you sure it doesn't go off the edge of the screen?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Help with simple java game/image drawing

    Apart from anything else, you have only given your string: 'spaceship' a name because when you draw it, you are currently just drawing an empty string to the screen. You need to actually give it a value and initialize it.
    Try changing:
    Java Code:
    String spaceship;
    
    to:
    
    String spaceship = "spaceship";
    You also should be drawing on a JPanel not directly to the JFrame. You must create a JPanel and add it to the JFrame.
    Try this: http://download.oracle.com/javase/tu...nts/panel.html
    Last edited by jammas615; 10-02-2011 at 05:02 AM.

Similar Threads

  1. need a simple java game
    By Shashwat in forum New To Java
    Replies: 1
    Last Post: 01-03-2011, 02:42 PM
  2. Simple Java image stream display
    By hoobajue in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 01:18 PM
  3. Replies: 1
    Last Post: 04-17-2009, 12:44 AM
  4. Need help getting java drawing match to image.
    By kiduut in forum New To Java
    Replies: 9
    Last Post: 12-28-2008, 01:55 PM
  5. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •