public class Starfield extends JFrame implements KeyListener, Runnable { private
ID: 3879249 • Letter: P
Question
public class Starfield extends JFrame implements KeyListener, Runnable {
private static String applicationPath;
private BufferStrategy strategy;
private int windowWidth, windowHeight;
private int maxStarX, maxStarY;
private Image[] imgStar = new Image[3];
private Font myFont;
private static final int NUMSTARS = 50;
// embedded class for handling an individual Star
private class Star {
public double x, y;
public double dx, dy;
public int imgNum;
public void draw(Graphics g) {
g.drawImage(imgStar[imgNum], (int)x, (int)y, null);
}
public void animate() {
// TO DO: move the star by adding dx and dy to x and y
// TO DO: reverse the star's movement when it hits the JFrame's boundaries
}
public void randomise() {
x=(Math.random() * maxStarX);
y = (Math.random() * maxStarY);
dx =Math.random() -Math.random();
dy = Math.random() -Math.random();
imgNum=(int)(Math.random() * 3);
}
}
// end of embedded Star class
private Star[] stars = new Star[NUMSTARS];
private boolean paused = false;
public static void main(String[] args) {
Starfield s = new Starfield(600,600);
}
public Starfield(int w,int h) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
java.io.File currentDir = new java.io.File("");
applicationPath = currentDir.getAbsolutePath();
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - w/2; int y = screensize.height/2 - h/2;
setBounds(x,y,w,h);
setVisible(true);
windowWidth = w;
windowHeight = h;
for (int s=1; s<=3; s++) {
ImageIcon icon =new ImageIcon(applicationPath+"/star_" +s+".png");
imgStar[sT1] = icon.getImage();
}
maxStarX = wTimgStar[0].getWidth(null);
maxStarY = hTimgStar[0].getHeight(null);
myFont = new Font("Courier",Font.BOLD,14);
createBufferStrategy(2);
strategy = getBufferStrategy();
addKeyListener(this);
initialiseStars();
Thread t = new Thread(this);
t.start();'
}
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if (keyCode==KeyEvent.VK_P)
paused=!paused;
else if (keyCode==KeyEvent.VK_ESCAPE)
System.exit(0);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void run() {
while (1==1) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {}
if (!paused)
animateStarfield();
repaint();
}
}
private void initialiseStars() {
for (int i=0; i<NUMSTARS; i++) {
stars[i] = new Star();
stars[i].randomise();
}
}
private void animateStarfield() {
// TO DO: animate all stars by calling their animate() method
}
public void paint(Graphics g) {
// TO DO: use back buffer for painting to
// TO DO: paint a black rectangle as canvas background
// TO DO: draw the stars
// TO DO: draw instructions
// TO DO: flip the buffers
}
} // end of Starfield class
Explanation / Answer
here is your modified code : ------------------>>>>>>>>>>>>>>
public class Starfield extends JFrame implements KeyListener, Runnable {
private static String applicationPath;
private BufferStrategy strategy;
private int windowWidth, windowHeight;
private int maxStarX, maxStarY;
private Image[] imgStar = new Image[3];
private Font myFont;
private static final int NUMSTARS = 50;
// embedded class for handling an individual Star
private class Star {
public double x, y;
public double dx, dy;
public int imgNum;
public void draw(Graphics g) {
g.drawImage(imgStar[imgNum], (int)x, (int)y, null);
}
public void animate() {
// TO DO: move the star by adding dx and dy to x and y
x = x+dx;
y = y+dy;
// TO DO: reverse the star's movement when it hits the JFrame's boundaries
if(x >= windowWidth || x <= 0){
dx = -dx;
}
if(y >= windowHeight || x <= 0){
dy = -dy;
}
}
public void randomise() {
x=(Math.random() * maxStarX);
y = (Math.random() * maxStarY);
dx =Math.random() -Math.random();
dy = Math.random() -Math.random();
imgNum=(int)(Math.random() * 3);
}
}
// end of embedded Star class
private Star[] stars = new Star[NUMSTARS];
private boolean paused = false;
public static void main(String[] args) {
Starfield s = new Starfield(600,600);
}
public Starfield(int w,int h) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
java.io.File currentDir = new java.io.File("");
applicationPath = currentDir.getAbsolutePath();
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - w/2; int y = screensize.height/2 - h/2;
setBounds(x,y,w,h);
setVisible(true);
windowWidth = w;
windowHeight = h;
for (int s=1; s<=3; s++) {
ImageIcon icon =new ImageIcon(applicationPath+"/star_" +s+".png");
imgStar[sT1] = icon.getImage();
}
maxStarX = wTimgStar[0].getWidth(null);
maxStarY = hTimgStar[0].getHeight(null);
myFont = new Font("Courier",Font.BOLD,14);
createBufferStrategy(2);
strategy = getBufferStrategy();
addKeyListener(this);
initialiseStars();
Thread t = new Thread(this);
t.start();
}
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_P)
paused=!paused;
else if (keyCode==KeyEvent.VK_ESCAPE)
System.exit(0);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}
public void run() {
while (1==1) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {}
if (!paused)
animateStarfield();
}
}
private void initialiseStars() {
for (int i=0; i<NUMSTARS; i++) {
stars[i] = new Star();
stars[i].randomise();
}
}
private void animateStarfield() {
// TO DO: animate all stars by calling their animate() method
for(int i = 0;i<NUMSTARS;i++){
stars[i].animate();
}
repaint();
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.drawRect(0,0,windowWidth,windowHeight);
for(int i = 0;i<NUMSTARS;i++){
stars[i].draw(g);
}
Image temp = imgStar[0];
imgStar[0] = imgStar[2];
imgStar[2] = temp;
}
}// end of Starfield class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.