Java Graphics

Download Report

Transcript Java Graphics

Java Graphics
Graphics
• Window is like a painter’s canvas
• Application must paint its window contents
• Java components paint themselves
• Anything else: Programmer
• When to paint?
• How to paint?
JButton
How to Paint?
Coordinate System
• Upside-down Cartesian
(0,0)
(0,height)
(width,0)
(width, height)
Component Hierarchy
• Each component has its own subwindow
• Subwindow = rectangular area within parent component
• Has own coordinate system
• Clipping:
• Can’t paint outside its subwindow
• Can’t paint over child components?
(0,0)
JPanel
(0,0)
JButton
JButton
(wb, hb)
(wp, hp)
Painting Components
• Can paint any component
• JPanel is good for custom graphics area
JButton
JPanel
Painting in Java
import java.awt.Graphics
import java.awt.Graphics2D // Java2
1. Get the “graphics context” of component
public void paintComponent (Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
…
2. Paint in it
g2.drawLine(x1,y1, x2,y2);
Graphics Primitives
Draw
• Point (x,y)
• Line (pt1,pt2)
• PolyLine (pt list)
• Arc
• Oval (pt, w,h)
• Rectangle (pt, w,h)
• RoundRectangle
• Polygon (pt list)
• Image (file, x,y)
• Text (string, x,y)
label
Fill
Graphics Attributes
• Color
• Font
• Stroke attributes:
– Line width, dash, end caps, joins, miter
• Paint attributes:
– Color, gradient, texture
• Composite:
– Blending
• Transforms:
– Translate, rotate, flip, shear, scale
Graphics Class: Methods include
• drawArc() – draws a hollow arc
• drawLine() – draws a straight line
• drawOval() - draws a hollow oval
– If width and height are same, it draws a circle
• drawPolygon() - draws a hollow polygon
• drawRect() - draws a hollow rectangle
• drawRoundRect() - draws a hollow round cornered rectangle
• drawString() – display a text string
• fillArc() - draw a filled arc
• fillOval()
• fillPolygon()
• fillRect()
• fillRoundRect()
• getColor – retrieve the current drawing colour
• getFont
• setColor – sets the drawing color
• setFont
More at http://java.sun.com/javase/6/docs/api/java/awt/Graphics.html
Color
• Combinations of Red, Green, Blue- each [0, 255]
• Java.awt.Color offers predefined constants of 13
colors BLACK, YELLOW, WHITE, PINK,
RED, …
g2.setPaint(Color.RED);
• Specify a custom color
g2.setPaint (new Color(255, 150, 0));
Hokie Orange
in Java
•
Drawing primitives:
•
•
g2.drawLine( ), g2.drawRect( ), … g2.fillRect( ), …
Object oriented:
1. Create shape:
•
•
•
•
import java.awt.geom.*
Shape interface
ClassImplements shape = new ClassImplements ( args );
Line2D, Rect2D, CubicCurve2D, …
Rectangle2D rect = new Rectangle2D.Double (leftX, topY, width, height)
in Java
2. Draw the shape:
•
•
•
Java uses floating-point coordinates
–
•
g2.draw(shape);
g2.fill(shape);
Either float or double
Rectangle2D is actually abstract class
–
–
–
Two concrete subclasses (static inner classes)
Rectangle2D.Float
Rectangle2D.Double
in Java
•
Color and font:
•
•
•
g2.setColor( new Color(r,g,b) );
g2.setFont( new Font(…) );
Advanced:
•
•
•
•
g2.setStroke(…);
g2.setPaint(…);
g2.setComposite(…);
g2.setTransform(…);
1. Set graphics attributes
2. Draw graphics
When to Paint?
Re-Paint
• Screen is like a painter’s canvas
• All windows paint on the same surface!
• Windows don’t “remember” whats under them
• Need to re-paint when portions are newly exposed
• Receive Repaint events
• Open, resize, bring to front
• When other windows in front move, resize, close
MyApp
Open Explorer, Notepad
Close Explorer
Repaint event sent to: MyApp, Desktop
Desktop gets repaint event
MyApp gets repaint event
MyApp JPanel gets repaint event
MyApp gets repaint event
MyApp JPanel forwards repaint event to JButton
Repainting Static Graphics
• Repaint event:
• Erase subwindow (fill with background color)
• Draw subwindow contents
In Java
• Repaint event:
• Java Swing components catch repaint event,
and call their paintComponent( ) method
• Default paintComponent( ) method paints component
– E.g. panel background color
• Sub-class the component (typically JPanel)
• Over-ride paintComponent( ) method
• Custom graphics here
• Can call repaint( ) to invoke paintComponent( )
Code
public class MyPanel extends JPanel {
public void paintComponent(Graphics g){
super.paintComponent(g);
// erases background
Graphics2D g2 = (Graphics2D)g; //cast for java2
}
}
// my graphics:
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);
Hello World
DEMO DrawTest.java