Declaring Objects

In this example we will look at how we can declare our own objects by looking at a simple example. In particular we will declare colour and font objects.

import java.applet.Applet;
import java.awt.*;

public class ObjectDemo extends Applet
{

Color color1, color2, color3; //Declare some objects
Font font1;
public void init()
{
//Use the init method to set up the colours and fonts we will use

setBackground(Color.yellow);
color1 = new Color(255,50, 50);
color2 = new Color(50, 255,50);
color3 = new Color(50, 50, 255);
font1 = new Font("SansSerif", Font.ITALIC, 14);


}
public void paint (Graphics g)
{
//The paint method is used to produce the graphics with the colours and font objects

g.setColor(color1);
g.drawRect(20,20,120,60);
g.setColor(color2);
g.fillOval(20,20,120,60);
g.setColor (color3);
g.setFont(font1);
g.drawString("Hello World", 50, 50);

}

}

This is the ouput

ObjectDemo.jpg
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License