FMS Java 07 GUI3
FMS Java 07 GUI3
Management
Systems
Java GUI – 3
Programming Graphical User Interfaces in Java.
Java Graphics
Basic Concepts
• Graphics, pixels, component hierarchy, primitives,
attributes
Primitives using Graphics library
Primitives using Graphics2D library
Repainting
• Problem definition
• Examples
• paintComponent method. Exercises
Designing a graphical traffic monitoring system
When to paint?
How to paint?
JButton
J. Vila & A. Rodas
Basic Concepts. Pixels
(0,0) (width,0)
JButton
JPanel
Line (pt1,pt2)
Arc
Basic Concepts
• Graphics, pixels, component hierarchy, primitives,
attributes
Primitives using Graphics library
Primitives using Graphics2D library
Repainting
• Problem definition
• Examples
• paintComponent method. Exercises
Designing a graphical traffic monitoring system
Draws a rectangle of the specified width and height. The top-left corner
of the rectangle has the coordinates (x, y). Only the outline of the rectangle
is drawn using the Graphics object’s color—the body of the rectangle is
not filled with this color.
public void fillRect( int x, int y, int width, int height )
Draws a filled rectangle with the specified width and height. The top-left
corner of the rectangle has the coordinate (x, y). The rectangle is filled
with the Graphics object’s color.
public void clearRect( int x, int y, int width, int height )
Draws a filled rectangle with the specified width and height in the
current background color. The top-left corner of the rectangle has the
coordinate (x, y). This method is useful if the programmer wants to
remove a portion of an image.
public void drawRoundRect( int x, int y, int width, int height,
int arcWidth, int arcHeight )
Draws a rectangle with rounded corners in the current color with the
specified width and height. The arcWidth and arcHeight determine
the rounding of the corners (see Fig. 12.20). Only the outline of the shape is
drawn.
Draws a filled rectangle with rounded corners in the current color with the
specified width and height. The arcWidth and arcHeight determine
the rounding of the corners (see Fig. 12.20).
public void draw3DRect( int x, int y, int width, int height, boolean b )
Draws an oval in the current color with the specified width and height.
The bounding rectangle’s top-left corner is at the coordinates (v, y). The
oval touches all four sides of the bounding rectangle at the center of each
side (see Fig. 12.21). Only the outline of the shape is drawn.
public void fillOval( int x, int y, int width, int height )
Draws a filled oval in the current color with the specified width and
height. The bounding rectangle’s top-left corner is at the coordinates (x,
y). The oval touches all four sides of the bounding rectangle at the center of
each side (see Fig. 12.21).
J. Vila & A. Rodas
Primitives using Graphics library
Method Description
public void drawArc( int x, int y, int width, int height, int startAngle, int
arcAngle )
Draws an arc relative to the bounding rectangle’s top-left x and y coordinates with the specified
width and height. The arc segment is drawn starting at startAngle and sweeps
arcAngle degrees.
public void fillArc( int x, int y, int width, int height, int startAngle, int
arcAngle )
Draws a filled arc (i.e., a sector) relative to the bounding rectangle’s top-left x and y
coordinates with the specified width and height. The arc segment is drawn starting at
startAngle and sweeps arcAngle degrees.
Basic Concepts
• Graphics, pixels, component hierarchy, primitives,
attributes
Primitives using Graphics library
Primitives using Graphics2D library
Repainting
• Problem definition
• Examples
• paintComponent method. Exercises
Designing a graphical traffic monitoring system
Other methods
g2.scale(shape);
g2.rotate(shape);
g2.shear(shape);
g2.clip(shape);
Insert a panel !!
// area of interest: Comunitat Valenciana
// size
double sizeEW = lonEast - lonWest;
double sizeNS = latNorth - latSud;
Basic Concepts
• Graphics, pixels, component hierarchy, primitives,
attributes
Primitives using Graphics library
Primitives using Graphics2D library
Repainting
• Problem definition
• Examples
• paintComponent method
Designing a graphical traffic monitoring system
MyApp
J. Vila & A. Rodas
Repainting. Example
Close
Explorer
REQUEST
R1 R2 RN
update( )
Repaint event:
- Java Swing components catch repaint event, and call their
paintComponent( ) method
- Default paintComponent( ) method paints component
‣ E.g. panel background color
// my graphics:
Hello World
g2.setColor(new Color(255,0,0));
g2.fillRect(10,10,200,50);
g2.setColor(new Color(0,0,0));
g2.drawString("Hello World", 10, 10);
}
}
Repaint event:
- Erase window (draw background color)
- Draw window contents using data structure
Problem: Flashing
Ugly flashing when repaint:
- Paint background
- Redraw shapes
Only red
rectangle is
persistent
We need to modify
this protected code
Using Customize
Code Menu
Done!
Basic Concepts
• Graphics, pixels, component hierarchy, primitives,
attributes
Primitives using Graphics library
Primitives using Graphics2D library
Repainting
• Problem definition
• Examples
• paintComponent method. Exercises
Designing a graphical traffic monitoring system