Advanced Programming: Java 2D Graphics
Advanced Programming: Java 2D Graphics
Java 2D Graphics
Computer Graphics
● Computer Graphics: the representation and
management of visual content: drawings,
charts, photographs, movies, etc.
● 2D, 3D, Raster (Pixel), Vector, Animation, etc.
● Rendering: generating an image from a model
using a computer, defining its shape, color,
texture, transparency, shades, etc.
● Support for different types of devices: screen,
memory, printer, plotter, etc.
● User Space → Device Space
Java 2D
● Two-dimensional graphics, text, and imaging
● A uniform rendering model for display devices and printers
● Geometric primitives: any geometric shape
● Hit detection on shapes, text, and images
● Ccontrol over how overlapping objects are rendered
● Enhanced color support that facilitates color management
● Support for printing complex documents
● Control of the quality of the rendering (hints)
The “Drawing” Concept
● Graphical interfaces are built using components.
The “system” draws the components automatically:
– when they are displayed for the first time,
– at minimize, maximize operations,
– when resizing the display area;
● The support methods for defining the graphical
representation of a Component are:
– void paint(Graphics g)
– void update(Graphics g)
– void repaint()
The paint method
This method is called when the contents of the component should
be painted; such as when the component is first being shown or is
damaged and in need of repair. The clip rectangle in the Graphics
parameter is set to the area which needs to be painted.
...
Working with Texts
● Font - A collection of glyphs (unique marks that collectively
add up to the spelling of a word) → name, style, size
Label label = new Label("Some text");
label.setFont(new Font("Dialog", Font.PLAIN, 12));
void paint(Graphics g) {
g.setFont(new Font("Courier", Font.BOLD, 10));
g.drawString("Another text", 10, 20); }
●
TextLayout - highlighting, strings with mixed fonts, mixed languages, bidirectional text.
Using Colors
● Paint interface defines how color patterns can be
generated for Graphics2D operations.
● Color encapsulates colors in the sRGB space
Red Green Blue Alpha
Color standardRed = Color.RED; (0 − 255, 0.0 − 1.0)
Color plainWhite = new Color(1.0, 1.0, 1.0);
Color translucentRed = new Color(255, 0, 0, 128);
● SystemColor encapsulate symbolic colors representing
the color of native GUI objects on a system.
SystemColor.desktop
● GradientColor provides a way to fill a Shape with a linear
color gradient pattern. Hello world!
●
TexturePaint provides a way to fill a Shape with a texture
that is specified as a BufferedImage. Hello again...
Using Images
● Image is the superclass of all classes that
represent graphical images.
● BufferedImage
– Loadind from a file
BufferedImage image = ImageIO.read(new File("hello.jpg"))
}
paint(g); Preventing
public void paint(Graphics g) { flickering
BufferedImage offImage =
new BufferedImage(100, 200, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = offImage.getGraphics();
// Draw off-screen
g2.setColor(...);
g2.fillOval(...); ...
// Transfer the drawing: back buffer -> primary surface (screen)
g.drawImage(offImage, 0, 0, this);
g2.dispose();
}
Printing
● Creat a component that implements Printable interface
public class HelloWorldPrinter implements Printable {
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
g.drawString("Hello world!", 100, 100);
return PAGE_EXISTS;
}
}
The Printable.print()
● Create a PrinterJob method is called by
PrinterJob job = PrinterJob.getPrinterJob(); the printing system,
job.setPrintable(new HelloWorldPrinter());
if (job.printDialog()) { just as the
job.print(); Component.paint()
}
● Some Swing components are printing-aware
(JTable, JTextComponent)
Java Tutorial
● Trail: 2D Graphics
http://docs.oracle.com/javase/tutorial/2d/index.html
● Lesson: Full-Screen Exclusive Mode API
http://docs.oracle.com/javase/tutorial/extra/fullscreen/index.html
● Trail: Sound
http://docs.oracle.com/javase/tutorial/sound/index.html
● Java Demos → Java2D application