Docsity

Java Graphics - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Toggle sidebar
bg1
Web Design & Development CS-506
- 217 -
Lecture 18
Java Graphics
Painting
Window is like a painter’s canvas. All window paints on the same surface. More
importantly, windows don’t remember what is under them. There is a need to repaint
when portions are newly exposed.
Java components are also able to paint themselves. Most of time, painting is done
automatically. However sometimes you need to do drawing by yourself.
How painting works?
Let’s take windows example. Consider the following diagram in which the blue area is
representing the desktop. The one frame (myApp) is opened in front of desktop with
some custom painting as shown below.
myApp consist of a JPanel. The JPanel contains a JButton. Two rectangles, a circle & a
lines are also drawn on the JPanel.
Java components
paint themselves
Anything else is
programmer responsibility
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Graphics - Web Design and Development - Lecture Handouts and more Lecture notes Web Design and Development in PDF only on Docsity!

Lecture 18

Java Graphics

Painting

Window is like a painter’s canvas. All window paints on the same surface. More importantly, windows don’t remember what is under them. There is a need to repaint when portions are newly exposed.

Java components are also able to paint themselves. Most of time, painting is done automatically. However sometimes you need to do drawing by yourself.

How painting works?

Let’s take windows example. Consider the following diagram in which the blue area is representing the desktop. The one frame (myApp) is opened in front of desktop with some custom painting as shown below.

myApp consist of a JPanel. The JPanel contains a JButton. Two rectangles, a circle & a lines are also drawn on the JPanel.

Java components paint themselves

Anything else is programmer responsibility

After opening notepad and windows explorer window, diagram will look like this:

Next, JPanel forwards repaint event to JButton that causes the button to be displayed in its original form.

This is all done automatically and we cannot feel this process cause of stunning speed of modern computers that performs all these steps in flash of eye.

Painting a Swing Component

Three methods are at the heart of painting a swing component like JPanel etc. For instance, paint() gets called when it's time to render -- then Swing further factors the paint() call into three separate methods, which are invoked in the following order:

— protected void paintComponent(Graphics g) — protected void paintBorder(Graphics g) — protected void paintChildren(Graphics g)

Lets look at these methods in order in which they get executed

ƒ paintComponet( )

— it is a main method for painting — By default, it first paints the background — After that, it performs custom painting (drawing circle, rectangles etc.)

ƒ paintBorder( )

— Tells the components border (if any) to paint. — It is suggested that you do not override or invoke this method

ƒ paintChildern( )

— Tells any components contained by this component to paint themselves — It is suggested that you do not override or invoke this method too.

Example Code 18.1:

Suppose, we want to draw one circle & rectangle and a string “Hello World”.

The first step is building a class that inherits from JPanel. The following class MyPanel is fulfilling this requirement. paintComponent( ) method is also override in this class. The sample code is given below

// importing required packages import javax.swing.; import java.awt.;

// extending class from JPanel public class MyPanel extends JPanel {

// overriding paintComponent method public void paintComponent(Graphics g){ // erasing behaviour – this will clear all the // previous painting super.paintComponent(g);

// Down casting Graphics object to Graphics2D Graphics2D g2 = (Graphics2D)g;

// drawing rectanle g2.drawRect(20,20,20,20);

// changing the color to blue g2.setColor(Color.blue);

// drawing filled oval with color i.e. blue g2.fillOval(50,50,20,20);

// drawing string g2.drawString("Hello World", 120, 50); } // end paintComponent } // end MyPanel class

The Test class that contains the main method as well uses MyPainel (previously built) class is given below

// importing required packages import javax.swing.; import java.awt.;

public class Test {

JFrame f;

// declaring Reference of MyPanel class MyPanel p;

// parameter less constructor public Test(){

f = new JFrame(); Container c = f.getContentPane();

c.setLayout(new BorderLayout());

// instantiating reference p = new MyPanel();

// adding MyPanel into container c.add(p);

f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} // end constructor

// main method public static void main(String args[ ]){

Test t = new Test();

}

} // end MyPanel class

Note: Here we have used only some methods (drawRect( ) & fillOval( ) etc. ) of Graphics class. For a complete list, see the Java API documentation