Download public void paint(Graphics g)

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Life Cycle of an Applet,
along with response to mouse
events and other simple
features.
(ps:only two possible examples for life circle of
an Applet)
Demonstrating the Life Cycle
of an Applet: Simple0
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Simple0 extends Applet {
StringBuffer buffer;
public void init() {
buffer = new StringBuffer();
addItem("initializing... "); }
public void start() {
addItem ("starting... ");
}
public void stop() {
addItem("stopping... "); }
public void destroy() {
addItem("preparing for unloading...");
void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint() }
}
public void paint(Graphics g)
{
g.setColor(new Color(200, 1, 200));
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
//Draw the current string inside the rectangle
g.setColor(Color.blue);
g.drawString(buffer.toString(), 5, 15);
}
}
Demonstrates Life Cycle of
an Applet + Mouse Events:
Simple1
import java.awt.*;
import java.awt.event.*;
public class Simple1 extends Applet {
StringBuffer buffer;
public void init() {
addMouseListener(new Bob());
buffer = new StringBuffer();
addItem("initializing... ");
}
public void start() {
addItem("starting... ") ; }
public void stop() {
addItem("stopping... "); }
public void destroy() {
addItem("preparing for unloading...");
void addItem(String newWord) {
System.out.println(newWord);
buffer.append(newWord);
repaint(); }
}
public void paint(Graphics g) {
g.setColor(new Color(200, 1, 200));
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
g.setColor(Color.blue);
g.drawString(buffer.toString(), 5, 15);
}
private class Bob implements MouseListener {
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
public void mousePressed(MouseEvent event) { }
public void mouseReleased(MouseEvent event) { }
public void mouseClicked(MouseEvent event) {
addItem("click!... ");
}
}
}
Three simple applet examples
A method which returns the volume of a cube
import java.awt.*;
import java.applet.Applet;
public class volume extends Applet {
public void paint(Graphics g) {
float answer = areaCube(7,4,2);
g.drawString("Area of cube is " +answer, 100, 100); }
private float areaCube(float height, float width, float depth) {
float area = height*width*depth; return area;
}
}
Draws a circle based on a given center position and
radius
import java.awt.*;
import java.applet.Applet;
public class DrawCircle extends Applet {
public void paint(Graphics g) {
circle(g, 150, 135, 120);
//call circle function
}
//end function
//function draws a circle
private void circle(Graphics g, int xCentre, int yCentre, int radius)
{
int horiz = xCentre - radius; //finds starting horizon. position
int vert = yCentre - radius; //finds starting vertical position
int diameter = radius * 2; //finds diameter
g.drawOval(horiz, vert, diameter, diameter);
}
//end function
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
Drawing Houses
public class drawHouse extends Applet {
public void paint(Graphics g) {
int wall=50;
//allows you to change the size of the house and have them still be 10 pixels apart
drawStreet(g,wall,20,100);
drawStreet(g,wall,20+wall+10,100);
drawStreet(g,wall,20+wall*2+20,100);
drawStreet(g,wall,20+wall*3+30,100);
}
private void drawStreet(Graphics g, int wallHeight, int bottomX, int bottomY)
{
g.drawRect(bottomX, bottomY, wallHeight, wallHeight);
g.drawLine(bottomX+wallHeight, bottomY, bottomX+wallHeight/2,
bottomY-wallHeight/2);
g.drawLine(bottomX+wallHeight/2, bottomY-wallHeight/2, bottomX,
bottomY);
} }
A more complex applet example
Converts temperature Celsius to Fahrenheit within 100
degrees with a scrollbar
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
public class Temp extends Applet implements AdjustmentListener {
private Scrollbar ConvertCelc;
//scrollbar named arbitrarily as ConvertCelc
private float ConvertCelcValue = 0;
//ConvertCelcValue is a float because of the transformation formula
public void init() {
Label title1;
title1 = new Label("degrees C . . . to . . . degrees F");
//label scrollbar
add(title1);
ConvertCelc= new Scrollbar (Scrollbar.HORIZONTAL, 0, 1, 0, 101);
add(ConvertCelc);
ConvertCelc.addAdjustmentListener (this);
} //end init
public void paint(Graphics g) {
float cCF;
cCF = ((ConvertCelcValue*1.8f) +32);
//calculate the conversion
g.drawString("Celcius = “ +ConvertCelctValue + “ Fahrenheit =“ +cCF
, 0,
75);
//draw text
}
//end paint
public void adjustmentValueChanged(AdjustmentEvent e) {
ConvertCelcValue = (float) ConvertCelc.getValue();
//slider gets value of conversion and placement of bar
repaint();
}
//end adujustmentValueChanged
} //end all
Related documents