intertec
New Member
- Messages
- 946
- Reaction score
- 0
- Points
- 0
creating a starfield with a java applet
this tutorial will show you how to create a starfield in java, I will also add action events so we can see the random number generator and starfield in action. this will require some knowlege of java before hand. it only requires basic java though, not much involed, its pretty much basic… if you want to see the full code now, its below…
my class is named “Game” even though this isnt a game, dont let it throw you off lol.
“extends applet” means were using an applet, and “implements ActionListener” is for our button, letting the applet know we are going to use some kind of action. its the implementation for our buttons.
enjoy.
this tutorial will show you how to create a starfield in java, I will also add action events so we can see the random number generator and starfield in action. this will require some knowlege of java before hand. it only requires basic java though, not much involed, its pretty much basic… if you want to see the full code now, its below…
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;
public class Game extends Applet implements ActionListener {
Button myButton = new Button(”press me”);
public void init() {
add(myButton);
myButton.addActionListener(this);
}
public void paint(Graphics g) {
Random r = new Random();
setBackground(Color.black);
for(int count=0;count<=200;count++) {
g.setColor(Color.LIGHT_GRAY);
g.drawOval(r.nextInt(200),r.nextInt(200),1,1);
}
}
public void actionPerformed(ActionEvent event) {
repaint();
}
}
this code is only 27 lines long so it shouldnt be to hard to explain and implement to beginners to java. The first thing i want to explain are the import statements and what they are used for.import java.awt.event.*;
import java.applet.*;
import java.util.Random;
public class Game extends Applet implements ActionListener {
Button myButton = new Button(”press me”);
public void init() {
add(myButton);
myButton.addActionListener(this);
}
public void paint(Graphics g) {
Random r = new Random();
setBackground(Color.black);
for(int count=0;count<=200;count++) {
g.setColor(Color.LIGHT_GRAY);
g.drawOval(r.nextInt(200),r.nextInt(200),1,1);
}
}
public void actionPerformed(ActionEvent event) {
repaint();
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.Random;
the first 1, java.awt.* imports javas “abstract window toolkit” and all the packages underneath it. this will handle the windows, and graphics for our application. the second one, awt.event, will handle our button and events that happend when our button is pressed. the third statement is to activate our applet instead of some normal hava console app. the fourth and final import is for our… random generator. duh. lolimport java.awt.event.*;
import java.applet.*;
import java.util.Random;
my class is named “Game” even though this isnt a game, dont let it throw you off lol.
Code:
public class Game extends Applet implements ActionListener {
Button myButton = new Button(”press me”);
before init (initialize) we are going to instantiate a button object, with the text “press me” on it. this is our button if you havnt guessed it already, lol. next is the init method, included in all applets, this initializes the applet.
public void init() {
right under that code (the code above) you see “add(myButton)” which actually displays the button on the form. and the code beneatht hat will add the actionListener to it, this allows us to add functionality to the button.
add(myButton);
myButton.addActionListener(this);
the next peice of code is the paint method in our applet, this handles all our graphics for us, its our interface. in here we first instantiate our random object and set the background color to black.myButton.addActionListener(this);
Random r = new Random();
setBackground(Color.black);
the next peice of code is a for loop, this will add all of our stars, this is really the meat of the application and there isnt much to it. the for loop is basically setup the same way in c and php, and a few other languages. if you have basic programming knowlege and syntax down this should be hella easy for you.setBackground(Color.black);
for(int count=0;count<=200;count++) {
g.setColor(Color.LIGHT_GRAY);
g.drawOval(r.nextInt(200),r.nextInt(200),1,1);
}
we set the color of the stars (which we coudl have done outside of the loop) to LIGHT_GRAY, and the next function is the “drawOval” function which will give us very small 1px by 1px ovals lol, we could have also used fillOval.g.setColor(Color.LIGHT_GRAY);
g.drawOval(r.nextInt(200),r.nextInt(200),1,1);
}
for(int count=0;count<=200;count++) {
g.setColor(Color.LIGHT_GRAY);
g.fillOval(r.nextInt(200),r.nextInt(200),1,1);
}
using the fillOval method gives us small 1px ovals but filled in, and they are smaller. its all in preference. Also in the for loop we used our random number generator. “r.nextInt(200)” which gives us a number from 0 to 200, we used that for the x & y coordinates of the drawOval function. drawOval takes 4 parametersg.setColor(Color.LIGHT_GRAY);
g.fillOval(r.nextInt(200),r.nextInt(200),1,1);
}
drawOval(x,y,w,h);
an x coordinate, y coordinate, width and height. get whats going on? we are randomly placing the stars in a 200×200 grid, with a width and height of 1×1 pixels. the for loop will execute until we reach 200, err 199 stars, wait no.. lol yea 200, “<=”
public void actionPerformed(ActionEvent event) {
repaint();
}
the last function is our repaint(); method that iwll randomly set the stars again, this is what is triggered when we press our button.repaint();
}
enjoy.