Help with simple java game/image drawing
2 posters
Help with simple java game/image drawing
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:
I'm working pretty much 24/7 to fix this but still can't find anything via google, anybody mind helping me? Thanks, Nighthawk.
- 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- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: Help with simple java game/image drawing
It's not that I would mind helping you, it's just that I haven't learned any Java yet. (Give me time...)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.
You might want to wait for legolizard. (You may also want to consider the Alice Community Forums.)
Similar topics
» Nighthawks Java Tutorials (Look here if you need to learn Java!)
» What compiler should I use for Java?
» Java Script is cool
» Java "Hello World" Tutorial by iD Tech 365
» Sad Game Moments...
» What compiler should I use for Java?
» Java Script is cool
» Java "Hello World" Tutorial by iD Tech 365
» Sad Game Moments...
Permissions in this forum:
You cannot reply to topics in this forum
|
|