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.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
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.
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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?
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
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.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
1 L37 Graphics and Java 2D™ (2). 2 OBJECTIVES To use methods of class Graphics to draw lines,  rectangles,  rectangles with rounded corners,  three-dimensional.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
©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.
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.
©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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
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.
A Simple Applet.
Chapter 5 Graphics. Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
1 Interface Types & Polymorphism & introduction to graphics programming in 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.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
METHODS AND SCOPE CSCE More on Methods  This is chapter 6 in Small Java.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
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.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
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.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
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.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Lecture # 6 Graphical User Interface(GUI). Introduction A graphical user interface (GUI) presents a user- friendly mechanism for interacting with an application.
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.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
Introducing, the JFrame Gives us a work area beside System.out.println.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Intro to Graphics from Chapter 2 of Java Software Solutions
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
JAVA 2 Design and programming of GUI
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Classes.
Presentation transcript:

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 cross-platform graphical user interface The basic window component in this package is the JFrame Previously, we saw an example in which a JFrame object was included in a class so that a JOptionPane object had a parent window; that example is repeated on the next slide

Example import javax.swing.*; class Example2 { public static void main (String [] args) { JFrame myWindow; myWindow = new JFrame(); myWindow.setSize (300, 200); myWindow.setVisible(true); JOptionPane.showMessageDialog(myWindow, “It’s my window”); JOptionPane.showMessageDialog(null, “and I’ll cry \n if I want to”); }

A new way: the window is the program In the previous example, we saw that JFrame objects come with member methods, such as setSize and setVisible We will see examples of other member methods throughout this lecture For many GUI programs, having a JFrame component isn’t enough to provide all the functionality we need inheritance In such a case, we can employ a mechanism called inheritance to create a program in which the class we define is a kind of window

Inheritance Inheritance is a powerful OOP tool; we will barely scratch the surface of its utility We can loosely define inheritance as the ability of one class to take on all of the characteristics of another class – the class that receives (or inherits) is called the child or subclass – the class that provides is called the parent or superclass We will confine our discussion of inheritance (for now) to the use of inherited characteristics by subclasses

Inheritance in Java extends We can declare our intention to inherit characteristics from another class by including the keyword extends and the name of the parent class in our class heading; example: public class Picture extends JFrame { This heading indicates that class Picture has all the characteristics (and can use all of the member methods) of class JFrame A simple example follows

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); }

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

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

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(); }

Example The code from the previous slide (along with the constructor and main method) produces this result: The 100 x 100 pixel square is placed within a 140 x 160 window with its upper left corner at position (20, 40)

Graphics drawing methods Shape drawing methods include: – drawRect – rectangle outline – drawOval – ellipse outline – fillRect – filled rectangle – fillOval – filled ellipse Each takes four int arguments – 1 st and 2 nd : x,y coordinates of bounding rectangle’s upper left corner – 3 rd and 4 th : width and length of figure

Graphics drawing methods drawRoundRect and fillRoundRect: rectangles with rounded corners – First 4 arguments to these are the same as for the rectangle methods – 2 additional int arguments specify the width and height of the corner arcs

Graphics drawing methods draw3DRect and fill3DRect: rectangles with slightly raised edges – First 4 arguments same as other rectangles – Last argument is boolean If true, the foreground color has the raised appearance with a small background color edge If false, the background color is the raised part, with a small foreground color edge

Graphics drawing methods drawLine: draws a line between two points specified by 4 int arguments: – 1 st and 3 rd arguments are the x coordinates – 2 nd and 4 th are the y coordinates

Graphics drawing methods drawArc and fillArc: draw partial ellipses within bounding rectangles; each takes 6 int arguments: – 1 st and 2 nd : xy position of upper left corner of bounding rectangle – 3 rd and 4 th : width and height of bounding rectangle – 5 th : start of drawing arc (number between 0 and 359) – 6 th : sweep of drawing arc (number of degrees of arc sweep) A positive sweep value draws arc in clockwise direction A negative sweep value draws arc in counterclockwise direction

Specifying an Arc (Part 1 of 2)

Specifying an Arc (Part 2 of 2)

The Color class As we have seen, we can draw shapes using Graphics methods To specify the color with which to draw, we use the setColor method, which takes an argument of type Color The color class contains both predefined colors and the ability to construct our own colors

Predefined Colors The Color class contains constants named for each of its predefined colors; they include Color.BLUE, Color.RED, Color.YELLOW, Color.WHITE, Color.BLACK, Color.CYAN, Color.GREEN, Color.MAGENTA, Color.ORANGE, Color.PINK and three shades of GRAY You can create your own Color objects using these constants; for example: Color lg = new Color(Color.LIGHT_GRAY);

More code examples … FinalPic.java Sampexam.java Shapes.java

Beyond the basic Crayola  set You are not limited to the predefined colors; another version of the Color constructor takes 3 int arguments, each of which must be a value between 0 and 255 The first argument represents the amount of red in the color; the other two represent green and blue, respectively

One more example JFrameEx4.java