Graphics in Java hussein suleman uct cs 116 2005.

Slides:



Advertisements
Similar presentations
Custom Painting Gestione della Grafica customizzata Vedi anche:
Advertisements

1 Event Listeners Some Events and Their Associated Event Listeners Act that Results in the EventListener Type User clicks a button, presses Enter while.
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Made with love, by Zachary Langley Applets The Graphics Presentation.
Web Design & Development Lecture 19. Java Graphics 2.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
CS18000: Problem Solving and Object-Oriented Programming.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Graphic User Interfaces Layout Managers Event Handling.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Graphical User Interfaces, 2D Graphics & Game Programming.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Event Handling Events and Listeners Timers and Animation.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 14 GUI and Event-Driven Programming.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Web Design & Development Lecture 18. Java Graphics.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
Welcome to CIS 083 ! Events CIS 068.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Swing II. Swing II Topics WindowListener interface WindowListener interface Icons Icons Scrollbars Scrollbars The Graphics class The Graphics class Colors.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Creating Your Own Widgets CompSci 230 S Software Construction.
Layout Managers Arranges and lays out the GUI components on a container.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Review_6 AWT, Swing, ActionListener, and Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
CS 240 Week 9. EventQueue.invokeLater In Swing, all user interface operations must occur on the “UI thread” – All components should be created on the.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
12 Graphics and Java 2D™.
Creating Your Own Widgets
Advanced User Interfaces
Computer Science 209 Graphics and GUIs.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Swing II.
Events, Event Handlers, and Threads
Presentation transcript:

Graphics in Java hussein suleman uct cs

What are graphics?  Graphic primitives: lines, squares, rectangles, circles, ellipses, arcs, polygons, text, etc. HelloWorld!

Built-in 2D Graphics Frameworks  AWT Graphics (Slack) drawRect (40, 40, 100, 30)  Swing Graphics Method-oriented  drawRect (40, 40, 100, 30) “Object”-oriented  draw (new Rectangle2D.Float (40, 40, 100, 30))  draws a rectangle  fill (new Rectangle2D.Float (40, 40, 100, 30))  draws a filled rectangle

Component Coordinate System  All graphics must be drawn as part of or on a Swing/AWT component.  All coordinates are then relative to that component. Component positive x axis positive y axis 0,0

Event-driven Graphics 1/2  Graphics should NOT be drawn in the main program. When a program is minimised and maximised, the graphics will not be redrawn.  Instead, override the pre-defined paint or paintComponent method to specify how Java/OS should redraw the component whenever necessary. The OS will then redraw the component whenever it is moved, resized, maximised, uncovered, etc.

Event-driven Graphics 2/2 class … { void Paint (…) { … } class MainClass { void main (…) { …} } program operating system/Java program starts UI is drawn user does stuff and UI changes user moves window program ends what you write what Java does for you

Example 1: Painting class DrawPanel extends JPanel { // override the painting routine of the component protected void paintComponent ( Graphics gr ) { // first call the superclass’s method super.paintComponent (gr); // then get a “handle” to the window for drawing Graphics2D canvas = (Graphics2D)gr; // issue a series of drawing commands canvas.draw (new Rectangle2D.Float (100, 100, 400, 400)); canvas.draw (new Line2D.Float (159, 159, 441, 441)); canvas.draw (new Line2D.Float (159, 441, 441, 159)); canvas.draw (new Ellipse2D.Float (100, 100, 400, 400)); canvas.drawString ("Hello World", 280, 520); }

Painting Example Swing 1/2 class TestFrame extends JFrame implements ActionListener { public DrawPanel dp; public TestFrame () { super ("Example One"); setSize (600, 600); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel(); pane.setLayout (new BorderLayout ()); JPanel pane2 = new JPanel(); pane2.setLayout (new FlowLayout ()); JButton exit = new JButton ("Exit"); exit.addActionListener (this);

Painting Example Swing 1/2 dp = new DrawPanel (); pane2.add (exit); pane.add ("North", pane2); pane.add ("Center", dp); setContentPane (pane); setVisible (true); } public void actionPerformed ( ActionEvent e ) { System.exit(0); }

Painting Example Main Class public class example1 { public static void main ( String [] arguments ) { // set user interface style try { UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ()); } catch (Exception e) {}; new TestFrame(); }

Paint Example Output

Graphics Primitives: Line  new Line2D.Float (x1, y1, x2, y2) x1 y1 x2 y2

Graphics Primitives: Rectangle  new Rectangle2D.Float (x, y, width, height) x y width height

Graphics Primitives: Ellipse  new Ellipse2D.Float (x, y, width, height) x y width height

Graphics Primitives: Arc  new Arc2D.Float (x, y, width, height, startangle, extent, type) type is in {Arc2D.PIE, Arc2D.CHORD, Arc2D.OPEN} angles are in degrees x y startangle extent width height

Graphics Primitives: RoundedRect  new RoundRectangle2D.Float (x, y, width, height, arcw, arch) x y width height arcwarch

Graphics Primitives: Polygon  new Polygon ( int [] xpoints, int [] ypoints, int npoints) x1 y1 x2 y2 x3 y3 int x [] = {x1, x2, x3}; int y [] = {y1, y2, y3}; p = new Polygon (x, y, 3);

Graphics Primitives: Text  drawString (text_string, x, y) x y Hello World!

Line Attributes  setColor ( Color c ) e.g., Color.blue, Color.red, Color.green sets the colour to be used for all subsequent graphics.  setStroke (new BasicStroke (weight, cap, join)) cap is in {BasicStroke.CAP_ROUND/CAP_BUTT/CAP-SQUARE} join is in {BasicStroke.JOIN_ROUND/JOIN_MITER/JOIN_BEVEL} sets the type of line and the way one line joins another at corners.

Text Attributes  setFont ( Font f ) sets the font to be used for all subsequent text drawn. new Font ( name, style, weight )  e.g., new Font (“TimesRoman”, Font.PLAIN, 12)  Font names also include “Serif” and “SansSerif”.  Font styles also include BOLD and ITALIC.

Problem  Draw the following figure using Java’s graphics primitives:

Coordinate Transformations  scale (scalex, scalex) scales all subsequent coordinates in graphics primitive operations by scalex in x direction and scaley in y direction.  translate (diffx, diffy) moves the origin of the axes to the location specified. 0,0 diffx,diffy

Example 2: Mouse Interaction class DrawPanel extends JPanel implements ActionListener, MouseMotionListener, MouseListener { JButton zoomin, zoomout; float shiftx, shifty, scale; float startx, starty; int boy; // set up panel DrawPanel ( JButton zin, JButton zout ) { zoomin = zin; zoomout = zout; reset(); } // set default values in variables public void reset () { scale = 1.0f; shiftx = 0; shifty = 0; boy = 100; }

Mouse Interaction: Painting public void drawBoy ( Graphics2D canvas, int x, int y ) { canvas.translate (x, y); canvas.setColor (Color.blue); canvas.setStroke (new BasicStroke (3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); canvas.draw (new Ellipse2D.Float (20, 0, 60, 60)); canvas.fill (new Ellipse2D.Float (28, 16, 12, 8)); canvas.fill (new Ellipse2D.Float (60, 16, 12, 8)); canvas.setStroke (new BasicStroke (5.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); canvas.draw (new Line2D.Float (50, 18, 50, 30)); canvas.setStroke (new BasicStroke (3.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); canvas.fill (new Arc2D.Float (40, 30, 20, 20, 0, -180, Arc2D.PIE)); canvas.draw (new Rectangle2D.Float (44, 60, 12, 40)); canvas.fill (new RoundRectangle2D.Float (18, 80, 64, 120, 10, 10)); int hand1x[] = {20,0,0,10,10,20}; int hand1y[] = {90,100,180,180,110,104}; canvas.draw (new Polygon (hand1x,hand1y,hand1x.length)); int hand2x[] = {80,100,100,90,90,80}; int hand2y[] = {90,100,180,180,110,104}; canvas.draw (new Polygon (hand2x,hand2y,hand2x.length)); int leg1x[] = {40,40,30,10,10,30,30}; int leg1y[] = {198,280,290,290,280,280,198}; canvas.draw (new Polygon (leg1x,leg1y,leg1x.length)); int leg2x[] = {60,60,70,90,90,70,70}; int leg2y[] = {198,280,290,290,280,280,198}; canvas.draw (new Polygon (leg2x,leg2y,leg2x.length)); canvas.translate (-x, -y); } protected void paintComponent ( Graphics gr ) { super.paintComponent (gr); Graphics2D canvas = (Graphics2D)gr; canvas.translate (shiftx, shifty); canvas.scale (scale, scale); drawBoy (canvas, boy, 100); }

Mouse Interaction: Actions public void actionPerformed ( ActionEvent e ) { if (e.getSource() == zoomin) scale *= 1.20f; else if (e.getSource() == zoomout) scale /= 1.20f; else reset(); repaint(); } public void mouseDragged ( MouseEvent m ) { shiftx += (m.getX() - startx); shifty += (m.getY() - starty); startx = m.getX(); starty = m.getY(); repaint(); } public void mouseMoved ( MouseEvent m ) {} public void mouseClicked ( MouseEvent m ) {} public void mouseEntered ( MouseEvent m ) {} public void mouseExited ( MouseEvent m ) {} public void mousePressed ( MouseEvent m ) { startx = m.getX(); starty = m.getY(); } public void mouseReleased ( MouseEvent m ) {} }

Mouse Interaction: Frame 1/2 class TestFrame extends JFrame implements ActionListener { public DrawPanel dp; public TestFrame () { super ("Graphics Editor"); setSize (600, 600); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); JPanel pane = new JPanel(); pane.setLayout (new BorderLayout ()); JPanel pane2 = new JPanel(); pane2.setLayout (new FlowLayout ()); JButton zin = new JButton ("Zoom In"); JButton zout = new JButton ("Zoom Out"); JButton reset = new JButton ("Reset"); dp = new DrawPanel (zin, zout); pane2.add (zin); pane2.add (zout); pane2.add (reset); pane.add ("North", pane2); pane.add ("Center", dp);

Mouse Interaction: Frame 2/2 zin.addActionListener (dp); zout.addActionListener (dp); reset.addActionListener (dp); dp.addMouseMotionListener (dp); dp.addMouseListener (dp); MenuBar mb = new MenuBar (); Menu file = new Menu ("File"); MenuItem exit = new MenuItem ("Exit"); exit.addActionListener (this); file.add (exit); mb.add (file); setMenuBar (mb); setContentPane (pane); setVisible (true); } public void actionPerformed ( ActionEvent e ) { System.exit(0); }

Mouse Interaction: Output

MouseMotionListener Interface  mouseMoved is invoked when the mouse is moved and no buttons are being pressed.  mouseDragged is invoked when the mouse is moved while one or more buttons are held down. mousePressed  ->mouseDragged  ->mouseReleased  Parameter same as for MouseListener.

Scrolling the Canvas  Store position of canvas as a set of offsets that must be added to all coordinates before drawing.  When a mouse button is pressed, store the position of the mouse.  When mouse is dragged, calculate difference between current position and stored position and add this to the offsets.  Use translate to offset canvas prior to drawing.

Zooming In and Out  Store zoom state as a set of scale multipliers in each direction.  Before drawing any graphics, multiply the coordinates by the multipliers.  When zooming in/out, multiply the multipliers by factors greater than or less than 1.  Use scale to scale canvas coordinates prior to drawing.

The repaint method  repaint can be called explicitly after any changes to the user interface.  repaint causes Java to invalidate the region i.e., make it seem in need of repainting. once a region has been invalidated, Java will call the paint function of the component, when it is safe to do so.

Example 3: Animation  Create multiple images on a single canvas, with parameters to indicate relative position.  Each time a button is clicked (or some trigger is activated), move the images to resemble animation by changing the parameters used by paintComponent to position graphics.  Non-interactive animation typically uses a separate “thread” (like a program) to control the animation.

Animation: Painting protected void paintComponent ( Graphics gr ) { super.paintComponent (gr); Graphics2D canvas = (Graphics2D)gr; canvas.translate (shiftx, shifty); canvas.scale (scale, scale); drawBoy (canvas, boy, 100, walk); drawGirl (canvas, girl, 100, walk); if ((girl - boy) == 100) { drawHeart (canvas, girl, 50); }

Animation: Button Processing public void actionPerformed ( ActionEvent e ) { if (e.getSource() == zoomin) scale *= 1.20f; else if (e.getSource() == zoomout) scale /= 1.20f; else if (e.getSource() == animate) { if (boy != 200) { boy += 5; girl -= 5; walk = 1-walk; } else reset(); repaint(); }

Animation: Mouse Actions public void mouseDragged ( MouseEvent m ) { shiftx += (m.getX() - startx); shifty += (m.getY() - starty); startx = m.getX(); starty = m.getY(); repaint(); } public void mousePressed ( MouseEvent m ) { startx = m.getX(); starty = m.getY(); }

Animation: Output