Chapter 7 –Section A Exercise - page 325
import java.applet.*;
import java.awt.*;
public class ch7_1 extends Applet
{
String myName = new String("Shin Liu");
public void paint(Graphics gr)
{
for (int i = 4; i<=24;i++)
{
Font varyFont=new Font("Helvetica",Font.BOLD,i);
gr.setFont(varyFont);
gr.drawString(myName, 100, 10+ i*10);
}
}
}
<html>
<applet code ="ch7_1.class" width = 400 height=300>
</applet>
</html>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class RedBlue extends Applet
implements ActionListener
{
int counter =0;
String name = new String("Shin Liu");
Button aButton = new Button("press ");
Font smallFont = new Font("Helvetica", Font.ITALIC,10);
Font largeFont = new Font("Helvetica", Font.ITALIC,20);
public void init( )
{
add(aButton);
aButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (counter ==0)
{
Graphics gr = getGraphics( );
gr.setFont(smallFont);
gr.setColor(Color.red);
gr.drawString(name,100,100);
counter++;
}
else
{
Graphics gr = getGraphics( );
gr.setFont(smallFont);
gr.setColor(getBackground());
gr.drawString(name,100,100);
gr.setFont(largeFont);
gr.setColor(Color.blue);
gr.drawString(name,100,100);
counter++;
}
}
}
Section B – page 360
1. a Write an applet that display a yellow smiling face on the screen.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Smile extends Applet
{
public void paint(Graphics gr)
{
gr.setColor(Color.yellow);
gr.fillOval(50,50,100,100);
gr.setColor(Color.black);
gr.fillOval(75,75,10,10);
gr.fillOval(100,75,10,10);
gr.drawArc(75,75,50,50,200,140);
}
}
b. Add a Button to the Smile applet so the applet changes to a frown when the user click the button.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Smile extends Applet implements ActionListener
{
Button aButton = new Button("click here");
public void init()
{
add(aButton);
aButton.addActionListener(this);
}
public void paint(Graphics gr)
{
gr.setColor(Color.yellow);
gr.fillOval(50,50,100,100);
gr.setColor(Color.black);
gr.fillOval(75,75,10,10);
gr.fillOval(100,75,10,10);
gr.drawArc(75,75,50,50,200,140);
}
public void actionPerformed(ActionEvent e)
{
Graphics gr = getGraphics();
gr.setColor(Color.yellow);
gr.drawArc(75,75,50,50,200,140);
gr.setColor(Color.black);
gr.drawArc(75,100,50,50,20,140);
}
}
2.
Exercise 2a
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
public
class Fireworks extends Applet
{
int
x1Points[] = {42,52,72,52,60,40,15,28,9,32,42};
int
y1Points[] = {38,62,68,80,105,85,102,75,58,30,38};
int
x2Points[] = {62,72,92,72,80,60,35,48,29,52,62};
int
y2Points[] = {48,72,78,90,115,95,112,85,68,40,48};
public
void paint(Graphics gr)
{
gr.setColor(Color.red);
gr.drawPolygon(x1Points,
y1Points, x1Points.length);
gr.setColor(Color.blue);
gr.drawPolygon(x2Points,
y2Points, x2Points.length);
gr.setColor(Color.green);
gr.drawLine(80,100,150,160);
gr.drawLine(80,40,150,30);
gr.drawLine(80,70,150,130);
gr.drawLine(80,60,150,100);
}
}
Exercise
2b
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
public
class Fireworksb extends Applet implements ActionListener
{
Button
aButton = new Button("Click here");
int
x1Points[] = {42,52,72,52,60,40,15,28,9,32,42};
int
y1Points[] = {38,62,68,80,105,85,102,75,58,30,38};
int
x2Points[] = {62,72,92,72,80,60,35,48,29,52,62};
int
y2Points[] = {48,72,78,90,115,95,112,85,68,40,48};
public
void init()
{
add
(aButton);
aButton.addActionListener(this);
}
public
void actionPerformed(ActionEvent e)
{
Graphics
gr = getGraphics();
gr.setColor(Color.red);
gr.drawPolygon(x1Points,
y1Points, x1Points.length);
gr.setColor(Color.blue);
gr.drawPolygon(x2Points,
y2Points, x2Points.length);
gr.setColor(Color.green);
gr.drawLine(80,100,150,160);
gr.drawLine(80,40,150,30);
gr.drawLine(80,70,150,130);
gr.drawLine(80,60,150,100);
}
}
3.
Exercise 3a
import
java.applet.*;
import
java.awt.*;
public
class Borders extends Applet
{
String
myName = new String("Joyce");
Font
serifItalic = new Font("Serif", Font.ITALIC, 20);
int
leading, ascent, descent, height, width;
int
a, x = 40, y = 60;
int
border = 10;
public
void paint(Graphics gr)
{
for(a
= 1; a < 5; ++a)
{
border
= a * 10;
gr.setFont(serifItalic);
gr.drawString(myName,x,y);
leading
= gr.getFontMetrics().getLeading();
ascent
= gr.getFontMetrics().getAscent();
descent
= gr.getFontMetrics().getDescent();
height
= gr.getFontMetrics().getHeight();
width
= gr.getFontMetrics().stringWidth(myName);
gr.drawRect(x
- border, y - (ascent + leading + border),
width
+ 2 * border, height + 2 * border);
}
}
}
Exercise
3b
import
java.applet.*;
import
java.awt.*;
public
class Bordersb extends Applet
{
String
myName = new String("Joyce");
Font
serifItalic = new Font("Serif", Font.ITALIC, 20);
int
leading, ascent, descent, height, width;
int
a, x = 40, y = 60;
int
border = 10;
public
void paint(Graphics gr)
{
for(a
= 1; a < 5; ++a)
{
border
= a * 10;
if
(a == 1)
gr.setColor(Color.magenta);
else
if (a == 2)
gr.setColor(Color.yellow);
else
if (a==3)
gr.setColor(Color.orange);
else
gr.setColor(Color.blue);
gr.setFont(serifItalic);
gr.drawString(myName,x,y);
leading
= gr.getFontMetrics().getLeading();
ascent
= gr.getFontMetrics().getAscent();
descent
= gr.getFontMetrics().getDescent();
height
= gr.getFontMetrics().getHeight();
width
= gr.getFontMetrics().stringWidth(myName);
gr.drawRect(x
- border, y - (ascent + leading + border),
width
+ 2 * border, height + 2 * border);
}
}
4.
Exercise 4
//
MyFonts.java
//
Chapter 7, Section B Exercise 4
import
java.applet.*;
import
java.awt.*;
public
class MyFonts extends Applet
{
int
x=50, y=50;
int
yoffset = 20;
String
phrase = "I Love Java Programming!";
Font
myFont;
public
void paint( Graphics gr)
{
String
availableFonts[] = Toolkit.getDefaultToolkit().getFontList();
for(int
i = 0; i <availableFonts.length; i++)
{
myFont = new Font(availableFonts[i], Font.PLAIN, 12);
gr.setFont(myFont);
gr.drawString(phrase,
x, y+i*yoffset);
}
}
5.
Exercise 5
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
public
class Yum extends Applet implements ActionListener
{
int
x=0;
Button
aButton = new Button("Click here");
public
void init()
{
setBackground(Color.white);
add
(aButton);
aButton.addActionListener(this);
}
public
void paint(Graphics gr)
{
setBackground(Color.white);
gr.setColor(Color.red);
gr.fillOval(50,50,100,100);
}
public
void actionPerformed(ActionEvent e)
{
Graphics
gr = getGraphics();
gr.setColor(Color.white);
if(x
== 0)
gr.fillOval(125,20,100,100);
else
if(x==1)
gr.fillOval(100,50,100,100);
else
if(x==2)
gr.fillOval(75,75,100,100);
else
if(x==3)
{
gr.fillOval(60,80,100,100);
gr.setColor(Color.black);
gr.drawString("Yum!",100,170);
}
++x;
}
6.
Exercise 6
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
import
java.util.*;
public
class PieChart extends Applet implements ActionListener
{TextField
a1Display = new TextField(5);
TextField
a2Display = new TextField(5);
TextField
a3Display = new TextField(5);
TextField
a4Display = new TextField(5);
TextField
a5Display = new TextField(5);
Label
a1Label = new Label("High Risk");
Label
a2Label = new Label("Medium Risk");
Label
a3Label = new Label("Low Risk");
Label
a4Label = new Label("No Risk");
Label
prompt1 = new Label("Enter 4 dollar amounts for your investment
portfolio");
Label
prompt2 = new Label(" and press ENTER in box 4.");
int
a1;
int
a2;
int
a3;
int
a4;
int
begin = 100;
int
a1Percent;
int
a2Percent;
int
a3Percent;
int
a4Percent;
int
a2Start;
int
a3Start;
int
a4Start;
public
void init()
{add(prompt1);
add(prompt2);
add(a1Label);
add(a1Display);
add(a2Label);
add(a2Display);
}
}
7.
Exercise 7
import
java.applet.*;
import
java.awt.*;
import
java.awt.event.*;
import
java.util.*;
public
class TimeGraph extends Applet implements ActionListener
{TextField
a1Display = new TextField(5);
TextField
a2Display = new TextField(5);
TextField
a3Display = new TextField(5);
TextField
a4Display = new TextField(5);
TextField
a5Display = new TextField(5);
TextField
a6Display = new TextField(5);
TextField
a7Display = new TextField(5);
TextField
a8Display = new TextField(5);
Label
a1Label = new Label("1");
Label
a2Label = new Label("2");
Label
a3Label = new Label("3");
Label
a4Label = new Label("4");
Label
a5Label = new Label("5");
Label
a6Label = new Label("6");
Label
a7Label = new Label("7");
Label
a8Label = new Label("8");
Label
prompt = new Label("Enter 8 daily temperatures and press ENTER in box
8");
int
a1;
int
a2;
int
a3;
int
a4;
int
a5;
int
a6;
int
a7;
int
a8;
public
void init()
{add(prompt);
add(a1Label);
add(a1Display);
add(a2Label);
add(a2Display);
add(a3Label);
add(a3Display);
add(a4Label);
add(a4Display);
add(a5Label);
add(a5Display);
add(a6Label);
add(a6Display);
add(a7Label);
add(a7Display);
add(a8Label);
add(a8Display);
a8Display.addActionListener(this);
}
public
void actionPerformed(ActionEvent e)
{Graphics
g = getGraphics();
a1 =
Integer.parseInt(a1Display.getText());
a2 =
Integer.parseInt(a2Display.getText());
a3 =
Integer.parseInt(a3Display.getText());
a4 =
Integer.parseInt(a4Display.getText());
a5 =
Integer.parseInt(a5Display.getText());
a6 =
Integer.parseInt(a6Display.getText());
a7 =
Integer.parseInt(a7Display.getText());
a8
= Integer.parseInt(a8Display.getText());
g.drawString("1",
100, 215);
g.drawString("2",
130, 215);
g.drawString("3",
160, 215);
g.drawString("4",
190, 215);
g.drawString("5",
220, 215);
g.drawString("6",
250, 215);
g.drawString("7",
280, 215);
g.drawString("8",
310, 215);
g.drawLine(90,
200, 330, 200);
g.drawLine(100,
200 - a1, 130, 200 - a2);
g.drawLine(130,
200 - a2, 160, 200 - a3);
g.drawLine(160,
200 - a3, 190, 200 - a4);
g.drawLine(190,
200 - a4, 220, 200 - a5);
g.drawLine(220,
200 - a5, 250, 200 - a6);
g.drawLine(250,
200 - a6, 280, 200 - a7);
g.drawLine(280,
200 - a7, 310, 200 - a8);
}
}