Objects and Classes
Introducing Classes and Objects
You will heard the buzzword OOP, or "Object Oriented Programming" before, but maybe you don't know exactly what it means yet. The following examples are supposed to shed some light onto that mystery. Have a look at the following, not object oriented example applet. Click somewhere inside the applet to paint a bubble with random diameter and color. The bubble is defined inside the main class and is totally dependent from every modification you might do to the main class code.
|
//Sourcecode
import java.awt.*;
import java.applet.*;
//bubbles applet without bubble class
public class Project22 extends Applet
{
Image Buffer;
Graphics gBuffer;
public void init()
{
Buffer=createImage(size().width,size().height);
gBuffer=Buffer.getGraphics();
}
public boolean mouseDown(Event evt,int x,int y)
{
drawRandomBubble(x,y);
repaint();
return true;
}
public void drawRandomBubble(int x, int y)
{
int red=(int)(Math.random()*255);
int green=(int)(Math.random()*255);
int blue=(int)(Math.random()*255);
Color randomColor=new Color(red, green, blue);
gBuffer.setColor(randomColor);
//generate a random number between 10 and 100
int diameter=(int)(Math.random()*90)+10;
//make mouse cursor the center of our random bubble
gBuffer.fillOval(x-diameter/2,y-diameter/2,diameter,diameter);
}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{
gBuffer.setColor(Color.black);
gBuffer.drawString("Click the applet to paint bubbles!", 50,20);
g.drawImage (Buffer,0,0, this);
}
}
|
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|