Learning Java
Custom Search

Previous Next

Graphics in Applications

We just showed how to use the Graphics object in the paint method for applets. For applications, we use the Graphics object in the paintComponent method.

Here, we will write GImage2.java for the application version of GImage.java and GDrawing2.java for the application version of GDrawing.java. The GText.java applet should be easy to rewrite for an application, so we will leave that to the student.

In the GImage2 application below, you are familiar with most of the coding that goes into an application. The size and the title are not in the HTML code as it was for the applet, instead we place it in the java code.

We want to place the paintComponent in the JPanel, so we made it a separate class, GPaneli. To get our image in the applet we used the following. The getDocumentBase was required for us to pick up our current directory that holds our image.

    picx = getImage(getDocumentBase(), "Sails.jpg");

The getDocumentBase is only available in the Applet and JApplet classes. For the application, to load our image we use a ToolKit class. The getDefaultToolkit lets you obtain a ToolKit since you can't create one.

    Toolkit kit = Toolkit.getDefaultToolkit();
    picx = kit.getImage("Sails.jpg");

We use the GPaneli with its paintComponent to instantiate pane. This pane is added to our JFrame application.

Here is the code for GImage2.

import java.awt.*;
import javax.swing.*;

public class GImage2 extends JFrame {

    public GImage2() {
	super("Image in Application");
	setSize(250, 200);
        GPaneli pane = new GPaneli();
        add(pane);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setVisible(true);
    }

    public static void main(String[] arguments) {
        GImage2 test = new GImage2();
    }
}

class GPaneli extends JPanel {
    Image picx;

    GPaneli() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        picx = kit.getImage("Sails.jpg");
    }

    public void paintComponent(Graphics g) {
        g.drawImage(picx, 1, 1, this);
        g.drawString("Sailing soothes the soul", 50, 140);
    }
}

The code below in GDrawing2.java shows the coding for drawing in an application.

import java.awt.*;
import javax.swing.*;

public class GDrawing2 extends JFrame {

    public GDrawing2() {
	super("Drawing on Application");
	setSize(250, 150);
        GPaneld pane = new GPaneld();
        add(pane);
	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	setVisible(true);
    }

    public static void main(String[] arguments) {
        GDrawing2 test = new GDrawing2();
    }
}

class GPaneld extends JPanel {

    public void paintComponent(Graphics g) {
        g.clearRect(0, 0, 250, 150);
        g.drawLine(1, 1, 50, 60);
        g.drawLine(50, 60, 50, 120);
	g.setColor(Color.blue);
        g.drawLine(50, 120, 130, 80);
	g.setColor(Color.green);
        g.drawRect(5, 10, 110, 90);
	g.setColor(Color.yellow);
        g.fillRect(15, 20, 90, 85);
	g.setColor(Color.red);
        g.fillOval(65, 75, 20, 20);
    }
}

Previous Next

Steps In Learning

Contact us

Copyright © 2008      N. Nelson      All Rights Reserved