Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a simple Screen Saver program that generates random shapes of random color

ID: 3630394 • Letter: W

Question

Write a simple Screen Saver program that generates random shapes of random colors and sizes at random positions within the display area. Your program should keep track of the number of shapes it has drawn on the screen and after 20 shapes have been drawn, it should clear the screen by calling the base class paintComponent method. You should use the Timer class as illustrated in the SimpleAnimation project discussed in class. Set the timer to fire an ActionEvent once a second. This will cause your actionPerformed method execute, which should simply call the repaint method. 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. Remember to clear the display area after 20 shapes have been created.

So Far i have this but the DrawJPanel is giving me trouble. Is a class for DrawJPanel need? can you show me please

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ScreenSaver extends JFrame
{
// JButton to clear drawingJPanel
private JButton clearJButton;

// DrawJPanel for displaying rectangles
private DrawJPanel drawingJPanel;

// no-argument constructor
public ScreenSaver()
{
createUserInterface();
}

// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();

// enable explicit positioning of GUI components
contentPane.setLayout( null );

// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds( 189, 16, 72, 23 );
clearJButton.setText( "Clear" );
contentPane.add( clearJButton );
clearJButton.addActionListener(

new ActionListener() // anonymous inner class
{
// method called when clearJButton is pressed
public void actionPerformed( ActionEvent event )
{
clearJButtonActionPerformed( event );
}

} // end anonymous inner class

); // end call to addActionListener

// set up drawingJPanel
drawingJPanel = new DrawJPanel();
drawingJPanel.setBounds( 0, 40, 450, 450 );
//drawingJPanel.setBackground( Color.WHITE );
contentPane.add( drawingJPanel );

// set properties of application's window
setTitle( "Screen Saver" ); // set title bar text
setSize( 500, 500 ); // set window size
setVisible( true ); // display window

} // end method createUserInterface

// reset drawingJPanel
private void clearJButtonActionPerformed( ActionEvent event )
{
drawingJPanel.clear();

} // end method clearJButtonActionPerformed

// main method
public static void main( String[] args )
{
ScreenSaver application = new ScreenSaver();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

} // end method main

} // end class ScreenSaver

Explanation / Answer

You can use this one instead:

This will ease you programming.... :)

001 importjavax.swing.*;
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote