Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.

Slides:



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

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?
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
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.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
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. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
1 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
Animation To animate our programs, that is, to have real time interactions where objects move on the screen, we want to call repaint( ) every few milliseconds.
Web Design & Development Lecture 18. Java Graphics.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
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.
Copyright © 2013 by John Wiley & Sons. All rights reserved. GRAPHICAL USER INTERFACES CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft 10/30/11.
1 Graphical User Interfaces AWT and Swing packages Frames and Panels Components Nested Panels Images Reading for this Lecture: L&L, 3.9 – 3.11.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
1 Windows program example import java.awt.*; import java.awt.event.*; public class wpexample extends Frame { public wpexample(String title) { super(title);
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(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 basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Computer Science 209 The Adapter Pattern. The Context of the Adapter Pattern I want to use an existing class (the adaptee) without modifying it The context.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Applets Java code is compiled into byte code instead of machine language –Languages like C, C++, Pascal and others are compiled into machine language so.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
GUI Tutorial Day 4. More GUI action  adding a Mouse Listener  SimpleDots  Simple mouse listener  Draw an oval where the mouse is clicked  Box example.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
Break Time! Graphical User Interface (GUI) NO EXAM….ONLY PROJECT!
Chapter 8 Graphics.
Java Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Java Graphics CS 2511.
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Building Java Programs
Building Java Programs
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Building Java Programs
Chapter 8 Graphics.
Presentation transcript:

Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics object by passing the object various drawing messages –a Graphics object can only be created within certain contexts including GUI components and Applets, we will use GUI components –there is some overhead needed to do this, for now we will find the simplest approach and just use it rather than worry about why or how it is done

The Basic Idea… Create a JPanel which is one of the GUI components that includes a Graphics context Insert the JPanel into a JFrame Show the JFrame Draw on the JPanel as desired using messages like –drawLine –drawRect –fillRect –setColor –drawString Unfortunately, to do this requires at least two separate class definitions and at least 2 methods –a skeleton is given next, we will use this for our graphics programs, at least for now, where all you have to do is fill in the paintComponent method with your Graphics messages

Graphics Skeleton import javax.swing.*; import java.awt.*; public class GraphicsSkeleton { private static final int X_SIZE = 300, Y_SIZE = 300; public static void main(String[ ] args) { JFrame frame = new JFrame(“Graphics Skeleton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(X_SIZE, Y_SIZE); GraphicsPanel panel = new GraphicsPanel( ); frame.add(panel); frame.setVisible(true); } private static class GraphicsPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); // Add drawing code here. } Here’s where your Graphics code will go Alter the size of the Graphics window here

Message Passing Recall that we use message passing when we want to command an object to perform some action –For a String, we did things like name.toUpperCase( ) and the random number generator was generator.nextInt( ), and so forth –Graphics is a class, and we will use a Graphics object, so we will have to send it messages –The Graphics object is automatically created for us by the JPanel, so all we have to do is get ahold of it (in a method called paintComponent) and pass it messages –Example: (assume the Graphics object is called g) g.drawLine(5,10,25,30);// draws a line from to –There are numerous Graphics messages, we look at the important ones next

Graphics Messages –drawLine(x1, y1, x2, y2) is the source point, is the destination point –drawRect(x, y, width, height) and fillRect(x, y, width, height) Draw/fill rectangle starting at with width and height as specified –draw3DRect/fill3DRect(x, y, width, height, bool) Bool is true or false denoting whether the rectangle is raised (true) or indented (false) –drawRoundRect/fillRoundRect(x, y, width, height, awd, aht) The corners of the rectangle are rounded using awd and aht as the height and width of the arc for the corners –drawOval/fillOval(x, y, width, height) draws or fills an oval that fills a “bounded box” starting at point with height and width as specified –drawString(str, x, y) prints the String str at point –NOTE: x1, y1, x2, y2, x, y, width, height, awd and aht are all int values

Additional Graphics Messages –setColor(col) changes the current drawing color, col must be of type Color and is usually denoted as Color.name such as Color.blue or Color.red –or create a color using RGB values as in new Color(128, 255, 0); –setFont(ft) changes the font for drawString commands to a different font, ft must be of type Font –drawPolygon/fillPolygon(x[ ], y[ ], num) draws/fills a polygon of points where each point is stored as one element in each of arrays x and y, and where there are num points –drawArc/fillArc(x, y, width, height, sAngle, aAngle) like draw/fillOval but only draws a portion based on the two angles provided –drawImage(src, x, y, width, height, col, obsv) used to display an image src (gif, jpg) – col is the background color for any transparent pixels, and obsv is an ImageObserver, use “this” –Note: col is a Color, ft is a Font, x[ ] and y[ ] are arrays of ints, num, x, y, width, height, sAngle, aAngle, are all ints, and obsv should be this

Some Graphics Examples public void paintComponent(Graphics g) { g.setColor(Color.blue); g.drawRoundRect(10, 10, 200, 200, 25, 25); g.setColor(Color.green); g.drawRoundRect(40, 40, 140, 140, 15, 15); g.setColor(Color.red); g.drawRoundRect(70, 70, 80, 80, 10, 10); g.setColor(Color.yellow); g.fillRoundRect(100, 100, 20, 20, 5, 5); } public void paintComponent(Graphics g) { g.setColor(new Color(128, 64, 128)); g.draw3DRect(10, 10, 100, 100, false); g.draw3DRect(20, 20, 80, 80, true); g.setColor(new Color(255, 0, 64)); g.fill3DRect(30, 30, 60, 60, true); g.setColor(Color.white); g.fill3DRect(40, 40, 40, 40, false); }

More Examples public void paintComponent(Graphics g) { g.setFont(new Font("Ariel", Font.BOLD, 20)); g.drawString("Hello", 10, 20); g.setFont(new Font("Chicago", Font.ITALIC, 28)); g.drawString("Goodbye", 10, 40); } public void paintComponent(Graphics g) { g.setColor(Color.red); int[ ] x = {15, 30, 60, 25, 35, 45, 10, 30, 45, 20, 10, 25}; int[ ] y = {10, 25, 40, 55, 30, 15, 30, 45, 60, 45, 30, 10}; g.fillPolygon(x, y, 12); }

And More Examples public void paintComponent(Graphics g) { ImageIcon icon = new ImageIcon("flag.gif"); Image image = icon.getImage( ); g.drawImage(image, 10, 10, 300, 200, Color.white, this); } public void paintComponent(Graphics g) { g.setColor(Color.blue); g.fillArc(10, 10, 100, 100, 0, 45); g.setColor(Color.red); g.fillArc(10, 10, 100, 100, 45, 45); g.setColor(Color.green); g.fillArc(10, 10, 100, 100, 90, 45); g.setColor(Color.yellow); g.fillArc(10, 10, 100, 100, 135, 45); }

Using Loops public void paintComponent(Graphics g) { super.repaint(); int i; for(i=0;i<255;i++) { g.setColor(new Color(i, 128, 128)); g.drawLine(i,0,i,300); } public void paintComponent(Graphics g) { super.repaint(); int i, j; for(i=0;i<255;i++) for(j=0;j<255;j++) if(j%2= =0) { g.setColor(new Color(i, j, 128)); g.drawLine(i,j,i,j+1); }

Drawing a Flag public void paintComponent(Graphics g) { super.repaint(); int i; g.setColor(Color.blue); g.fillRect(0,0,60,60); for(i=0;i<6;i++) { if(i%2==0) g.setColor(Color.red); else g.setColor(Color.white); g.fillRect(60,i*10,140,10); } for(i=6;i<13;i++) { if(i%2==0) g.setColor(Color.red); else g.setColor(Color.white); g.fillRect(0,i*10,200,10); } How would you add the 50 stars?

Some Comments The paintComponent method is automatically called whenever –your code calls repaint( ) –the JFrame first appears on the screen so you don’t need to call repaint( ), but it’s a good habit to get into –the JFrame is resized or altered on the screen You cannot automatically generate a Graphics object, so you must rely on the JPanel doing it for you –this is inconvenient and causes you to include all the JFrame and JPanel code that we don’t normally want or we would have to generate the drawing in an Applet, which is just as much work It is helpful to work out the size of your drawing components before starting –you need to know the size for the JFrame, JPanel and the values used in your messages Sizes, dimensions, end points, etc are all in terms of pixels, starting at in the upper left hand corner of the JPanel

Animating a Program Animation requires displaying images very quickly –We will want to slightly alter the image in between each display –We call super.paintComponent(g) in paintComponent which “clears the Graphics object screen –We then draw a new (slightly different) image The instruction panel.repaint(); will cause paintComponent to execute –We will continually call panel.repaint(); but place a delay in between each call: while(true) { for(long i=0;i< l;i++); panel.repaint(); } notice the ; here

Example import javax.swing.*; import java.awt.*; import java.util.*; public class GraphicsSkeleton { private static final int X_SIZE = 300, Y_SIZE = 300; private static int x=150,y=150; // the item being animated public static void main(String[ ] args) { JFrame frame = new JFrame(“Graphics Skeleton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(X_SIZE, Y_SIZE); GraphicsPanel panel = new GraphicsPanel( ); frame.add(panel); frame.setVisible(true); Random g=new Random(); long i; while(true) { x+=g.nextInt(3)-1; y+=g.nextInt(3)-1; panel.repaint(); for(i=0;i< l;i++); } }

Continued private static class GraphicsPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.black); g.fillRect(0,0,X_SIZE,Y_SIZE); g.setColor(Color.red); if(x<25) x=25; else if(x>275) x=275; if(y<25) y=25; else if(y>275) y=275; g.drawOval(x-2,y-2,5,5); }

Another Approach: Input to Pause Instead of using a for loop to pause the program, we can force the program to pause by asking for input This requires that the user control when the next “turn” takes place – for(i=0;i< l;i++); Instead of the above for loop we use –in.nextLine(); –in is a Scanner, nextLine inputs the next line including just an empty line by pressing –With this command, the user pressed to move on to the next turn