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

In this lab assignment, you are going to develop a small graphics program that c

ID: 673777 • Letter: I

Question

In this lab assignment, you are going to develop a small graphics program that can draw a growing spiral. Each ring on the spiral is composed of 6 straight edges. The spiral grows one edge every some time. Initially, the frequency is one edge per second. It can be changed by using your mouse wheel. In detail, when you are scrolling up, the growing gets faster; vice versa. Once the spiral grows to 10 rings, the whole spiral will be erased from the panel and be redrawn all over again.

Class javax.swing.Timer can be used to repeatedly invoke method actionPerformed of a class that implements ActionListener. Look at the following code:

class ScreenSaver extends JPanel implements ActionListener, MouseWheelListener{

private int x[]=new int[60];

private int y[]=new int[60];

private int numOfPoints=0;

private int radius=10;

private int delay=1000;

private Timer timer=null; // javax.swing.timer

public ScreenSaver(){

timer=new Timer(1000, this); // the interval is 1000 milliseconds

timer.start(); }

public void actionPerformed(ActionEvent e){

/* insert your code here */

repaint(); }

public void paintComponent(Graphics g){

g.clearRect(0,0,(int)getSize().getWidth(),(int)getSize().getHeight());

/* insert your code here */ }

public void mouseWheelMoved(MouseWheelEvent e){

/* * e.getWheelRotation() returns the number of "clicks" the mouse wheel was rotated. * Negative values if the mouse wheel was rotated up/away from the user, * and positive values if the mouse wheel was rotated down/ towards the user */

/* update delay */

}

……

}

lass ScreenSaver extends JPanel and implements ActionListener. Then it has all the instance variables and member methods from JPanel. You can draw on ScreenSaver. Additionally, it declares to implement ActionListener. So it must implement actionPerformed. In the constructor, a Timer object is created. It is going to invoke actionPerformed periodically.

Apparently, we should add new edges in actionPerformed. But should we also draw edges in actionPerformed. The answer is no. You should draw edges in paintComponent(Graphics g). Only this method has the access to the Graphics object.

The logic is actionPerformed generates edges and paintComponent draws the edges.

Hints: 1. Declare the following instance variables in ScreenSaver. private int x[ ]=new int[60]; // each ring is defined by 10 points, then private int y[ ]=new int[60]; // 10 rings would be 60 points private int numOfLines=0; // how many have been generated?

2. After actionPerformed generates a new point, it needs to call repaint() to refresh the window.

3. "double maxX=getSize(null).getWidth();" and “double maxY=getSize(null).getHeight();” can be used to find the center of the panel.

4. When drawing lines in paintComponent, clear the background and draw all the lines. g.clearRect(0,0,(int)getSize().getWidth(),(int)getSize().getHeight());

Use the following code as the main method:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Lab4 extends JFrame{

public Lab4(){

super("Lab 4");

ScreenSaver saver=new ScreenSaver();

setLayout(new BorderLayout());

add(saver, BorderLayout.CENTER);

}

public static void main(String args[]){

Lab4 lab4=new Lab4();

lab4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

lab4.setSize( 200, 210 ); // set frame size

lab4.setResizable(true);

lab4.setVisible( true ); // display frame

}

}

Explanation / Answer

Solution :

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class Lab4 extends JFrame {

public Lab4()

{

super("Lab 4");

ScreenSaver saver=new ScreenSaver();

setLayout(new BorderLayout());

add(saver, BorderLayout.CENTER);

}

public static void main(String args[])

{

Lab4 lab4=new Lab4();

lab4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );

lab4.setSize( 200, 210 ); // set frame size

lab4.setResizable(true);

lab4.setVisible( true ); // display frame

}

}

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