WDD_lec_19 - Web Design & Development Lecture 19 Java...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 19
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Java Graphics 2
Background image of page 2
When to Paint? Understanding Repaint Cycle
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Your Painting Strategy Steps Subclass JPanel class MyPanel extends JPanel Override the paintComponent(Graphics g) method Inside method do what ever you want to do by using graphics object. Install that JPanel inside a JFrame When frame becomes visible and its paintChildren() gets called your panel will become visible. To become visible your panel calls paintComponent()
Background image of page 4
Last Example Code: MyPanel.java import javax.swing.*; import java.awt.*; public class MyPanel 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); } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Last Example Code: Test.java import javax.swing.*; import java.awt.*; public class Test{ JFrame f; MyPanel p; public Test1(){ f = new JFrame(); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); p = new MyPanel(); c.add(p); f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } // end constructor
Background image of page 6
Last Example Code: Test.java public static void main(String args[ ]){ Test1 = new Test(); } } // end class
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/01/2010 for the course CS CS506 taught by Professor mr. umair javed during the Spring '10 term at Virtual University of Pakistan.

Page1 / 22

WDD_lec_19 - Web Design & Development Lecture 19 Java...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online