Download presentation
Presentation is loading. Please wait.
Published byΦιλομήλα Βαμβακάς Modified over 6 years ago
1
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component needs to be displayed, its paint or update method is automatically invoked with an appropriate Graphics context.
2
Complete Java example shown later.
To draw anything in Java we override a graphical components paint method. Complete Java example shown later. public void paint(Graphics g) { /* old code */ } public void paint(Graphics g) { /* Our new code */ }
3
Graphics2D class Graphics2D class extends the Graphics class.
Provides more control over geometry, coordinate transformations, colour management, text layout. Fundamental class for rendering 2-dimensional shapes, text and images in Java. Graphics Graphics2D UML Class Diagram
4
Overview of the Java 2D API
The Java 2D API provides A uniform rendering model for display devices and printers A wide range of geometric primitives, such as curves, rectangles, and ellipses and a mechanism for rendering virtually any geometric shape Mechanisms for performing hit detection on shapes, text, and images A compositing model that provides control over how overlapping objects are rendered Enhanced colour support that facilitates colour management Support for printing complex documents
5
Rendering Take description of objects, including images, and convert that into a form which can be displayed as a grid of pixels. Each pixel displays a colour from some palette. The colour can be defined as a combination of red, green, blue for example.
6
Graphics Rendering Context
Graphics2D rendering context : the collection of state attributes associated with a Graphics2D object. Used to display text, shapes, images, To render: set up the Graphics2D rendering context call one of the Graphics2D rendering methods, such as draw or fill.
7
Rendering context: Attributes
Attribute methods: setStroke setPaint setComposite setTransform setClip setFont setRenderingHints
8
pen style: applied to outline of shape. stroke attribute enables
lines with any point size, dashing pattern end-cap and join decorations for lines
9
fill style: applied to a shape's interior
fill style: applied to a shape's interior. paint attribute enables fill shapes with solid colours, gradients, and patterns. fill lines with any point size
10
composite style: renders objects that overlap existing objects
11
transform: applied during rendering converts rendered object from user space to device-space coordinates. Can also transform with translation rotation scaling shearing
12
clipping: uses a Shape to define a clipping path rendering only applied area within clipping path. Any Shape can be used to define clip.
13
Setting Attributes To set an attribute, pass the appropriate attribute object to graphics object: E.g to change paint attribute of g2 to a red-yellow fill, construct a GradientPaint object gp = new GradientPaint (0, 0, Color.red, 175, 175, Color.yellow, true); call setPaint. g2.setPaint(gp);
14
Attributes are referenced
A Graphics2D context object holds references to its attribute objects. After attribute object modified, call a ‘set’ method to notify the Graphics2D context.
15
The 2D drawing process: code perspective
After obtaining graphics object g, cast as 2D graphics object. Following slides outline optional stages of rendering objects with Java Graphics Graphics2D g2 = (Graphics2D) g; Graphics2D UML Class Diagram
16
Rendering Hints RenderingHints myHints =
/* code to define rendering */; g2.setRenderingHints(myHints);
17
Define kind of ‘brush’ used: E.g.
Set Stroke Define kind of ‘brush’ used: E.g. line thickness, dotted, continuous, etc Stroke stroke = /* code, E.g. line thickness */; g2.setStroke(stroke);
18
Choose Paint Style Paint paint
= /* choose paint options, e.g colour */; g2.setPaint(paint);
19
Set clipping region Shape clip = /* code to define clipped region */; g2.setClip(clip);
20
Transformation Transformation from user space to device space AffineTransformation transform = /* code to choose transform */; g2.transform(transform);
21
/* choose composite options */; g2.setComposite(composite);
Set a composition rule Composite composite = /* choose composite options */; g2.setComposite(composite);
22
Define shape to draw in Graphic2D object
Shape shape = /* code defining shape to draw in g2. Shape is an interface class in Java, to choose an actual shape pick from one of the classes that implement shape, such as Line2D. See example later. */;
23
Draw of fill shape g2.fill(shape); g2.draw(shape);
24
Complete Example 2D Java Code
import java.awt.*; import java.awt.geom.*; public class StringArt { public static void main(String[ ] args) { Frame f = new ApplicationFrame("StringArt v1.0") { private int mNumberOfLines = 25; private Color[ ] mColors = { Color.red, Color.green, Color.blue }; public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Dimension d = getSize(); for (int i = 0; i < mNumberOfLines; i++) { double ratio = (double)i / (double)mNumberOfLines; Line2D line = new Line2D.Double(0, ratio * d.height, ratio * d.width, d.height); g2.setPaint(mColors[i % mColors.length]); g2.draw(line); }}}; f.setSize(200, 200); f.setVisible(true); }} Complete Example 2D Java Code
26
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g; Dimension d = getSize(); for (int i = 0; i < mNumberOfLines; i++) { double ratio = (double)i / (double)mNumberOfLines; Line2D line = /*** implements Shape ***/ new Line2D.Double(0, ratio * d.height, ratio * d.width, d.height); g2.setPaint(mColors[i % mColors.length]); g2.draw(line); } };
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.