Applets and More 5, GridFiller Applets Write an applet that displays a 4 X 4 gri
ID: 3548850 • Letter: A
Question
Applets and More
5, GridFiller Applets
Write an applet that displays a 4 X 4 grid. When the user clicks on a square in the grid, the applet should draw a filled circle in it. If the square already has a circle, clicking on it should cause the circle to disappear.
9, Stopwatch Applet
Write an applet that simulates a stopwatch. It should have a Start button and a Stop button. When the Start is clicked the applet should count the seconds that pass. When the Stop button is clicked, the applet should stop counting seconds.
Explanation / Answer
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class GridDrawingApplet extends Applet {
public void paint (Graphics g)
{
// Draw the vertical lines:
g.drawLine (0,0, 0,240);
g.drawLine (30,0, 30,240);
g.drawLine (60,0, 60,240);
g.drawLine (90,0, 90,240);
g.drawLine (120,0, 120,240);
g.drawLine (150,0, 150,240);
g.drawLine (180,0, 180,240);
g.drawLine (210,0, 210,240);
g.drawLine (240,0, 240,240);
// Draw the horizontal lines:
g.drawLine (0,0, 240,0);
g.drawLine (0,30, 240,30);
g.drawLine (0,60, 240,60);
g.drawLine (0,90, 240,90);
g.drawLine (0,120, 240,120);
g.drawLine (0,150, 240,150);
g.drawLine (0,180, 240,180);
g.drawLine (0,210, 240,210);
g.drawLine (0,240, 240,240);
}
}
Alternate way of drawing an applet
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class GridDrawingAppletForLoop extends Applet {
public void paint (Graphics g)
{
// Draw the vertical lines:
for (int x=0; x<=240; x+=30) {
g.drawLine (x,0, x,240);
}
// Draw the horizontal lines:
for (int y=0; y<=240; y+=30) {
g.drawLine (0,y, 240,y);
}
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class BouncingBallApplet2 extends Applet implements Runnable, MouseListener {
int delay;
int frameNumber;
Thread animatorThread;
public void init ()
{
delay = 500;
animatorThread = null;
// Need to tell Java to call "us" when a mouseclick occurs.
addMouseListener (this);
}
public void start ()
{
frameNumber = 0;
if (animatorThread == null) {
animatorThread = new Thread (this);
}
animatorThread.start ();
}
public void stop ()
{
animatorThread = null;
}
public void run ()
{
while (animatorThread != null) {
frameNumber ++;
// Draw next frame.
repaint ();
// Delay
try {
Thread.sleep (delay);
}
catch (InterruptedException e) {
break;
}
}
}
public void paint (Graphics g)
{
// Set foreground and background colors.
setBackground (Color.white);
g.setColor (Color.blue);
// Here's how to get the size of the applet bounds within the program:
// d.width is the width, d.height is the height.
Dimension d = getSize();
if (frameNumber % 2 == 1) {
// On odd frames, draw the ball at the top.
g.fillOval (0, 0, d.width/2, d.height/2);
}
else {
// On even frames, draw the ball at the bottom.
g.fillOval (0, d.height/2, d.width/2, d.height/2);
}
}
// This method gets called when mouse click occurs.
public void mouseClicked (MouseEvent e)
{
// Draw a small red dot where the click occurred.
int x = e.getX ();
int y = e.getY ();
Graphics g = getGraphics ();
g.setColor (Color.red);
g.fillOval (x-5, y-5, 10, 10);
// If no thread is running, we'll create one.
if (animatorThread == null) {
start ();
}
else {
stop ();
}
}
// We need to have these methods, but
// don't actually have to do anything inside.
public void mouseEntered (MouseEvent m) {}
public void mouseExited (MouseEvent m) {}
public void mousePressed (MouseEvent m) {}
public void mouseReleased (MouseEvent m) {}
}
9.import java.awt.event.*;
import javax.swing.*;
public class StopWatch extends JLabel
implements MouseListener, ActionListener {
private long startTime; // Start time of stopwatch.
// (Time is measured in milliseconds.)
private boolean running; // True when the stopwatch is running.
private Timer timer; // A timer that will generate events
// while the stopwatch is running
public StopWatch() {
// Constructor.
super(" Click to start timer. ", JLabel.CENTER);
addMouseListener(this);
}
public void actionPerformed(ActionEvent evt) {
// This will be called when an event from the
// timer is received. It just sets the stopwatch
// to show the amount of time that it has been running.
// Time is rounded down to the nearest second.
long time = (System.currentTimeMillis() - startTime) / 1000;
setText("Running: " + time + " seconds");
}
public void mousePressed(MouseEvent evt) {
// React when user presses the mouse by
// starting or stopping the stopwatch. Also start
// or stop the timer.
if (running == false) {
// Record the time and start the stopwatch.
running = true;
startTime = evt.getWhen(); // Time when mouse was clicked.
setText("Running: 0 seconds");
if (timer == null) {
timer = new Timer(100,this);
timer.start();
}
else
timer.restart();
}
else {
// Stop the stopwatch. Compute the elapsed time since the
// stopwatch was started and display it.
timer.stop();
running = false;
long endTime = evt.getWhen();
double seconds = (endTime - startTime) / 1000.0;
setText("Time: " + seconds + " sec.");
}
}
public void mouseReleased(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
} // end StopWatchRunner
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.