web design & development lecture 18. java graphics
TRANSCRIPT
Web Design & DevelopmentLecture 18
Java Graphics
Graphics
• Window is like a painter’s canvas
• App must paint its window contents• Java components paint themselves
• Anything else: Programmer
• When to paint?
• How to paint?JButton
MyApp
Open WinExp, Notepad
Close WinExplorer
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
Painting a Swing Component
• Three methods are at the heart of painting a swing component– paintComponent()– paintBorder()– paintChildren()
The Swing Painting Methods
• It's one of three methods, that objects of type JComponent use to paint themselves. The three methods are invoked in this order:
– paintComponent — The main method for painting. By default,
it first paints the background. Then it performs any custom painting.
– paintBorder — Tells the component's border (if any) to paint. Do not invoke or override this method.
– paintChildren — Tells any components contained by this component to paint themselves. Do not invoke or override this method
The following figure illustrates the order in which each component that inherits from JComponent paints itself. Steps 1 and 2 — painting the background and performing custom painting — are performed by the paintComponent method. Step 3 is performed by paintBorder, and step 4 is performed by paintChildren
• An Example of Painting • To illustrate painting, we'll use the
swingApplication program. Here is its GUI:
Your Painting Strategy
• Steps– Subclass JPanel
• class MyPanel extends JPanel
– Override the paintComponent(Graphics g) method• Inside method using graphics object Do whatever drawing
you want to do
– Install that JPanel inside a JFrame• When frame becomes visible through the paintChildren()
method your panel become visible• To become visible your panel will call paintComponent()
method which will do your custom drawing
Example Code: MyPan1.javaimport javax.swing.*;import java.awt.*;
public class MyPan1 extends JPanel {
public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour
Graphics2D g2 = (Graphics2D)g;
g2.drawRect(20,20,20,20); g2.setColor(Color.blue); g2.fillOval(50,50,20,20);
g2.drawString("Hello World", 120, 50);
} }
Example Code: Test1.javaimport javax.swing.*;import java.awt.*;
public class Test1{
JFrame f; MyPan1 p;
public Test1(){
f = new JFrame(); Container c = f.getContentPane();
c.setLayout(new BorderLayout());
p = new MyPan1(); c.add(p);
f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} // end constructor
Example Code: Test1.java public static void main(String args[ ]){
Test1 t = new Test1();
}
} // end class
Output
Graphics PrimitivesDraw Fill
• 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
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
Color
• Combinations of Red, Green, Blue
• Each [0, 255]
• Java: new Color(255, 150, 0)
Hokie Orange