Menu

[Solved]Consider Following Java Program Spanning 2 Pages Comments Intentionally Removed Produces A Q37272738

Consider the following Java program (spanning 2 pages, commentsintentionally removed), which produces an animated ‘Star-field’using double buffering and threaded animation. Each star is drawnas one of three randomly selected images.

(i) Explain the purpose of using a ‘thread’ to controlanimations such as this, and identify and explain each line of codein this program related to threads [5]

(ii) Write suitable code for each of the following methods(identified by TO DO comments in the code, below):

a) animate() [5]

b) animateStarfield() [5]

c) paint(Graphics g)

Consider the following Java program (spanning 2 pages, comments intentionally removed), which produces an animated Star-fielpublic class Starfield extends 3Frame implenents KeyListener, Runnable private static String applic private BufferStrategy stDinension screensize java.awt.Toolkit.getDefaultToolkit) getscreeize int xscreensize.width/2 w/2; int y - screensize.height/2

Consider the following Java program (spanning 2 pages, comments intentionally removed), which produces an animated ‘Star-field’ using double buffering and threaded animation. Each star is drawn as one of three randomly selected images (i) Explain the purpose of using a ‘thread’ to control animations such as this, and identify and explain each line of code in this program related to threads ii) Write suitable code for each of the following methods (identified by TO DO comments in the code, below): a) animate0 b) animateStarfieldO c) paint(Graphics g) public class Starfield extends 3Frame implenents KeyListener, Runnable private static String applic private BufferStrategy strategy private int windowwidth, windowHeight; private int maxStarX, maxStarY; private Inage] imgStar-new Image[3]; private Font myFont; private static final int NUMSTARS = 50; /I embedded class for handling an individual Star private class Star ( public double x, y public double dx, dy; public int ingNum; public void draw(Graphics g) g.drawImage(ingstar[imgNum], (int)x, (int)y, null); public void animate) I TO DO: move the star by adding dx and dy to x and y /I TO D0: reverse the star’s movement when it hits the JFrame’s boundaries public void randomise) ( X = (Math . random() * maxstarX); y(Math.randommaxStarY) dx = Math.random()-Math, random(); dyMath.random()-Math.random); imgNum (int) (Math.random) 3); // end of enbedded Star class private Star[] stars new Star [NUMSTARS]; private boolean paused-false; public static void main(String[] args) ( Starfield s new Starfield (68e,680) public Starfield(int w, int h) ( setDefaultcloseoperation (JFrame.EXIT_ON CLOSE); java.io.File currentDir-new java.io.File(“”; applicationPath-currentDir.getAbsolutePath (O: Dinension screensize java.awt.Toolkit.getDefaultToolkit) getscreeize int xscreensize.width/2 w/2; int y – screensize.height/2 h/2; setBounds(x, y, , h) setvisible(true) windonhidth ; windonHeight h; for (int s-1; s-3; s*) f InageIcon icon – new InageIcon(applicationPat”Istar”s+pe) ingStar[s-1-icon.get Inage) maxStarx–ingstar[].getwidth(null) maxStarY- h-ingStart ].getHeight (null) yFontnew Font( Courier”, Font.BOLD, 14) createBufferStrategy(2) strategy -getBufferStrategy) addkeyListener(this); initialisestars) Thread t-new Thread (this); t.start; public void keyPressed(KeyEvent e) f int keyCode e.getkeyCode) if 〔keyCode..KeyEvent.VK P) pausedIpaused else if (keyCode–KeyEvent.VK ESCAPE) Systen.exit (e); public void keyReleased(KeyEvent e) public void keyTyped (KeyEvente public void run() ( while (1–1) try t Thread.sleep (28) catch (InterruptedException e)) if (lpaused) aninateStarfield repaint() private void initialisestars) for (int stars[i]-new Star; stars[i].randonise) i-e; ịcNUMSTARS; İ++) { private void aninateStarfield) TO DO: aninate all stars by calling their aninate() nethod public void paint(Graphics e) TO DO: use back buffer for painting to TO 00: paint a black rectangle as canvas background I TO 00: draw the stars I TO 00: draw iInstructions TO DD: flip the buffers end of Starfield class Show transcribed image text Consider the following Java program (spanning 2 pages, comments intentionally removed), which produces an animated ‘Star-field’ using double buffering and threaded animation. Each star is drawn as one of three randomly selected images (i) Explain the purpose of using a ‘thread’ to control animations such as this, and identify and explain each line of code in this program related to threads ii) Write suitable code for each of the following methods (identified by TO DO comments in the code, below): a) animate0 b) animateStarfieldO c) paint(Graphics g)
public class Starfield extends 3Frame implenents KeyListener, Runnable private static String applic private BufferStrategy strategy private int windowwidth, windowHeight; private int maxStarX, maxStarY; private Inage] imgStar-new Image[3]; private Font myFont; private static final int NUMSTARS = 50; /I embedded class for handling an individual Star private class Star ( public double x, y public double dx, dy; public int ingNum; public void draw(Graphics g) g.drawImage(ingstar[imgNum], (int)x, (int)y, null); public void animate) I TO DO: move the star by adding dx and dy to x and y /I TO D0: reverse the star’s movement when it hits the JFrame’s boundaries public void randomise) ( X = (Math . random() * maxstarX); y(Math.randommaxStarY) dx = Math.random()-Math, random(); dyMath.random()-Math.random); imgNum (int) (Math.random) 3); // end of enbedded Star class private Star[] stars new Star [NUMSTARS]; private boolean paused-false; public static void main(String[] args) ( Starfield s new Starfield (68e,680) public Starfield(int w, int h) ( setDefaultcloseoperation (JFrame.EXIT_ON CLOSE); java.io.File currentDir-new java.io.File(“”; applicationPath-currentDir.getAbsolutePath (O:
Dinension screensize java.awt.Toolkit.getDefaultToolkit) getscreeize int xscreensize.width/2 w/2; int y – screensize.height/2 h/2; setBounds(x, y, , h) setvisible(true) windonhidth ; windonHeight h; for (int s-1; s-3; s*) f InageIcon icon – new InageIcon(applicationPat”Istar”s+pe) ingStar[s-1-icon.get Inage) maxStarx–ingstar[].getwidth(null) maxStarY- h-ingStart ].getHeight (null) yFontnew Font( Courier”, Font.BOLD, 14) createBufferStrategy(2) strategy -getBufferStrategy) addkeyListener(this); initialisestars) Thread t-new Thread (this); t.start; public void keyPressed(KeyEvent e) f int keyCode e.getkeyCode) if 〔keyCode..KeyEvent.VK P) pausedIpaused else if (keyCode–KeyEvent.VK ESCAPE) Systen.exit (e); public void keyReleased(KeyEvent e) public void keyTyped (KeyEvente public void run() ( while (1–1) try t Thread.sleep (28) catch (InterruptedException e)) if (lpaused) aninateStarfield repaint() private void initialisestars) for (int stars[i]-new Star; stars[i].randonise) i-e; ịcNUMSTARS; İ++) { private void aninateStarfield) TO DO: aninate all stars by calling their aninate() nethod public void paint(Graphics e) TO DO: use back buffer for painting to TO 00: paint a black rectangle as canvas background I TO 00: draw the stars I TO 00: draw iInstructions TO DD: flip the buffers end of Starfield class

Expert Answer


Answer to Consider the following Java program (spanning 2 pages, comments intentionally removed), which produces an animated ‘Star… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *