Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 text in transcribed

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

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 Consider the following Java program (spanning 2 pages, comments intentionally removed), which produces an animated 'Star-field' using double

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions