Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.

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

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
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.
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 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.
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.
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.
Web Design & Development Lecture 18. Java 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.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
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.
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.
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.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Java Dynamic Graphics.
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:
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Review_6 AWT, Swing, ActionListener, and Graphics.
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Basics of GUI Programming Chapter 11 and Chapter 22.
GUI Components. The Swing package has numerous GUI components that can be added to a window. The Swing package has numerous GUI components that can be.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Java Swing One of the most important features of Java is its ability to draw graphics.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
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.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
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.
Intro to Graphics from Chapter 2 of Java Software Solutions
Java Graphics.
A First Look at GUI Applications
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
GUI AND GRAPHICS.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphical User Interface
Presentation transcript:

Java Review Structure of a graphics program

Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution to the problem being solved Classes are used to define new data types needed by the program –Java comes with a large collection of classes that have already been written Inheritance can be used to build on existing classes

Computer Graphics and User Interfaces Java Programs A java program consists of one or more classes Each class is defined in a file whose name is the name of the class followed by the extension.java –The class named Demo2D is defined in the file Demo2D.java One class must contain a main method The program executes by creating objects and calling their methods

Computer Graphics and User Interfaces Structure of a Java Program public class { // body of class goes here // Data members // Constructors // Methods }

Computer Graphics and User Interfaces The main method public static void main( String [] args) { // code to make the program do // what it needs to }

Computer Graphics and User Interfaces Constructors All instantiable classes have at least one constructor –the constructor is used to create an object –the name of the constructor is the same as the name of the class –a constructor does not have a return type

Computer Graphics and User Interfaces Creating Objects You declare an object ; JFrame frame; Create an object using new = new (); frame = new JFrame(); Some constructors have parameters –provide appropriate arguments when you call them frame = newFrame( "frame title");

Computer Graphics and User Interfaces Calling methods Call a method by giving the object name and the method name followed by a list of arguments if needed. ( ); frame.setTitle( "My Picture");

Computer Graphics and User Interfaces Graphical Programs Graphical programs need a window –Frame and JFrame are used for stand-alone graphical applications –Applet and JApplet are used for programs that run in a browser

Computer Graphics and User Interfaces Java Graphics Libraries AWT classes (e.g. Frame, Applet) were provided with original version of Java –import from java.awt package Swing classes (e.g. JFrame, JApplet) were added later to provide better cross-platform behavior –import from javax.swing package

Computer Graphics and User Interfaces Using JFrame Create a class that extends JFrame A JFrame has a content pane that serves as –a container for other GUI components –a place to draw graphics Create a panel to use as the content pane –use setContentPane( panel)

Computer Graphics and User Interfaces Defining a Frame class public class extends JFrame { // constructor used to create a Frame object public { // create a panel setContentPane( panel); }

Computer Graphics and User Interfaces Creating a panel Define a class that extends JPanel Override the paintComponent method which controls what is drawn in the panel Create a panel using new PanelClass panel = new PanelClass() Set the content pane of the frame to the panel

Computer Graphics and User Interfaces Defining a Panel class public class extends JPanel { // constructor if needed public void paintComponent( Graphics g) { // code for drawing here } }

Computer Graphics and User Interfaces The main method public static void main( String [] args) { FrameClass frame = new FrameClass(); frame.setVisible( true); frame.setDefaultClosingOperation( Jframe.EXIT_ON_CLOSE); }

Computer Graphics and User Interfaces Graphics Class Graphics class creates an object that can be used as a context for drawing The state of a Graphics object includes such properties as foreground color and font. Positions within the Graphics context are measured from the upper left corner and they have units of pixels.

Computer Graphics and User Interfaces Graphics Methods Get and set methods are provided for properties like color, font, stroke, … There are methods for drawing lines, rectangles, ovals, … There is a method for drawing text There is a method for displaying images

Computer Graphics and User Interfaces Graphics2D extends Graphics It has more methods than Graphics You can cast a Graphics to a Graphics2D to use these methods Graphics2D g2 = (Graphics2D)g There is a method for drawing Shape objects

Computer Graphics and User Interfaces Shapes Shape is an interface used as a supertype for various graphical shapes –Rectangle –Ellipse2D –Polygon Drawing a shape on Graphics2D g2 g2.draw( shapeObject);

Displaying a String The drawString method can be used to display text in a Graphics context. void drawString( String text, int x, int y; *The position of the text relative to x and y is shown in the figure

Computer Graphics and User Interfaces Color Class The java.awt.Color class allows us to create a color object. Color class has public constants for common colors: –Gray scale: Color.black –Primary colors: Color.red Color.green Color.blue –Secondary colors: Color.yellow Color.cyan Color.magenta –Others: Color.orange

Computer Graphics and User Interfaces Custom Colors Create custom colors by specifying three values ranging from 0 to 255 for red, green, and blue. Color pinkColor = new Color(255,175,175);