Sei sulla pagina 1di 4

Java2D

Standard package of Java 1.2 Advanced two dimensional graphics capabilities


Improvements over AWT

Use instance of Graphics2D class


subclass of Graphics Must downcast Graphics reference passed to paint Graphics2D g2d = (Graphics2D) g;

~ marini

Limitations of AWT Graphics


All lines were drawn with a single-pixel thickness Only a handfull of fonts available Have to write own program for Transformation Special fills, gradients & patterns Image support are in early stage of development Control of transparency are awkward

Java Coordinate System


Measured in pixels (PIXture ELEmentS) smallest unit of resolution
0 0 (x, y) +y y axis +x x axis

marini - 2011

Component Hierarchy
Each component has its own subwindow
Subwindow is a rectangular area within parent component Has own coordinate system
(0,0) JPanel panel1 (0,0) JPanel panel2

example:
import javax.swing.*; import java.awt.*; public class Grafik extends JApplet { public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.drawString("Menggunakan Kelas Graphics2d", 10, 20); } }
Casting Graphics g (10, 20)

(wp1, hp1)

(wp2, hp2)

Graphics Contexts and Graphics Objects


Graphics contexts enables drawing on screen Graphics object manages graphics context
Controls how information is drawn Has methods for drawing, font manipulation, etc

Methods of Graphics Class


void setColor(Color c) void setFont(Font f) void drawString(String str, int x, int y) void drawImage(Image img, int x, int y, ImageObserver observer)

We overides method paint in applets


Method paint takes Graphics object as argument paint called automatically when applet starts

We use paintComponent for other containers


Method paintComponet takes Graphics object as argument

marini - 2011

Color control
Combinations of Red, Green, Blue Each [0, 255]

Hokie Orange Class Color e.g. new Color(255,150,0) Graphics method setColor sets drawing color
Takes Color object Method getColor gets current color setting Graphics2D uses setPaint class

import java.awt.*; import javax.swing.*; public class WarnaTeks extends JApplet { public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setPaint(Color.blue); g2d.drawString("Grafik Java", 20, 50); } }

example..

Color constants
Predefined color objects:
Color.white Color.gray Color.black Color.pink Color.orange Color.green Color.cyan Color.lightGray Color.darkGray Color.red Color.blue Color.yellow Color.magenta

Font Control
Class Font
public Font(String name,int style,int size) name: any font supported by system
Serif, Monospaced

style:
Constants: FONT.PLAIN, FONT.ITALIC, FONT.BOLD Combinations: FONT.ITALIC + FONT.BOLD

size:
measured in points (1/72 of an inch)

Use similar to Color


g2d.setFont(fontObject );

marini - 2011

Font Control
Class FontMetrics Has methods for getting font metrics g2d.getFontMetrics
returns FontMetrics object Font fon = new Font("TimesRoman", Font.PLAIN, 14); FontMetrics fm = getFontMetrics(fon);
height

leading

ebkp

ascent descent baseline

FontMetrics methods:
int int int int stringWidth(String str) getAscent() getDescent() getHeight()

example..
import java.awt.*; import javax.swing.*; public class FonMetrik extends JApplet { public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g;

Get str1 width

Font fon = new Font("SanSerif", Font.PLAIN, 14); FontMetrics metrik = getFontMetrics(fon); String str1 = "Selamat "; int lebar_str1 = metrik.stringWidth(str1); g2d.setFont(fon); g2d.setPaint(Color.blue); Draws maju jaya g2d.drawString(str1, 50, 50); next to str1 g2d.setFont(new Font("Serif", Font.BOLD+Font.ITALIC, 24)); g2d.setPaint(Color.red); g2d.drawString("Maju Jaya", 50+lebar_str1, 50); } }

marini - 2011

Potrebbero piacerti anche