Eleventh Graphics in Java.

Slides:



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

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.
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
CSC1401 Drawing in Java - 2. Reminder from last class How do you save your modified picture? String filename = …; Picture stevePicture = new Picture(filename);
Draw Shapes Introduction to simple graphics. What is a graphics context? An instance of the Graphics class Graphics is ABSTRACT! You can extend Graphics.
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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
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.
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
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.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
Web Design & Development Lecture 18. Java Graphics.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Drawing-Turtle-and-AWT1 Drawing in Java Barb Ericson Georgia Institute of Technology Sept 21, 2009.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Georgia Institute of Technology Drawing in Java Barb Ericson Georgia Institute of Technology August 2005.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Georgia Institute of Technology Drawing in Java – part 3 Barb Ericson Georgia Institute of Technology September 2006.
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.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Introducing, the JFrame Gives us a work area beside System.out.println.
Georgia Institute of Technology Drawing in Java – part 2 Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah.
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.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Barb Ericson Georgia Institute of Technology September 2005
12 Graphics and Java 2D™.
Java Graphics.
Java Graphics CS 2511.
Building Java Programs
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Building Java Programs
Building Java Programs
JAVA 2 Design and programming of GUI
Workshop for Programming And Systems Management Teachers
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Building Java Programs
Workshop for Programming And Systems Management Teachers
Barb Ericson Georgia Institute of Technology September 2005
Presentation transcript:

eleventh Graphics in Java

Notes on SimpWin example Because of the inheritance mechanism, we can say that a SimpWin object is a JFrame object, just as we can say that, for example, a Labrador Retriever is a dog SimpWin is just a specific type of JFrame The first line in the main method is worth noting: we are creating an instance of the class we’re defining We have to do this because of the nature of main – it is a static method, and, therefore, exists independent of any SimpWin object – so we have to give it an object to work with The rest of the code in main() consists of instructions that set up the window to be displayed – this sort of functionality usually appears in a class’s constructor, as in the next example

Simple window example import javax.swing.*; public class SimpWin extends JFrame { public static void main (String [] args) { SimpWin sw = new SimpWin(); sw.setSize(300,200); sw.setTitle("Simple window"); sw.setLocation(150, 150); sw.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); sw.setAlwaysOnTop(true); sw.setVisible(true); }

Example, part 2 import javax.swing.*; public class SimpWin2 extends JFrame { public SimpWin2 () { setSize(300,200); setTitle("Simple window, part deux"); setLocation(150, 150); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setAlwaysOnTop(true); setVisible(true); } public static void main (String [] args) { SimpWin2 sw = new SimpWin2();

Notes on second part of example The second example is just like the first, except: we have explicitly defined the constructor, which contains all of the code that describes the object to be created we have limited main() to a simple call to the constructor; main() exists just as a tester method for the rest of the class Note the method call syntax within the constructor – we call the methods without an apparent calling object or class In fact, the calling object is the object being created by the constructor

A more interesting window We can now create a blank window, but we want to be able to draw pictures The JFrame class includes a method (inherited by any of its child classes) called paint(), which enables us to use the window as a blank canvas on which we can place shapes and colors The shapes and colors are controlled by an instance of a class named Graphics, which is passed to paint() as an argument

Drawing pictures with Java Java’s Graphics class contains methods for drawing lines and simple geometric figures (e.g. rectangles, ellipses) Figures can be hollow or filled with color Drawings are created within the context of an X-Y coordinate system illustrated on the next slide

Drawing pictures with Java Java’s Graphics class contains methods for drawing lines and simple geometric figures (e.g. rectangles, ellipses) Figures can be hollow or filled with color Drawings are created within the context of an X-Y coordinate system illustrated on the next slide

Java screen coordinate system

Java coordinate system and graphics objects As the previous slide illustrates, the origin point (0,0) is in the upper lefthand corner of the screen (or Container, such as a JFrame ) The X axis is horizontal and the Y axis is vertical; all values are positive A figure, such as a rectangle or ellipse, has a bounding rectangle that indicates the borders in which it is drawn in the space The upper left corner of this position and the size of the figure are specified in its constructor

Swing objects and the paint() method Most Swing components include a paint() method, which is used to draw the component on the screen This method is inherited from JComponent, an ancestor class for most of the Swing objects As an inherited method, we have been able to use it (invisibly, since none of our code has called it directly) without modifying, or overriding the original version In order to draw our own pictures, we will need to provide a new paint() definition, overriding the original

The paint() method The paint method for an object is automatically invoked when the object is made visible; there is almost never an explicit call to the method The paint() method has a single parameter of type Graphics, typically named g We use g to invoke the methods that draw pictures The Graphics class is defined in the awt package, which means we need another import statement if we want to add a paint method to our code

Example import javax.swing.*; import java.awt.*; public class BlueSquare extends JFrame { public BlueSquare () { setSize(140,160); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void paint (Graphics g) { g.setColor(Color.BLUE); g.fillRect(20,40,100,100); public static void main (String[] args) { BlueSquare b = new BlueSquare();

Learning Goals Understand at a conceptual and practical level How to draw more simple shapes with java.awt.Graphics Arcs Rectangles Bitmapped pictures versus vector graphics How to use a java.awt.Graphics2D class How to convert the Graphics class to it How to set the color How to set the paint How to set the stroke (paint brush) How to create the object to be painted How to paint the object

Drawing Arcs Arcs Outlined Arc Filled Arc g.drawArc(topLeftX, topLeftY, width, height, startAngle, arcAngle); Filled Arc g.fillArc((topLeftX, topLeftY, width, height, startAngle, arcAngle); The top left x and y in the ovals and arcs are for the top left corner of the bounding rectangle. The arcAngle is the amount to add to the startAngle to get the endAngle.

Georgia Institute of Technology Drawing Rectangles Rectangle Outlined Rectangle g.drawRect(topLeftX, topLeftY, width, height); Filled Rectangle g.fillRect(topLeftX,topLeftY,width,height); Outlined Rounded Rectangle g.drawRoundRect(topLeftX,topLeftY,width,height,arcWidth,arcHeight); Filled Rounded Rectangle g.fillRoundRect(topLeftX,topLeftY,width,height,arcWidth,arcHeight); The topLeftX and topLeftY are the coordinates of the top left point of the enclosing rectangle for the rounded rectangle. The arcWidth and arcHeight in the rounded rectangle are the width and height of the oval that creates the rounded part of the rectangle. To draw a rectangle in the background color (clear the area). g.clearRect(topLeftX,topLeftY,width,height); To draw a 3-d rectangle (looks like a button) g.draw3DRect(topLeftX, TopLeftY,width,height,isRaised); g.fill3DRect(topLeftX,TopLeftY,width,height,isRaised);

Drawing on a Blank Picture Georgia Institute of Technology Drawing on a Blank Picture You can make pictures from the “blank” files They will have all white pixels 640x480.jpg 7inX95in.jpg You can also create a “blank” picture with a width and height They will also have all white pixels Picture blankPicture = new Picture(width,height);

Draw a Picture Exercise Create a method that will draw a simple picture Use at least one rectangle Use at least one polygon Use at least one oval Use at least one arc Should this be a class method or object method? Georgia Institute of Technology

Bitmapped Versus Vector Graphics Georgia Institute of Technology Bitmapped Versus Vector Graphics We have been doing bitmapped graphics Specifying the color of each pixel in the picture We just wrote a method that drew a simple picture Which is smaller the program or the picture? Some applications use vector graphics which are programs that produce the picture Used in Postscript, Flash, and AutoCAD Advantages: smaller, easy to change, can be scaled

Georgia Institute of Technology Precision Drawings How would you draw a stack of filled rectangles starting from the lightest one at the bottom right and the darkest one at the top left. With 10 pixels between each Not easy with drawing packages Georgia Institute of Technology

Draw Filled Rectangles Method Georgia Institute of Technology Draw Filled Rectangles Method public void drawFilledRectangles() { Graphics g = this.getGraphics(); Color color = null; // loop 25 times for (int i = 25; i > 0; i--) color = new Color(i * 10, i * 5, i); g.setColor(color); g.fillRect(0,0,i*10,i*10); } Try drawing a series of filled ovals.

1 // Fig. 28.5: ShowColors.java 2 // Demonstrating Colors 3 import java.awt.*; 4 import javax.swing.*; 5 import java.awt.event.*; 6 7 public class ShowColors extends JFrame { 8 public ShowColors() 9 { 10 super( "Using colors" ); 11 12 setSize( 400, 130 ); 13 show(); 14 } 15 16 public void paint( Graphics g ) 17 { 18 // set new drawing color using integers 19 g.setColor( new Color( 255, 0, 0 ) ); 20 g.fillRect( 25, 25, 100, 20 ); 21 g.drawString( "Current RGB: " + g.getColor(), 130, 40 ); 22 23 // set new drawing color using floats 24 g.setColor( new Color( 0.0f, 1.0f, 0.0f ) ); 25 g.fillRect( 25, 50, 100, 20 ); 26 g.drawString( "Current RGB: " + g.getColor(), 130, 65 ); 27

28 // set new drawing color using static Color objects 29 g.setColor( Color.blue ); 30 g.fillRect( 25, 75, 100, 20 ); 31 g.drawString( "Current RGB: " + g.getColor(), 130, 90 );   32 33 // display individual RGB values 34 Color c = Color.magenta; 35 g.setColor( c ); 36 g.fillRect( 25, 100, 100, 20 ); 37 g.drawString( "RGB values: " + c.getRed() + ", " + 38 c.getGreen() + ", " + c.getBlue(), 130, 115 ); 39 } 40 41 public static void main( String args[] ) 42 { 43 ShowColors app = new ShowColors(); 44 45 app.addWindowListener( 46 new WindowAdapter() { 47 public void windowClosing( WindowEvent e ) 48 { 49 System.exit( 0 ); 50 } 51 } 52 ); 53 } 54 }

Graphics 2D

Java 2D Graphics – java.awt Newer drawing classes More object-oriented Instead of drawOval() or fillOval() you create a Ellipse2D object and ask a 2d graphics object to draw or fill it Geometric shapes are in the java.awt.geom package Advanced Drawing Support for different types of brushes Line thickness, dashed lines, etc Supports cubic curves and general paths Drawing of gradients and textures Move, rotate, scale and shear text and graphics Create composite images See http://java.sun.com/docs/books/tutorial/2d/ for the tutorial on 2d graphics from Sun.

How To Use Java 2D Cast the Graphics class to Graphics2D Graphics2D g2 = (Graphics2D) gObj; Set up the stroke if desired (type of pen) g2.setStroke(new BasicStroke(widthAsFloat)); Set up any Color, GradientPaint, or TexturePaint g2.setPaint(Color.blue); g2.setPaint(blueToPurpleGradient); g2.setPaint(texture); Create a geometric shape Line2D line2D = new Line2D.Double(0.0,0.0,100.0,100.0); Draw the outline of a geometric shape g2.draw(line2d); Fill a geometric shape g2.fill(rectangle2d);

Graphics2D inherits from Graphics Inherits basic drawing ability from Graphics Adds more advanced drawing ability Graphics Graphics2D Georgia Institute of Technology

Using Graphics2D to Copy an Image public void copy2D(Picture source, int x, int y) { // get the graphics object Graphics g = this.getGraphics(); Graphics g2 = (Graphics2D) g; // copy the image g2.drawImage(source.getImage(),x,y,null); } We had created a general copy method that copied a picture to a x and y location in the current picture but we had to copy all the pixels ourselves. But, there is a method that will do this for us in both Graphics and Graphics2D.