Wednesday, September 7, 2011

How to Draw Images into an Applet in Java?

by eturo

Drawing an image to an applet in Java is very easy. You can use a predefined method of the Graphics class of the java.awt package called drawImage(). But first, you need to track where the image is by creating a class variable using the Image class where to store the image. To track the image, you can use the getImage() method where the parameters is the path where the image is. Then, you can now paint or draw the said image using the drawing method mentioned above.

Now, we have here an example which an Applet I made. I draw the Java logo, a center part of the applet window. I added some information which is taken from the net just to show how to draw also string in the applet.

My Applet:



You can copy the source code below and try running it using BlueJ text editor if you are using this (I am using this text editor right now) or you can use any text editor where you can write your Java code.
 

My Program Source Code:

 /* This applet is to demonstrate how to draw or paint image in the Java Applet */

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

public class JavaLogo extends Applet
{
    //create a class variable from the Image class
    Image javaLogo;
    //create a class variable from the Font class
    Font titleFont;
   
    public void init()
    {
        //this is to set the size of the applet window
        setSize(600,600);
        //this is to track where the image is
        javaLogo = getImage(getDocumentBase(),"images/JavaLogo.png");
        //declare the font name, font style and font size of class variable titleFont
        titleFont = new Font("Arial",Font.BOLD + Font.ITALIC,24);
    }
   
    public void paint(Graphics g)
    {
        //this is to draw the image
        g.drawImage(javaLogo,130,30,this);
        //this is to set the Color of the context in the applet using the RGB color syste
        g.setColor(new Color(20,70,150));
        //this is draw a string
        g.drawString("Java is an Object oriented application programming language developed by Sun Microsystems.",50,510);
        g.setColor(new Color(250,70,80));
        g.drawString("Java is a very powerful general-purpose programming language.",50,530);
        g.setColor(new Color(120,70,10));
        g.drawString("It is a stupendous programming language which is not confined to machine applications only.",50,550);
        g.setColor(new Color(200,60,120));
        //this is set the class variable titleFont
        g.setFont(titleFont);
        g.drawString("The Java Logo",230,480);
    }
}


Happy Coding :)

My Simple CSS Web Template

by eturo

I designed this simple webpage using HTML and CSS. This is one my laboratory activity for my Information Technology students. Integrating CSS to HTML is easy to learn.


The trick in designing a simple yet very nice and cool webpage is to divide the different parts of the template. You should first make a draft (a drawing maybe) of your webpage before transforming it to actual (from scratch to actual). Of course you should know the different tags of HTML and the different properties with the appropriate values in CSS. Take note that, a mistake in your stylesheet (CSS) can affect the whole webpage.

Like Us on Facebook

Related Posts Plugin for WordPress, Blogger...