import java.awt.*;
import java.applet.*;
/**
* This class reads PARAM tags from its HTML host page and sets
* the color and label properties of the applet. Program execution
* begins with the init() method.
*/
public class graphic extends Applet
{
/**
* The entry point for the applet.
*/
public void init()
{
//initForm();
//usePageParams();
// TODO: Add any constructor code after initForm call.
Label label1, label2;
label1 = new Label(" Java's Graphics ",Label.CENTER);
label2 = new Label(" (by Lucio Gayosso). ",Label.CENTER);
Font font = new Font("Helvetica", Font.BOLD,14);
label1.setFont(font);
label2.setFont(font);
add(label1);
add(label2);
}
private final String labelParam = "label";
private final String backgroundParam = "background";
private final String foregroundParam = "foreground";
/**
* Reads parameters from the applet's HTML host and sets applet
* properties.
*/
private void usePageParams()
{
final String defaultLabel = "Default label";
final String defaultBackground = "C0C0C0";
final String defaultForeground = "000000";
String labelValue;
String backgroundValue;
String foregroundValue;
/**
* Read the ,
* ,
* and tags from
* the applet's HTML host.
*/
labelValue = getParameter(labelParam);
backgroundValue = getParameter(backgroundParam);
foregroundValue = getParameter(foregroundParam);
if ((labelValue == null) || (backgroundValue == null) ||
(foregroundValue == null))
{
/**
* There was something wrong with the HTML host tags.
* Generate default values.
*/
labelValue = defaultLabel;
backgroundValue = defaultBackground;
foregroundValue = defaultForeground;
}
/**
* Set the applet's string label, background color, and
* foreground colors.
*/
label1.setText(labelValue);
label1.setBackground(stringToColor(backgroundValue));
label1.setForeground(stringToColor(foregroundValue));
this.setBackground(stringToColor(backgroundValue));
this.setForeground(stringToColor(foregroundValue));
}
/**
* Converts a string formatted as "rrggbb" to an awt.Color object
*/
private Color stringToColor(String paramValue)
{
int red;
int green;
int blue;
red = (Integer.decode("0x" + paramValue.substring(0,2))).intValue();
green = (Integer.decode("0x" + paramValue.substring(2,4))).intValue();
blue = (Integer.decode("0x" + paramValue.substring(4,6))).intValue();
return new Color(red,green,blue);
}
/**
* External interface used by design tools to show properties of an applet.
*/
public String[][] getParameterInfo()
{
String[][] info =
{
{ labelParam, "String", "Label string to be displayed" },
{ backgroundParam, "String", "Background color, format \"rrggbb\"" },
{ foregroundParam, "String", "Foreground color, format \"rrggbb\"" },
};
return info;
}
Label label1 = new Label();
/**
* Intializes values for the applet and its components
*/
void initForm()
{
this.setBackground(Color.lightGray);
this.setForeground(Color.black);
label1.setText("label1");
this.setLayout(new BorderLayout());
this.add("North",label1);
}
public void paint(Graphics g)
{
doGradientFill(g, 300,150); // Call to 'doGradientFill()' method passing parameters: g,nWinWidth, and nWinHeight values
g.drawLine(10,165,250,230); // Draw line from (10,115) to (270,180)
g.drawRect(275,75,70,80); // Draw empty rectangle
g.fillRect(275,165,70,80); // Draw filled rectangle
g.drawRoundRect(355,75,70,80,20,20); // Draw empty rounded rectangle
g.drawRoundRect(355,165,70,80,30,40); // Draw filled rounded rectangle
g.drawRoundRect(360,255,50,50,50,50); // Draw filled rounded rectangle with same width and height values for rectangle and borders
g.draw3DRect(435,75,60,80,false); // Draw an empty 3D rectangle
g.fill3DRect(435,165,50,60,true); // Draw a filled 3D rectangle
int x[] = {5,25,150,250,200}; // Initialization of arguments
int y[] = {235,285,295,335,285}; // required to draw
int nPoints = x.length; // a polygon
g.drawPolygon(x, y, nPoints); // Draw a polygon
// Use of the second method to draw a polygon to create a 'thick' line
Polygon polygon = new Polygon();
polygon.addPoint(5,400);
polygon.addPoint(6,401);
polygon.addPoint(250,403);
polygon.addPoint(251,401);
g.drawPolygon(polygon);
//********************************************************************
// Initialize arguments for oval
int x1=260;
int y1=325;
int w1=150;
int h1=75;
g.drawOval(x1,y1,w1,h1); //Draw an empty oval
g.fillArc(415,325,150,75,200,275); // Draw a filled arc
g.copyArea(275,75,100,50,185,175); // Copy the rectangle defined by (275,25,100,50) at (250,0)from the rectangle's origin
//Strings to draw text on the applet
g.setColor(Color.red);
g.drawString("(1)",20,90);
g.drawString("(2)",300,90);
g.drawString("(3)",385,90);
g.drawString("(4)",460,90);
g.drawString("(5)",20,195);
g.drawString("(6)",20,325);
g.drawString("(7)",20,425);
g.drawString("(8)",320,425);
g.drawString("(9)",485,425);
g.drawString("(10)",485,290);
}
public void doGradientFill(Graphics g, int nWinWidth, int nWinHeight)
{
int nStep = (int)((double) nWinWidth / 256.0); // 'Double' type is required because nWinWidth is an integer that will produce a result type of double.
int nX = 10;
for (int i=255; i>=0; i--)
{
Color color = new Color(0, i, 0); // 'i' assigs values to the 'blue' component
g.setColor(color);
for (int j=0; j