Results 1 to 6 of 6
- 09-26-2011, 05:33 AM #1
Game Programmer
- Join Date
- Sep 2011
- Location
- In front of my computer
- Posts
- 4
- Rep Power
- 0
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 } }
- 09-26-2011, 07:08 AM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Help with simple java game/image drawing
y = 25; <- maybe thats too small? Try y = 50;
- 09-26-2011, 04:59 PM #3
Game Programmer
- Join Date
- Sep 2011
- Location
- In front of my computer
- Posts
- 4
- Rep Power
- 0
Re: Help with simple java game/image drawing
nope. Still nothing. Thanks for trying.
- 09-26-2011, 05:07 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
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
- 09-26-2011, 06:20 PM #5
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!
- 10-02-2011, 04:09 AM #6
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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:
You also should be drawing on a JPanel not directly to the JFrame. You must create a JPanel and add it to the JFrame.Java Code:String spaceship; to: String spaceship = "spaceship";
Try this: http://download.oracle.com/javase/tu...nts/panel.htmlLast edited by jammas615; 10-02-2011 at 05:02 AM.
Similar Threads
-
need a simple java game
By Shashwat in forum New To JavaReplies: 1Last Post: 01-03-2011, 02:42 PM -
Simple Java image stream display
By hoobajue in forum New To JavaReplies: 2Last Post: 08-17-2010, 01:18 PM -
Problems drawing a section of an image onto another image.
By Cain in forum Java 2DReplies: 1Last Post: 04-17-2009, 12:44 AM -
Need help getting java drawing match to image.
By kiduut in forum New To JavaReplies: 9Last Post: 12-28-2008, 01:55 PM -
drawing an image to an offscreen image
By hunterbdb in forum Java 2DReplies: 9Last Post: 10-30-2008, 06:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks