You are on page 1of 14

AWT Graphics

Simon Kroly
simon.karoly@codespring.ro
Introduction

Graphical interface from the programmer's viewpoint abstract


graphical elements element properties represented by data structures
Concepts: geometrical shapes, surfaces, colors, textures, lights, positions,
dimensions, fonts etc.
The interface displayed for the user: graphical views, images displayed
on some output devices (typically the monitor), composed of pixels
having different colors.
The image is created based on the abstract model, but the it is also
influenced by other factors (the properties of the output device, e.g.
resolution etc.)
Rendering: the process of creating the image based on the abstract
model (internal representation).
A digital image (raster image, bitmap) is created based on the model.
It is realized using a native mechanism (the concrete visualization is platform-
dependent).

2016. 07. 11. 15:36 AWT grafika s Applet 2


Introduction

E.g. a button (with given properties) a rectangle filled with pixels


having a background color + a label displayed on the screen according
to the current resolution.
Usually, it is sufficient to use standard platform components (provided by
the graphical toolkits) for creating GUIs, but in some cases custom
components are also needed (e.g. a component has to be customized by
placing graphical elements on its surface)
E.g. custom graphical contents can be created using the Canvas
component from the AWT package.
There are several Java APIs for creating graphical contents: Java 2D API,
Java 3D API, JOGL, JMonkey etc.

2016. 07. 11. 15:36 AWT grafika s Applet 3


The Graphics class

Graphics abstract base class: the abstract internal representation of


graphical contents.
Using this class custom graphical contents can be created, extending GUI
components.
The properties needed to perform rendering operations (shape, color,
font, size, position etc.) are represented by Graphics objects, and can be
customized using these objects.
The class is abstract, the rendering process is platform-dependent, it is
realized by native implementations. Different implementations are
provided by JVMs supporting different platforms. These implementations
are "cooperating" with the native window system.

2016. 07. 11. 15:36 AWT grafika s Applet 4


The Graphic class

Methods are provided for drawing different shapes, for defining different
properties. E.g.:
void setColor(Color c)
void drawRect(int x, int y, int width, int height)
void fillRect(int x, int y, int width, int height)
setFont, drawOval, drawPolygon, drawString, drawImage etc.
The Graphics2D class is derived from the Graphics base class. Several
additional methods are provided by this extension (e.g. for more efficient
color handling, transformations etc.).

2016. 07. 11. 15:36 AWT grafika s Applet 5


Updating AWT components

System-triggered painting: component update is initiated by the window


system. The component is visualized for the first time, it is resized or it is
"damaged" (it has been overlapped by another component, it has been
minimized and restored etc.) and needs to be repainted.
Application-triggered painting: component update is initiated by the
application, as a consequence of some internal state changes.
The update is based on a callback mechanism. The code for creating the
graphical content (related to rendering) has to be placed within a given
method (paint). If the component has to be refreshed, this method will
be called by the toolkit. The method is inherited from the Component
base class and it is overridden by the subclasses.

2016. 07. 11. 15:36 AWT grafika s Applet 6


Paint

public void paint(Graphics g)


The parameter is a Graphics object, the graphical model representing the
component's surface. The properties related to visualization (color, font
etc.) are specified by this object. The area, which has to be refreshed is
also specified (clipping).
The properties can be modified within the paint method, drawing
operations can be performed using the g reference.
During system-triggered painting, the "damaged" surface is determined
by the system (it could be the whole surface of the component) and the
paint method is called. The surface will be erased and refreshed, all the
pixels will be repainted.
Application-triggered painting is initiated calling the repaint method from
the application. In fact, it is an asynchronous request to the toolkit for
refreshing the component (it is not a direct paint method call). As a
response, the update method will be called by the toolkit.

2016. 07. 11. 15:36 AWT grafika s Applet 7


Repaint, update

public void update(Graphics g)


The surface is erased by the default implementation (it is filled with pixels
having a background color).
The method can be overridden within the subclasses. E.g. the surface will
be not erased within the overridden method. In this way, there is a
possibility to use incremental painting (extending the surface with new
graphical elements, without erasing its content).
There are several repaint methods provided by the components: repaint
can be called without parameters (the whole surface will be updated),
with parameters indicating the area to be refreshed, with a parameter
indicating a timeout value (indicating the maximum time in milliseconds
before update).

2016. 07. 11. 15:36 AWT grafika s Applet 8


Observations

The repaint method call is an asynchronous request. The update/paint methods


will be called later by the toolkit. In certain situations more repaint invocations
can be made, before the first effective update/paint call. In these situations the
invocations can be merged into a single paint call by the toolkit. (This
mechanism is useful when there are a lot of small UI updates, e.g. within a
loop.)
The paint methods are never called directly during application-triggered
painting (this is obvious: the parameter type is Graphics and we cannot
instantiate an abstract class), eventually from the overridden update methods
(by forwarding the parameter of these methods).
Each AWT component overrides the paint methods. If we need custom
graphical content, we can also override this method in our subclasses. If we
only want to extend the graphical content of the superclass, we can call the
super.paint method within our overridden paint method (this could be important
when overriding the paint method of containers). If we want to create some
custom graphics we can use the Canvas class provided by the AWT package.

2016. 07. 11. 15:36 AWT grafika s Applet 9


Canvas - example

A Canvas is added to a Frame and there is possibility for the user to draw little circles
onto that canvas, by clicking with the mouse.
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyCanvas extends Canvas {
private int x = 0;
private int y = 0;
public MyCanvas() {
setBackground(new Color(50,100,250));
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
});
2016. 07. 11.
} 15:36 AWT grafika s Applet 10
Canvas - example

public void paint(Graphics g) {


g.setColor(Color.red);
g.fillOval(x,y,20,20);
}

public static void main(String args[]) {


Frame f = new Frame("Paint");
f.setBounds(50,50,300,200);
f.add(new MyCanvas(), BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}

2016. 07. 11. 15:36 AWT grafika s Applet 11


Canvas example - update

According to the previous example, the result of multiple mouse clicks


will be a single circle at the last click position. This happens, because the
surface of the canvas is erased by the default implementation of the
update method. If we don't want to erase the surface we have to
override the update method. We simply call the paint method within the
overridden update method.

public void update(Graphics g) {


paint(g);
}

2016. 07. 11. 15:36 AWT grafika s Applet 12


Canvas example Image

Image abstract base class (e.g. BufferedImage is an implementation


the createImage method of the Component class (introduced for supporting
double-buffering)

//imports
public class MyCanvas extends Canvas {
private Image img;
private Graphics gr;

public MyCanvas() {
setBackground(new Color(50,100,250));
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
gr.fillOval(e.getX(),e.getY(),20,20);
repaint();
}
});
}

2016. 07. 11. 15:36 AWT grafika s Applet 13


Vszon plda Image

public void paint(Graphics g) {


if (img == null) {
img = createImage(getWidth(),getHeight());
gr = img.getGraphics();
gr.setColor(Color.red);
}
g.drawImage(img,0,0,null);
}
public void update(Graphics g) {
paint(g);
}
//the main method
}

2016. 07. 11. 15:36 AWT grafika s Applet 14

You might also like