Download Java Graphics: Painting Swing Components and Primitives and more Slides Web Design and Development in PDF only on Docsity! Web Design & Development Lecture 18 Docsity.com Java Graphics Docsity.com Open WinExp, Notepad
St
BB fee
ea
Tatas
Leip
coat
ae
Cemmane
[He edt ven vers
ot Hele
[tek = fel] Beet |Syrodee (piniy OM A
[same [E cy Suan ealTel HES OEE ere
a
Bint
ES aevaates
ES voe
a
lectures
ecuredont
eo Paver resent
Pedicd uence aM
nes (nama)
pater chen
Iinpat wosi -onatontmeonaten uber vetenh Wor ie Waxed ogarrnasiEpa Garasee 1s Vere (Grp
Docsity.com
Close WinExplorer
St
Cemmane
[rE edt ven Fovorms coke tle
[tek = fel] Beet |Syrodee (piniy OM A
[panes [So ee
lectures
ecuredont
eo Paver resent
Pedicd uence aM
nes (nama)
pater chen
Iinpat wosi -onatontmeonaten uber vetenh Wor ie Waxed ogarrnasiEpa Garasee 1s Vere (Grp
Repaint event sent to: MyApp, Desktop
Docsity.com
Desktop gets repaint event
=lBLx/
lectures
ecuredont
Pesce Pavey
Painting a Swing Component • Three methods are at the heart of painting a swing component – paintComponent() – paintBorder() – paintChildren() Docsity.com 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 Docsity.com 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 Docsity.com Example Code: MyPan1.java import 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); } } Docsity.com Example Code: Test1.java import 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 Docsity.com Example Code: Test1.java public static void main(String args[ ]){ Test1 t = new Test1(); } } // end class Docsity.com 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 Docsity.com Color • Combinations of Red, Green, Blue • Each [0, 255] • Java: new Color(255, 150, 0) Hokie Orange Docsity.com