1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Fun with Swing Chapter 9. Overview Swing vs. AWT Creating windows and panels. Displaying formatted text in panels. Drawing graphics (lines, circles, etc.)
Graphical User Interfaces
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
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.
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.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Chapter 2 Programming by Example. A Holistic Perspective Three sections separated by blank lines. Program comment File: FileName.java
Problem Solving 6 GUIs and Event Handling ICS-201 Introduction to Computing II Semester 071.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Graphics Programming. In this class we will cover: Drawing lines, rectangles, and ellipses Copying an area Drawing an image.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
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.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
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.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Web Design & Development Lecture 18. Java Graphics.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
JAPPLET.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
tiled Map Case Study: Rendering with JPanel © Allan C. Milne v
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Lesson 34: Layering Images with Java GUI. The FlowLayout RECAP.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real support of text boxes, buttons, etc. Procedural - not OOP –No.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Creating Your Own Widgets CompSci 230 S Software Construction.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
© 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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Basics of GUI Programming Chapter 11 and Chapter 22.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Graphical User Interface (GUI) Two-Dimensional Graphical Shapes.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
EE2E1. JAVA Programming Lecture 5 Graphics programming and Swing.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Java Graphics.
Java Graphics CS 2511.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Presentation transcript:

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer

2 Drawing Outline  Drawing with a Graphics object  Graphics and Graphics2D  paintComponent and repaint  draw and fill messages  Strings, Lines, Rectangle, Ellipse, Polygons  Colors  Text and Fonts  Drawing Images  Toolkit to convert jpg and gif files into Images

3 Drawing Drawing with a Graphics Object  The use of graphics is common among modern software systems  Java has strong support for graphics  coordinate system for Java graphics  drawing shapes such as lines, ovals, rectangles,...  basic animation techniques  the use of color  the use of fonts  drawing images

4 Drawing The Coordinate System  A simple two-dimensional coordinate system exists for each graphics context or drawing surface  Each point on the coordinate system represents a single pixel  top left corner of the area is coordinate // This string will be drawn 20 pixels right, // 40 pixels down as the lower left corner; // other shapes are upper right g2.drawString("is in Panel1", 20, 40);  A drawing surface has a width and height  Anything drawn outside of that area is not visible

5 Drawing The Coordinate System x y Y X

6 Drawing Draw on a JPanel  Need to extend a class that extends JComponent  JPanel is good  To draw things:  extend JPanel  have the class override the paintComponent method  panel surface is transparent, so send drawing messages inside paintComponent to the graphic context  a Graphics2D object we'll reference with g2

7 Drawing Put something in a JPanel  Implement a JPanel class and draw a few strings import java.awt.*; public class DrawingPanel extends javax.swing.JPanel { // Override the paintComponent method in public void paintComponent(Graphics g) { super.paintComponent(g); // Always call this Graphics2D g2 = (Graphics2D) g; g2.drawString("Put this in g, which", 20, 20); g2.drawString("is in Panel", 20, 40); g2.drawString("which is in MyFrame", 20, 60); }

8 Drawing Then add the JPanel to a JFrame to see the drawing import javax.swing.JFrame; public class DrawOnAPanel extends JFrame { public static void main(String[] args) { new DrawOnAPanel().setVisible(true); } public DrawOnAPanel() { setTitle("A Frame with a panel"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(200, 120); add(new DrawingPanel()); }

9 Drawing The Graphics Object  paintComponent 's Graphics g argument represents a "graphical context" object.  You can tell it to draw things on the panel  If you want another method to draw, pass the Graphics object to it—it like a canvas that understands draws  The actual object passed to every JPanel is a Graphics2D, so you can cast to Graphics2D  Never send paintcomponent messages  send repaint() messages instead

10 Drawing The Graphics Context  Graphics is still around, but Java added an improved 2D Library in Java 1.3, use Graphics2D  Better drawing capabilities  It is analogous to using swing ( JButton, JFrame ) rather than older awt components ( Button, Frame )

11 Drawing First cast Graphics to Graphics2D  Send messages to g2 such as drawString draw fill setColor and setFont Rectangle2D body = // xcoord, ycoord, width, height new Rectangle2D.Double(30.0, 70.0, 200.0, 50.0); g2.draw(body); Shape leftWheel = // xcoord, ycoord, width, height new Ellipse2D.Double(50.0, 110.0, 50.0, 50.0); g2.setColor(Color.BLACK); g2.fill(leftWheel); // 30.0, 70.0: upper left corner Font aFont = new Font("SansSerif", Font.BOLD, 16); g2.setFont(aFont); g2.setPaint(Color.MAGENTA); g2.drawString("A car with no top", 45, 180);

12 Drawing So far…  We know how to subclass a JPanel and use a Graphics2D object as a drawing canvas inside the paintComponent method.  Sometimes we need to use an existing image rather than draw something ourselves

13 Drawing Drawing an Image  Java’s Image class in java.awt abstracts a bitmap image for use in drawing.  Images can be drawn to a panel through a Graphics object  An important Graphics2D method:  drawImage  But first…

14 Drawing How do we load an image?  java.awt contains a method that returns an image from a file on your disk Image img = ImageIO.read(new File("fileName"));  Once we have an image and a graphics object, we can draw it // some other drawing code // 'g2' is a Graphics context object and img // is an initialized Image. 12 is x, 24 is y pixels g.drawImage(img, 12, 24, null);

15 Drawing Drawing Our Image  This code will draw img at the coordinates (12, 24) on the panel  The final ‘null’ is for an ImageObserver object, which we'll not need

16 Drawing Summary  To draw a jpg or gif image 1. Extend JPanel 2. Declare Image instance variables in that class 3. Let the constructor initialize 4. Overide paintComponent 5. Call super.paintComponent() 6. get a Graphics2D object named g2 perhaps 7. send drawImage messages to g2

17 Drawing Example code that needs 6 jpg files in images public class CardsOnTheWater extends JPanel { private Image ocean, card1, card2, card3, card4, card5; public CardsOnTheWater() { try { ocean = ImageIO.read(new File("images/ocean.jpg")); card1 = ImageIO.read(new File("images/14h.jpg")); card2 = ImageIO.read(new File("images/13h.jpg")); card3 = ImageIO.read(new File("images/12h.jpg")); card4 = ImageIO.read(new File("images/11h.jpg")); card5 = ImageIO.read(new File("images/10h.jpg")); } catch (IOException e) { e.printStackTrace(); }

18 Drawing This method is called when the panel needs to be public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.drawImage(ocean, 0, 0, null); g2.drawImage(card1, 10, 10, null); g2.drawImage(card2, 30, 15, null); g2.drawImage(card3, 50, 20, null); g2.drawImage(card4, 70, 25, null); g2.drawImage(card5, 90, 30, null); }