SOLUTIONJUG.COMCLICK HERE TO GET THE SOLUTION !!!!!COMP 274 - Week 5 Programming AssignmentWrite a Screen Saver program th...
14. When the „c‟ key is pressed, your KeyListener must change the color of the background of thedisplay to a random color....
public void keyPressed(KeyEvent e){// Terminate the program.System.exit(0);}}public static void main(String[] args){FullSi...
Upcoming SlideShare
Loading in …5
×

Comp 274 week 5 programming assignment

177 views
130 views

Published on

Write a Screen Saver program that generates random shapes of random colors and sizes at random positions within the display area. Add controls so typing different keys on the keyboard cause the window to change size and background color as described below.
Requirements:
You can NOT use loops of any kind in this program.
Your member variables must be private.
Your screen saver class must inherit from the JPanel class and it must have a JFrame member variable.
Your program must keep track of the number of shapes it draws on the screen and after 25 shapes have been drawn, it must clear the screen. This is most easily done by calling the base class (JPanel’s) paintComponent method.
You should use the Timer object to generate ActionEvents to drive your Screen Saver. Set the timer to fire an ActionEvent once a second. This will cause your actionPerformed method execute, which should simply call the repaint method. Calling the repaint method causes your paintComponent method to execute.
Your paintComponent method will do all the work to select a random color, random shape and random position within the boundaries of your panel, and draw that shape, keeping count of the number of shapes which have been drawn.
Create a private method that returns a random color whenever called that can be any possible random color value. This method will be called from your paintComponent method and from your KeyListener event handler as described below.
Your paintComponent method must use at least four different shapes that you randomly select from… oval, rectangle, filled ovals, filled rectangles, polygons, lines, etc.
The size and position of the shapes that you draw must vary based on the size of the screen. At least part of each shape drawn must be visible on the screen.
Make your screen saver the full size of the screen.
Add a KeyListener to your frame. The KeyListener must handle KeyPressed events.
Your KeyListener must call System.exit when the ‘e’ key is pressed.
When the ‘s’ key is pressed, your KeyListener must change the size of the window from full screen size to half size (half the width and height of the screen), or from half size back to full size. That is, each time the ‘s’ key is pressed the size of the display toggles between full and half size. This requires changing the size of the frame and calling repaint. HINT: Remember, in order to get the background to be redrawn, you will need to set count back to 0.
When the ‘c’ key is pressed, your KeyListener must change the color of the background of the display to a random color. This means it must get a new random color and call the setBackground with the new random color. It must then call repaint as well. HINT:

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
177
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Comp 274 week 5 programming assignment

  1. 1. SOLUTIONJUG.COMCLICK HERE TO GET THE SOLUTION !!!!!COMP 274 - Week 5 Programming AssignmentWrite a Screen Saver program that generates random shapes of random colors and sizes at randompositions within the display area. Add controls so typing different keys on the keyboard cause thewindow to change size and background color as described below.Requirements:1. You can NOT use loops of any kind in this program.2. Your member variables must be private.3. Your screen saver class must inherit from the JPanel class and it must have a JFrame membervariable.4. Your program must keep track of the number of shapes it draws on the screen and after 25 shapeshave been drawn, it must clear the screen. This is most easily done by calling the base class(JPanel‟s) paintComponent method.5. You should use the Timer object to generate ActionEvents to drive your Screen Saver. Set the timerto fire an ActionEvent once a second. This will cause your actionPerformed method execute, whichshould simply call the repaint method. Calling the repaint method causes your paintComponentmethod to execute.6. Your paintComponent method will do all the work to select a random color, random shape andrandom position within the boundaries of your panel, and draw that shape, keeping count of thenumber of shapes which have been drawn.7. Create a private method that returns a random color whenever called that can be any possiblerandom color value. This method will be called from your paintComponent method and from yourKeyListener event handler as described below.8. Your paintComponent method must use at least four different shapes that you randomly selectfrom… oval, rectangle, filled ovals, filled rectangles, polygons, lines, etc.9. The size and position of the shapes that you draw must vary based on the size of the screen. Atleast part of each shape drawn must be visible on the screen.10. Make your screen saver the full size of the screen.11. Add a KeyListener to your frame. The KeyListener must handle KeyPressed events.12. Your KeyListener must call System.exit when the „e‟ key is pressed.13. When the „s‟ key is pressed, your KeyListener must change the size of the window from full screensize to half size (half the width and height of the screen), or from half size back to full size. That is,each time the „s‟ key is pressed the size of the display toggles between full and half size. Thisrequires changing the size of the frame and calling repaint. HINT: Remember, in order to get thebackground to be redrawn, you will need to set count back to 0.
  2. 2. 14. When the „c‟ key is pressed, your KeyListener must change the color of the background of thedisplay to a random color. This means it must get a new random color and call the setBackgroundwith the new random color. It must then call repaint as well. HINT: Remember, in order to get thebackground to be redrawn, you will need to set count back to 0.The following example program illustrates how to create a frame the size of the screen and how tokill the program when any key is pressed.import javax.swing.*;import java.awt.*;import java.awt.event.*;public class FullSize extends JPanel{private JFrame frame = new JFrame(“FullSize”);private Rectangle rectangle;boolean full;FullSize(){// Remove the title bar, min, max, close stuffframe.setUndecorated(true);// Add a Key Listener to the frameframe.addKeyListener(new KeyHandler());// Add this panel object to the frameframe.add(this);// Get the dimensions of the screenrectangle = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();// Set the size of the frame to the size of the screenframe.setSize(rectangle.width, rectangle.height);frame.setVisible(true);// Remember that we are currently at full sizefull = true;}// This method will run when any key is pressed in the windowclass KeyHandler extends KeyAdapter{
  3. 3. public void keyPressed(KeyEvent e){// Terminate the program.System.exit(0);}}public static void main(String[] args){FullSize obj = new FullSize();}}Screenshot 1Screenshot 2Screenshot 3CLICK HERE TO GET THE SOLUTION !!!!!

×