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

Java Illuminated - Chapter 4 - 4.7.7 Programming Projects 34. Write an applet th

ID: 3565082 • Letter: J

Question

Java Illuminated - Chapter 4 - 4.7.7 Programming Projects

34. Write an applet that displays two eyes. An eye can be drawn using an
oval, a filled circle, and lines. On the applet, write a word or nyo about
these eyes.


35. Write an applet that displays the following coins: a quarter, a dime, and
a nickel. These three coins should be drawn as basic circles (of different
diameters) with the currency value inside (for instance, "$.25").


36. Write an applet that displays a basic house, made up of lines (a nd
possibly rectangles). Your house should have mult iple colors. On the
applet, give a title to the house (for instance, "Java House").


37. Write an applet that displays a black and red bull 's eye target, typically
made up of several concentric circles.

I am not sure if I am allowed to ask questions in this manner, but I saw one other person do so; so I thought I would try. Please feel free to answer two or three at a time, and whatever you don't get too I might just re-post what is lefts. Thank you in adivce

Explanation / Answer

34. An Java applet that displays two eyes

// FILE: Eyes.java
// A Java applet that draws eyes that follow the mouse when the cursor
// is inside the applet window. Clicking anywhere will place a new eye
// and launch a new thread for that eye's processing.
// Written by: Grant Macklem (Grant.Macklem@colorado.edu)

import java.applet.Applet; // Provides the Applet class.
import java.awt.*; // Provides the Button class, etc.
import java.awt.event.*; // Provides ActionEvent, ActionListener
import java.text.*; // Provides DecimalFormat


/**
* The <code>Eye</code> Java class implements an eye on the screen
* that looks at the mouse pointer. Each eye is a separate thread.
* @author
* Grant Macklem (<a href=mailto:macklem@colorado.edu>macklem@colorado.edu</a>)
**/
class Eye extends Thread
{
/**
* Every time the mouse is moved, this is set to the x-coordinate of the mouse.
**/
public static int mouseX;
/**
* Every time the mouse is moved, this is set to the y-coordinate of the mouse.
**/
public static int mouseY;
private static final int WIDTH = 50; // Eye width, in pixels
private static final int HEIGHT = 75; // Eye height, in pixels
private static final int IRISSIZE = 30; // Iris size, in pixels
private static final int PUPILSIZE = 12; // Pupil size, in pixels
private Color irisColor;

// Radii of the inner ellipse that the iris slides along
private static final int SMALLXRAD = (WIDTH - IRISSIZE)/2;
private static final int SMALLYRAD = (HEIGHT - IRISSIZE)/2;

private int x, y; // Current position of the eye
private double newx, newy; // Position of the iris
private Graphics g; // Graphics context

/**
* Constructs a new eye thread.
* @param x
* The x-coordinate of the eye (in pixels) in the current window.
* @param y
* The y-coordinate of the eye (in pixels) in the current window.
* @param g
* The graphics context to which the eye should be drawn.
**/
public Eye(int x, int y, Graphics g)
{
this.g = g;
this.x = x;
this.y = y;
// Create a random iris color
irisColor = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());
}

private void draw()
{
synchronized(g)
{
// Erase the old eye
g.setColor(Color.white);
g.fillOval(x - WIDTH/2, y - HEIGHT/2, WIDTH, HEIGHT);
// Draw the iris
g.setColor(irisColor);
g.fillOval((int)newx - IRISSIZE/2 + 1, (int)newy - IRISSIZE/2 + 1, IRISSIZE, IRISSIZE);
// Draw the pupil
g.setColor(Color.black);
g.fillOval((int)newx - PUPILSIZE/2 + 1, (int)newy - PUPILSIZE/2 + 1, PUPILSIZE, PUPILSIZE);
// Draw the eye outline
g.drawOval(x - WIDTH/2, y - HEIGHT/2, WIDTH, HEIGHT);
}
}

/**
* Continually calculates where the eye should be and redraws it on the screen
* until the thread is destroyed.
**/
public void run()
{
for(;;)
{
updateCoordinates();
draw();
try
{
// Sleep to reduce flicker
sleep(50);
}
catch (InterruptedException e)
{
       // Continue execution
}
}

}

private void updateCoordinates()
{

if (mouseX == x)
{ // mouse is vertical above eye center
newx = mouseX;

if (Math.abs(y - mouseY) >= SMALLYRAD)
{ // Pointer is outside the eye
if ( (y - mouseY) > 0 )
newy = y - SMALLYRAD;
else
newy = y + SMALLYRAD;
}
else // pointer is in the eye
newy = mouseY;
return;
}

// Find intersection point of line to mouse with eye ellipse
double slope = (double)(mouseY - y) / (double)(mouseX - x);
double numerator = SMALLXRAD * SMALLXRAD * SMALLYRAD * SMALLYRAD;
double denominator = SMALLYRAD * SMALLYRAD + slope * slope * SMALLXRAD * SMALLXRAD;
newx = Math.sqrt(numerator / denominator);
newy = slope * newx;

// Choose appropriate intersection point
if (mouseX < x)
newx = -Math.abs(newx);
else
newx = Math.abs(newx);

if (mouseY < y)
newy = -Math.abs(newy);
else
newy = Math.abs(newy);

newx += x;
newy += y;

if ( (double)(mouseX - x)*(mouseX - x) / (SMALLXRAD * SMALLXRAD) + (double)(mouseY - y)*(mouseY - y) / (SMALLYRAD * SMALLYRAD) < 1 )
{ // Mouse is inside of the eye
newx = mouseX;
newy = mouseY;
}
}
}


/**
* The <code>Eyes</code> Java applet implements eyes on the screen
* that follow the mouse pointer. Each eye is a separate thread.
* There can be a maximum of 50 eyes on the screen.
* @author
* Grant Macklem (<a href=mailto:macklem@colorado.edu>macklem@colorado.edu</a>)
**/
public class Eyes extends Applet
{
// Most comments are for double buffering, which does not help too much
// on my system since the buffers can swap before all threads have
// drawn their eye.
static final int NUM_EYES = 50; // Number of threads
Eye[] eyes = new Eye[NUM_EYES]; // Array of Eyes
int count = -1; // Count of eyes
int width, height; // Height and width of applet
//Image myOffScreenImage; // Second drawing buffer
//Graphics myOffScreenGraphics; // Second graphics context

/**
* Initializes the applet by loading sizes and starting two eye threads.
**/
public void init( )
{
addMouseMotionListener( new MouseMotionListener( ) {
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
Eye.mouseX = e.getX();
Eye.mouseY = e.getY();
repaint();
}
} );
}

/**
* (Re)starts all the eye threads after initializing or having been suspended.
* Start is called once the applet has been placed on the screen.
* It is recalled whenever the browser goes elsewhere and later comes
* back to this applet.
**/
public void start()
{
   if (count == -1)
   { // Nothing has been placed on the screen yet
   width = getSize().width; // Width of the applet
   height = getSize().height; // Height of the applet
   //myOffScreenImage = createImage(width, height);
   //myOffScreenGraphics = myOffScreenImage.getGraphics();
   final Graphics g = getGraphics( );
   //eyes[++count] = new Eye(width/4, height/2, myOffScreenGraphics);
   eyes[++count] = new Eye(width/4, height/2, g);
   eyes[count].start();
   //eyes[++count] = new Eye(3*width/4, height/2, myOffScreenGraphics);
eyes[++count] = new Eye(3*width/4, height/2, g);
   eyes[count].start();  
   addMouseListener( new MouseListener ( ) {
       public void mouseClicked(MouseEvent e) {
       if ((count+1) >= NUM_EYES)
           return;
       //eyes[++count] = new Eye(e.getX(), e.getY(), myOffScreenGraphics);
       eyes[++count] = new Eye(e.getX(), e.getY(), g);
       eyes[count].start();
       }
       public void mousePressed(MouseEvent e) {}
       public void mouseReleased(MouseEvent e) {}
       public void mouseEntered(MouseEvent e) {}
       public void mouseExited(MouseEvent e) {}
   } );
   }
   else
   {
   for (int i = 0; i < count; i++)
       eyes[i].resume();
   }
   repaint();
}

/**
* Pauses all the eye threads after minimizing the applet.
**/
public void stop()
{
for (int i = 0; i < count; i++)
eyes[i].suspend();
}

/**
* Destroys all the eye threads when the applet is exiting..
**/
public void destroy()
{
for (int i = 0; i < count; i++)
eyes[i].stop();
}

/**
* Redraws a border around the applet.
**/
public void update(Graphics g)
{
   g.drawRect(0,0,width-1,height-1);
//paint(myOffScreenGraphics); // Draws on the db
// draws the double buffer onto applet
//g.drawImage(myOffScreenImage,0,0,this);
}
}


35. An java applet that displays the following coins: a quarter, a dime, and a nickel

import java.util.Scanner;

public class CoinChange
{
public static void main(String[] args)
{
double amt;
int cents, quarter, dime, nickle, penny;

Scanner keyboard = new Scanner(System.in);

System.out.print("Change in Coins " +
"---------------- ");

System.out.println("Enter the amount less than $1.00, but " +
"more than zero.");

System.out.print(" Enter amount: ");
amt = keyboard.nextInt();

// ----------------------------------------------

cents = (int)(amt*100 + .1);

quarter = cents/25;
cents %= 25;

dime = cents/10;
cents %= 10;

nickle = cents/5;
cents %= 5;

penny = cents;

// -----------------------------------------

while(amt > 0 && amt <= 1)
{

// -----------------------------------------

if(quarter == 0)
{
System.out.print("no ");
}
else
System.out.print(quarter);
}
if(quarter == 1)
{
System.out.print("quarter");
}
else
{
System.out.print("quarters");
}

}
}

36. An java applet that displays a basic house

import java.awt.*;
import java.applet.*;
  
public class HouseDrawing extends Applet
{
public void paint (Graphics g)
{
background(g);
house (g);
roof1 (g);
roof2 (g);
windows (g);
framing (g);
extras (g);
text (g);
}
  
public void background(Graphics g)
{
setBackground (new Color(65,105,225));
g.setColor (new Color (225,225,225));
g.fillOval (15,35,170,55);
g.fillOval (20,20,160,50);
g.fillOval (350,50,170,55);
g.fillOval (355,35,160,50);   
g.setColor (new Color(225,140,0));  
g.fillOval (650,035,120,120);
}
  
public void house (Graphics g)
{
g.setColor (new Color(139,69,19));   
g.fillRect (100,250,400,200);
g.fillRect (499,320,200,130);
g.setColor(new Color(190,190,190));   
g.fillRect (160,150,60,90);
g.fillRect (245,380,110,70);
g.fillRect (508,350,180,100);
g.setColor (new Color(186,134,11));  
g.fillOval (282,412,10,10);
g.fillOval (307,412,10,10);
  
}
  
public void roof1 (Graphics g)
{
g.setColor(new Color(190,190,190));  
int x[] = {98,300,501};
int y[] = {250,130,250};
g.fillPolygon(x,y,3);
}
  
public void roof2 (Graphics g)
{
g.setColor (new Color(190,190,190));
int x[] = {499,499,700};
int y[] = {320,249,320};
g.fillPolygon(x,y,3);
}
  
  
public void windows (Graphics g)
{
g.setColor (new Color(186,134,11));  
g.fillOval (521,350,68,31);
g.fillOval (606,350,68,31);
g.fillRect (121,261,78,78);
g.fillRect (121,361,78,78);
g.fillRect (401,261,78,78);
g.fillRect (401,361,78,78);
g.fillOval (241,261,118,78);   
g.setColor (new Color(175,238,238));
g.fillRect (125,265,70,70);
g.fillRect (125,365,70,70);
g.fillRect (405,265,70,70);
g.fillRect (405,365,70,70);
g.fillOval (245,265,110,70);
g.fillOval (525,353,60,25);
g.fillOval (610,353,60,25);
}
  
public void framing (Graphics g)
{
g.setColor (new Color(139,69,19));   
g.fillRect (298,380,2,70);
g.fillRect (508,382,180,2);
g.fillRect (508,417,180,2);
g.setColor (new Color(186,134,11));  
g.fillRect (157,265,5,70);
g.fillRect (157,365,5,70);
g.fillRect (437,265,5,70);
g.fillRect (438,365,5,70);
g.fillRect (297,265,5,70);
g.fillRect (125,298,70,5);
g.fillRect (125,398,70,5);
g.fillRect (405,298,70,5);
g.fillRect (405,398,70,5);
g.fillRect (245,298,110,5);
g.fillRect (245,375,110,5);  
g.fillRect (240,375,5,75);
g.fillRect (352,375,5,75);
g.fillRect (508,345,180,5);
g.fillRect (503,345,5,105);
g.fillRect (688,345,5,105);
}
  
public void extras (Graphics g)
{
g.setColor (new Color(210,180,140));  
g.fillOval (160,105,35,45);
g.fillOval (170,95,35,45);
g.fillOval (160,85,35,45);
g.fillOval (170,35,35,45);
g.fillOval (160,25,35,45);
g.fillOval (170,15,35,45);
g.setColor (new Color(105,105,105));  
g.fillRect (508,450,180,150);
g.fillRect (245,450,107,50);
g.fillRect (274,500,50,40);
g.fillRect (274,520,250,45);
}
  
public void text (Graphics g)
{   
g.setColor(new Color(225,0,0));   
g.drawString("House portrait by: Qilmax",390,70);
}
}

37. An java applet that displays a black and red bull 's eye target

This is a simple applet which draws a series of filled, concentric circles alternating red and white, in other words a bullseye.

import java.applet.*;

import java.awt.*;

public class Bullseye extends Applet {

  public void paint(Graphics g) {

    int appletHeight = this.getSize().height;

    int appletWidth = this.getSize().width;

    for (int i=8; i >= 0; i--) {

      if ((i % 2) == 0) g.setColor(Color.red);

      else g.setColor(Color.white);

     

      // Center the rectangle

      int rectHeight = appletHeight*i/8;

      int rectWidth = appletWidth*i/8;

      int rectLeft   = appletWidth/2 - i*appletWidth/16;

      int rectTop    = appletHeight/2 - i*appletHeight/16;

      g.fillOval(rectLeft, rectTop, rectWidth, rectHeight);

    }

}

}

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