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.

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

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.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
©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 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
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.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
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 ( 李德成 )
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
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.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Layout Managers Arranges and lays out the GUI components on a container.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
© 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.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Review_6 AWT, Swing, ActionListener, and Graphics.
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.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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.
Java Applets.
Chapter 8 Graphics.
Java Applets.
Event Handling CS 21a: Introduction to Computing I
Chapter 10 Graphics.
Java Applets.
Java Applets.
Chapter 8 Graphics.
Advanced GUIs and Graphics
Presentation transcript:

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 Slide 2 Drawing your own stuff Java provides a Component that allows you to draw on the screen -- Canvas You can draw things like Basic shapes – rectangles, ovals, polygons, lines Text – various font types Images --.GIF,.JPG,.PNG This is essential to making your own games or your own components

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 3 Canvas Canvas, like JApplet or JFrame, must be extended in order to be used Need to override the paint() method Signature: public void paint( Graphics g ) Inside the paint() method, invoke drawing commands on the Graphics object Once created, the Canvas class must be instantiated and added to the content pane of the JApplet/JFrame

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 4 Example: FaceCanvas import javax.swing.*; import java.awt.*; public class FaceFrame extends JFrame { private FaceCanvas myCanvas; public FaceFrame() { myCanvas = new FaceCanvas(); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.add( myCanvas ); } import java.awt.*; public class FaceCanvas extends Canvas { private int xpos; private int ypos; public FaceCanvas() { setBackground( Color.blue ); setSize( 300, 300 ); xpos = 50; ypos = 50; } public void paint( Graphics g ) { g.drawOval( xpos, ypos, 15, 15 ); //head g.drawLine( xpos + 5, ypos + 10, xpos + 10, ypos + 10 ); // mouth } Instantiate and add Canvas object to Frame or Applet Extend Canvas and override paint() method

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 5 Graphics This class is what is used to create drawings on the Canvas The Graphics class can do the following set the current drawing color -> g.setColor(…) draw lines -> g.drawLine( … ) draw outline rectangles/ovals/polygons -> g.drawRect( … ), g.drawOval( … ) draw filled rectangles/ovals/polygons -> g.fillRect( … ), g.fillOval( … ) draw text draw images

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 6 Buttons and animation It can be arranged so that button actions cause the drawing on the canvas to be updated Through the actionPerformed() method What to do: Add a method in your canvas class that updates its drawing attributes (xpos & ypos, in the example) Call repaint() after updating the drawing attributes (causes paint() to be called, and the canvas is refreshed) Call that method when a button is clicked

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 7 … public class FaceFrame extends JFrame implements ActionListener { private JButton move; private FaceCanvas myCanvas; public FaceFrame() { move = new JButton( "Move" ); myCanvas = new FaceCanvas(); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.add( move ); c.add( myCanvas ); move.addActionListener( this ); } public void actionPerformed( ActionEvent ae ) { if ( ae.getSource() == move ) { myCanvas.moveRight(); } import java.awt.*; public class FaceCanvas extends Canvas { private int xpos; private int ypos; … public void moveRight() { xpos += 5; repaint(); } public void paint( Graphics g ) { g.drawOval( xpos, ypos, 15, 15 ); //head g.drawLine( xpos + 5, ypos + 10, xpos + 10, ypos + 10 ); // mouth }

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 8 Encapsulating shapes Particularly if the canvas contains many shapes, it is better (and more object-oriented) to define classes for the shapes to be drawn The canvas class becomes a container or aggregate of the shape(s) Shape objects are created inside the canvas class The paint() method contains calls to methods that draw the shapes Drawing details are encapsulated in shape class

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 9 import java.awt.*; public class FaceCanvas extends Canvas { private Face face; public FaceCanvas() { setBackground( Color.blue ); setSize( 300, 300 ); face = new Face( 50, 50 ); } public void moveRight() { face.moveRight(); repaint(); } public void paint( Graphics g ) { face.draw( g ); } import java.awt.*; public class Face { private int xpos; private int ypos; public Face( int x, int y ) { xpos = x; ypos = y; } public void moveRight() { xpos += 5; } public void draw( Graphics g ) { g.drawOval( xpos, ypos, 15, 15 ); g.drawLine( xpos + 5, ypos + 10, xpos + 10, ypos + 10 ); }

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 10 Benefits of encapsulation With this new design, it is easier to display similar shapes in the canvas Code is more organized, java source files are shorter Can add other kinds of shapes into the canvas while keeping all other classes manageably short Can use arrays (or ArrayLists) and inheritance to maintain the different shape objects and apply drawing changes in a uniform way to these objects

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 11 More about Graphics Color Fonts Images

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 12 Colors Basic shapes, text and lines use the current color this can be changed using setColor(Color c) in the Graphics class Colors can be taken from a list of constants in the Color class e.g. Color.black, Color.white, etc. Colors can also be created using Red, Green, Blue components via following constructor Color(int red, int green, int blue) e.g. Color c = new Color(128,0,128) the values of red, green, blue must each be from 0-255

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 13 Fonts Text drawing using drawString() uses the current color and the current font this can be changed using setFont(Font f) in the Graphics class Font object are obtained using a String describing the font via following constructor Font(String name, int style, int size)

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 14 Fonts The following styles are available Font.PLAIN Font.BOLD FONT.ITALIC these can be combined using the | operator Examples: Font f = new Font ("ArialBlack", Font.ITALIC | Font.BOLD, 36) Font f = new Font (“Times New Roman", Font.BOLD, 12)

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 15 Images (optional) Images are important for any applications to make it appealing to the eye e.g. for games or colorful GUIs In order to load images one needs to import the following packages import java.awt.image.*; import javax.imageio.*; import java.io.*; Sample code: ImageApplet and ImageCanvas

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 16 ImageIO The ImageIO package is a new package in Java related purely with loading and saving images of different formats. It automatically detects the format of the image file you intend to load no need to specify the exact format all you need to do is use the read(URL url) method A URL is a special way of representing where a resource can be found, e.g. a file in a directory or a file on the Internet BufferedImage img = ImageIO.read(getClass().getResource(filename)) ImageIO.read() returns an object of type BufferedImage which can be drawn using Graphics.drawImage() methods

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 17 import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.image.*; import javax.imageio.*; public class ImageCanvas extends Canvas { BufferedImage image = null; public ImageCanvas() { try { image = ImageIO.read(getClass().getResource("java.GIF")); } catch (Exception e) { } } public void paint( Graphics g ) { g.drawImage(image, 100,100, null); } ImageIO Example

9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas Slide 18 Exercises Start with the second version of the FaceFrame example Have two faces instead of one face displayed Arrange it so that the constructor of the Face class includes face size as a third parameter (have different sizes for the two faces in the canvas) Add a button that moves the faces to the left Add a button that that increases the size of the faces Add eyes to the faces (update the draw method of the Face class)