Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.

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

Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
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.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
©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.
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.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
©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.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
A Simple Applet.
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.
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.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 14 Java Fundamentals 2D Graphics Wed. 10/11/00.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Graphics in Java Applications The Graphics Class The Canvas Class The MouseListener.
10/24/20151 Java GUI Programming. 10/24/20152 What is a GUI? Java has standard packages for creating custom Graphical User Interfaces Some of the fundamental.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
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 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.
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.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
Building Java Programs Graphics Reading: Supplement 3G.
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.
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.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Object Oriented Programming
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
A note on Programming Assignment
Building Java Programs
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 8 Graphics.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Graphics Reading: Supplement 3G
Building Java Programs
Presentation transcript:

Chapter 7 Graphics

© Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements. 2 © Daly and Wrigley

Graphical User Interfaces Swing Set ▫ Flexible cross-platform GUIs that allow windows to appear in a similar format on different operating systems. ▫ Start with a “J” (example: JFrame) ▫ Import: import javax.swing.*; Abstract Windowing Toolkit ▫ Older GUI components, change colors, or change fonts ▫ Import: import java.awt.*; 3

© Daly and Wrigley JFrame Before GUI components can be placed onto the screen, a window must first be created to hold these components. JFrame frame = new JFrame("Title of window here "); frame.setSize(200, 100); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); 4

© Daly and Wrigley Container Every JFrame has a container called a content pane. Purpose - provide a visual area in which to place GUI components. Container content = frame.getContentPane(); content.setBackground(Color.YELLOW); 5

© Daly and Wrigley Null Layouts Place components wherever you would like on the container by using x and y coordinates. To add this JComponent to your content pane called content: content.add(this); 6

© Daly and Wrigley Graphic Components The AWT (Abstract Windowing Toolkit) includes methods for drawing many different types of shapes, everything from lines to polygons; using colors; using fonts; and drawing images. Painting: allows us to draw graphics on the screen ▫ Paint takes an argument of a Graphics object ▫ JComponent has a paint method associated with it ▫ g.drawLine tells the computer to do the method called drawLine on the Graphics object called g. public void paint ( Graphics g ) { } 7

© Daly and Wrigley Strings g.drawString("This is great", 20,50); 8

© Daly and Wrigley Changing the Font Typeface( font name): Helvetica, Courier, TimesRoman, etc. Style: Font.PLAIN, Font.BOLD, Font.ITALIC, Font.BOLD+Font.ITALIC Size: Point size such as 24 point. Note: These are points -- not pixels) The standard typewriter size font is 12 point. g.setFont(new Font ("TimesRoman", Font.ITALIC, 72) ); You can also create a font object (instance): Font f = new Font("TimesRoman", Font.BOLD, 36); g.setFont(f) ; 9

© Daly and Wrigley Adding Color The 13 predefined colors are: white, black, lightGray, gray, darkGray, red, green, blue, yellow, magenta, cyan, pink and orange. g.setColor(Color.GREEN); Define your own color: g.setColor(new Color(100,50,25)); Color myTeal = new Color (0,128,128); g.setColor(myTeal); 10

© Daly and Wrigley Drawing Lines g.drawLine(0,0,50,50); g.drawLine(50,0,50,75); 11

© Daly and Wrigley Drawing Rectangles 1.g.drawRect(0, 0, 50, 25); 2. g.setColor(Color.GREEN); g.fillRect(100,0,50,40); 3. g.setColor(Color.BLACK); g.drawRoundRect(175,0,50,50,20,20); (20 pixel curve) 4. g.setColor(Color.RED); g.fillRoundRect(0,75,50,50,35,35); (35 pixel curve) 5. g.setColor(Color.BLACK); g.fillRect(100,100,50,50); g.clearRect(120,120,10,10); 6. g.draw3DRect(175,100,50,30,true); (true means raised) 7. g.draw3DRect(250,100,50,20,false); (false means indented) 8. g.setColor(Color.DARKGRAY); g.fill3DRect(250,175,50,20,false); (false means indented) 12

© Daly and Wrigley Drawing Ovals g.setColor(Color.BLUE); g.drawOval(0,0,30,60); // draws an oval starting at point 0,0 width=30, height =60 // see the first oval below g.fillOval(50,0,100,40); // draws oval starting pt is 50,0 width =100, height = 40 // see the second filled in oval below 13

© Daly and Wrigley Drawing Arcs g.drawArc(0, 40, 50, 50, 0, 75); // the first arc shown below // Picture an oval with upperleft corner of its rectangle at 0,40 and its 50 wide and 50 high // starting angle is 0 which is 3 o'clock // degrees is 75 which means to go counterclockwise 75 degrees. g.fillArc(100, 75, 50, 50, 90, 180); // the filled in black arc below // Picture an oval with upperleft corner of its rectangle at 100,75 and its 50 wide and 50 high // starting angle is 90 which is 12 o'clock // degrees is 180 which means to go counterclockwise 180 degrees // the same arc could be drawn clockwise with g.fillArc(100, 75, 50, 50, 270, -180); The syntax is: g.drawArc (x, y, width, height, startangle, degrees); g.fillArc (x, y, width, height, startangle, degree s); 14