(20) Halloween is coming, and so are ghosts! Write a complete program that draws a ghost that looks like the one at left by putting a filled circle on top of a filled rectangle. The ghost should also have two eyes in its head. Make the ghost appear or disappear by adding a button labeled labeled "(in)visible". When you hit that button the ghost should appear if it was invisible and disappear if it was visible. The eyes should always remain visible. Point values: 10 points for the ghost, 4 points for the layout and connecting the button to the applet, 4 points for making the ghost appear and disappear, 3 points for keeping the eyes visible.
Solution starts here
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/**
* Author: Me
* File: Ghost.java (displayed in midterm.html)
* Purpose: Answer to midterm question
* Description: draw a small ghost in the form or a circle sitting on top of
* a rectangle. The ghost has two eyes (above the rectangle). When hit a
* button, the ghost goes invisible, but the eyes always remain visible.
**/
public class Ghost extends Applet implements ActionListener
{
/**
* The invisibility button
**/
Button invis;
/**
* If true, then the ghost will be visible
**/
boolean vis=true;
/**
* Respond to button pushes. Just changes the state of vis.
**/
public void actionPerformed(ActionEvent ae)
{
if (vis)
vis=false;
else
vis=true;
repaint();
}
/**
* Set up the interface. Mostly thisis just putting in the invis button
**/
public void init()
{
setLayout(new GridLayout(6,1));
invis = new Button("(in)Visible");
add(invis);
invis.addActionListener(this);
}
/**
* Do the actual work of drawing the ghost (or not)
**/
public void paint(Graphics g)
{
if (vis)
{
// the ghost
g.setColor(Color.green);
g.fillRect(10,115,30,45);
g.fillOval(10, 100, 30, 30);
}
// the eyes
g.setColor(Color.black);
g.fillOval(20, 108, 4, 3);
g.fillOval(26, 108, 4, 3);
}
}
(20) Write a program that writes a number, sarting in the upper left corner of the display and going down and to the right until it reaches the botton, or left side, of the display. If at the left side, just stop. If at the bottom, continue moving to the left but start going up. Bounce off the top or bottom until reaching the left wall. The image at left shows what a the program might look like after 10 button hits. The button controls when the screen is redrawn. Steps in the X direction should be fixed in size at 20. Steps in the Y direction should be fixed at 55. The number drawn should be the number of times the button has been hit. Partial credit will be as follows: the interface and attaching it correctly to the program, 5 points; printing the number of button hits (instead of just some number), 5 points; the number just going down and to the right after a button hit; 5 points; the number able to "bounce" off the bottom and top of the screen, 5 points.
Recall that to get the size of an applet you can put the following lines into your paint method:
int appX = (int) getSize().getWidth();
int appY = (int) getSize().getHeight();
Solution starts here
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/**
* AUTHOR: me
* File: Angles.java displayed in midterm.html
* Purpose: question answer from midterm
* Description: displays the button count going diagonally down the applet.
* If the number reaches the bottom, bounce and start going up. Then at
* top go back down, etc until you reach the right side of the applet.
* Control the angle of descent with a scrollbar.
**/
public class Angles extends Applet implements ActionListener
{
/**
* the number of button hits
**/
int bc=0;
/**
* the button. Redraw everything when hit.
**/
Button b;
/**
* The scrollbar that controls the angle of descent of the button count
* on the applet.
**/
Scrollbar s;
/**
* Respond to a button hit. Just increment the button counter and repaint
**/
public void actionPerformed(ActionEvent ae)
{
bc = bc + 1;
repaint();
}
/**
* Set up the layout
**/
public void init()
{
setLayout(new GridLayout(10, 1));
s = new Scrollbar(Scrollbar.HORIZONTAL, 20,1,20,50);
add(s);
b = new Button("Do it!");
add(b);
b.addActionListener(this);
}
/**
* Do the drawing
**/
public void paint(Graphics g)
{
int ymin = (int)(getSize().getHeight()*2/10) + 10; // this is the extra credit portion. Go down in the applet 2/10 of the way before starting to draw because my grid layout has 10 rows and I used 2 of them. The go down another 10 to leave space for drawing characters.
int x = 0;
int y = ymin;
int xinc = 20;
int yinc = s.getValue();
int direction = 1; // controls whether number are going up (negative) or down (positive).
while (x < getSize().getWidth())
{
g.drawString(""+bc, x, y);
x = x + xinc;
y = y + direction*yinc;
if (y>getSize().getHeight())
{
// below the bottom
y = (int)getSize().getHeight();
direction=-1;
}
if (y < ymin)
{
// above the top
y = ymin;
direction = 1;
}
}
}
}