Presentation is loading. Please wait.

Presentation is loading. Please wait.

Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button.

Similar presentations


Presentation on theme: "Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button."— Presentation transcript:

1 Draw Shapes Introduction to simple graphics

2 What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button –Panel, java.awt.Panel –Slider, Checkbox, List, RadioButton, Window, Frame, In javax.swing.* we have AWT Component subclasses based in JComponent

3 What is Swing? An AWT based API for the creation of GUI’s. Swing components subclass the JComponent class (javax.swing.JComponent). JButton, JSlider, JPanel, JList, JFrame, JCheckBox, JTextField, ….

4 JComponent JComponent subclasses java.awt.Component. Components have: –Size, color, default font, location, other properties. –Threads are associated with Components

5 What do I do with a Component? Show on the screen. It is displayed in a Container. –java.awt.Container –javax.swing.JContainer –Panel, JPanel –You can add components to containers, for display.

6 Containers hold Components Containers manage the damage in the Components that are displayed. Suppose a window is covering another window. This causes damage.

7 How do we repair damage? Damage control manager. –Runs in a thread –Senses Damage –invokes a call-back method called public void paint(Graphics g) { –g.drawStuff here….. }

8 How do I schedule a repaint? JPanel jp = new JPanel(); jp.repaint(); jp.repaint(long ms); jp.repaint(long ms, Rectangle r);

9 What is a graphics context? An instance of the Graphics class Graphics is ABSTRACT! You can extend Graphics You can get instances of the Graphics class –From the paint method –From an instance of an java.awt.Image –From a java.awt.Component

10 What is it with the Graphics Context? Polymorphism enables me to output to any device using invocations to the graphics context. I can ouput a file (postscript, Windows meta file) I can drive a pen-plotter, laser, robot (lego).

11 Why do I need a graphics context? To get access to simple 2D draw methods. - Draw Business graphics - Draw Vectors into the screen. - Draw text - Draw java.awt.Images - simple shapes (2D) - I can print vectors, and bit map.

12 Who calls Paint? public void paint(Graphics g) {... –Not the programmer –Not the JVM –AWT Event Dispatcher (it runs in a thread). –called in response to a repaint invocation! –repaint() –repaint(1000); // paint invoked in 1 second

13 When does the Damage control manager invokes repaint? If there is damage, damage control for the window causes repaint. Window resize Window exposed Component demands. repaint(int x, int y, int w, int h)

14 Damage control manager AWT Dispatcher Component repaint Damage caused update Window system

15 Dimensions Windows have dimension Dimension d = w.getSize(); Components have dimensions too. Int w = d.width; Int h = d.height;

16 Coordinates Integers 0,0 (w,h) x y

17 Formula for a circle Implicit formula –(X-xc)^2 + (Y-yc)^2 = R^2 Parametric Formula –X = sin(t)+xc –Y = cos(t)+yc –T is in the range from 0 to 1 inclusive


Download ppt "Draw Shapes Introduction to simple graphics. What is a Component? A class that resides in the java.awt package Examples include: –Button, java.awt.Button."

Similar presentations


Ads by Google