Programming Seminar
@12:30 – 13:30 pm on Wednesday 9/4/2014
Location : 2.505.01
• Coordinate systems
• Colors
• Fonts
• Drawing shapes
• Graphics2D API
Agenda
By: dr. Amal Khalifa
1
Painting components
Every component is responsible for drawing itself.
If you want to use a standard component, you only
have to add it to your applet or frame.
You don’t have to worry about painting it on the screen.
That will happen automatically, since it already knows
how to draw itself.
2
dr. Amal Khalifa, 2014
Painting components
dr. Amal Khalifa, 2014
3
Class JComponent (package javax.swing)
contains a paintComponent(Graphics g)
for drawing graphics.
Not called directly; called only by the system when a
lightweight Swing component needs to be repainted.
Use the component’s repaint() method instead.
repaint() method returns immediately, without doing any
painting itself.
Graphics Objects
A graphics context enables drawing on the screen.
To do any drawing at all in JAVA, you need an
object belonging to the class
java.awt.Graphics
manages a graphics context and draws pixels on the
screen
Instance methods are provided in this class for drawing
shapes, text, and images.
4
dr. Amal Khalifa, 2014
Graphics Contexts
dr. Amal Khalifa, 2014
5
To create a drawing surface:
define a subclass of JPanel
provide a custom paintComponent(Graphics g)method.
the paintComponentmethod must be smart enough to
correctly redraw the component at any time A program should store state information in its instance variables decide what to draw.
When the program wants to change the content
of the component change the values of the relevant variables and call repaint().
•a scheme for identifying every point on the screen.
• x-coordinates from left to right.
• y-coordinates from top to bottom.
Java coordinate system 6
Colors
JAVA is designed to work with the RGB color system.
An RGB color is specified by three numbers that give the level of red, green, and blue, respectively, in the color.
A color in JAVA is an object of the class, java.awt.Color.
Color class defines several named constants representing common colors
8
dr. Amal Khalifa, 2014
JColorChooser Dialog
dr. Amal Khalifa, 2014
11
Package javax.swing provides the JColorChooser GUI component that enables application users to select colors.
static method showDialog
creates a JColorChooser object,
attaches it to a dialog box
displays the dialog.
Returns the selected Color object, or null if the user presses Cancel or closes the dialog without pressing OK.
Three arguments—a reference to its parent Component, a String to display in the title bar of the dialog and the initial selected Color for the dialog.
Fonts
A font represents a particular size and style of text.
The available font names are system dependent, but
you can always use the following four strings as font
names: “Serif”, “SansSerif”, “Monospaced”, and
“Dialog”.
Example: Font plainFont = new Font("Serif", Font.PLAIN, 12);
Font bigBoldFont = new Font("Sans Serif", Font.BOLD,
24);
13
dr. Amal Khalifa, 2014
Java 2D API
The Java 2D API provides advanced two-dimensional graphics capabilities for programmers who require detailed and complex graphical manipulations.
For an overview, see the Java 2D demo visit download.oracle.com/javase/6/docs/technotes/guides/2d/
Drawing with the Java 2D API is accomplished with a Graphics2D reference (package java.awt).
To access Graphics2D capabilities, we must cast the Graphics reference (g) passed to paintComponent into a Graphics2D reference with a statement such as
Graphics2D g2d = ( Graphics2D ) g;
21
dr. Amal Khalifa, 2014
Example
dr. Amal Khalifa, 2014
22
Class GradientPaint helps draw a shape in gradually
changing.
Graphics2D method fill draws a filled Shape
object—an object that implements interface Shape
(package java.awt)
method setStroke sets the characteristics of the shape’s border.
Class GeneralPath
dr. Amal Khalifa, 2014
23
General path—constructed from straight lines and complex curves (package java.awt.geom).
Methods: moveTo moves to the specified point.
lineTo draws a line from the current point to the specified point.
closePath draws a line from the last point to the point specified in the last call to moveTo.
Graphics2D method translate moves the drawing origin to the specified location.
Graphics2D method rotate rotates the next displayed shape.
The argument specifies the rotation angle in radians (with 360° = 2p radians).
Top Related