Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.

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 Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
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.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Computer Science 209 Graphics and GUIs. Working with Color The class java.awt.Color includes constants for typical color values and also supports the.
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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
©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.
Swinging Into Swing Leo S. Primero III. Understanding what Swing Is Swing is a package that lets you create applications that use a flashy Graphical User.
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 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.
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.
Week 9 Generalization, inheritance, class hierarchy and 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.
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.
Object-Oriented Programming in Java. Object-Oriented Programming Objects are the basic building blocks in an O-O program. – A program consists of one.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
JAPPLET.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
GUI in Java. Graphical User Interface Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides users with basic level of.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
© A+ Computer Science - Chicken yeller = new Chicken();
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CSE 219 Computer Science III Image Manipulation. HW 1 Has been posted on Blackboard Making a Game of Life with limited.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
EVENTS Wiring together objects, code, and actions.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
© 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)
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 2 – Welcome Application Introduction to Graphical.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
Advanced OOP MCS-3 OOP BSCS-3 Lecture # 6. TOPICS TO COVER GUI Components - > JframeGUI Components - > Jframe (javax.Swing.JFrame) JFrame() JFrame(String)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
TCU CoSc Programming with Java The JFrame Class.
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.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
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.
1 Sections 5.1 – 5.2 Digital Image Processing Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
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.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Java Swing.
Gimp Guide Mr Hall.
Basic Graphics Drawing Shapes 1.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
A+ Computer Science METHODS.
Class Hierarchies and Type Conformance
A+ Computer Science METHODS.
Presentation transcript:

Java Graphics Graphical Components as objects

Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains children  Other components that constitute the ‘parent’ component  Can only contain children if the Component is a Container

Components Java contains some pre-defined types of components ◦JFrame ◦JButton ◦JLabel ◦JTextField ◦JSlider We will also use customized components ◦Oval ◦Rectangle ◦Line ◦EventButton ◦EventField ◦EventSlider

Components All Components have the following traits java.awt.Component -int x,y,width,height -Color background + void repaint( ) + int getX() + int getY() + int getHeight() + int getWidth() +Color getBackground() + void setBackground(Color) + void setBounds(int, int, int, int) + void setLayout(LayoutManager) + void setLocation(int, int) + void setSize(int,int) + void setVisible(boolean)

Components All Containers are Components that have the following traits java.awt.Container +Container() +void add(Component) +void add(Component, int) +void remove(Component) +void removeAll() +void setLayout(LayoutManager)

JFrame JFrame -String title +JFrame(String) A JFrame is a Container A JFrame is a Window A JFrame has a ‘title’.

JFrame Class Specification Invariant A JFrame object is a rectangular window. is placed on a computer screen with its upper-left corner x pixels from the left and y pixels from the top. (The upper left corner of the screen is (0, 0).) has usable dimension of width pixels from side to side and height pixels from top to bottom; this region has a background color of backColor. has title displayed in the window's title bar Invariant A JFrame object is a rectangular window. is placed on a computer screen with its upper-left corner x pixels from the left and y pixels from the top. (The upper left corner of the screen is (0, 0).) has usable dimension of width pixels from side to side and height pixels from top to bottom; this region has a background color of backColor. has title displayed in the window's title bar

JFrame Class Specification Constructor public JFrame(String s) post: a new JFrame (window) object is created and s is displayed in the window’s title bar note: this method call needs to be followed by calls to setBounds and setVisible Update Methods public void add(java.awt.Component pic, int j) pre: j ==0 for best results post: image pic will be drawn upon this JFrame public void remove(java.awt.Component pic) post: image pic will be removed from this JFrame (assuming it was previously added) public void repaint() post: this JFrame is marked to be redrawn as soon as possible public void setBounds(int newX, int newY, int w, int h) pre: w >= 0 and h >= 0 post: x == newX and y == newY and width == w and height == h Constructor public JFrame(String s) post: a new JFrame (window) object is created and s is displayed in the window’s title bar note: this method call needs to be followed by calls to setBounds and setVisible Update Methods public void add(java.awt.Component pic, int j) pre: j ==0 for best results post: image pic will be drawn upon this JFrame public void remove(java.awt.Component pic) post: image pic will be removed from this JFrame (assuming it was previously added) public void repaint() post: this JFrame is marked to be redrawn as soon as possible public void setBounds(int newX, int newY, int w, int h) pre: w >= 0 and h >= 0 post: x == newX and y == newY and width == w and height == h

JFrame Class Specification Update Methods public void setBackground(Color c) post: backColor == c public void setLayout(LayoutManager m) pre: m == null (for our purposes) post: added objects are rearranged via m public void setLocation(int newX, int newY) post: x == newX && y == newY public void setSize(int w, int h) pre: w >= 0 and h >= 0 post: width == w && height == h public void setVisible(boolean b) post: b == true implies this JFrame is made visible and brought to the front Update Methods public void setBackground(Color c) post: backColor == c public void setLayout(LayoutManager m) pre: m == null (for our purposes) post: added objects are rearranged via m public void setLocation(int newX, int newY) post: x == newX && y == newY public void setSize(int w, int h) pre: w >= 0 and h >= 0 post: width == w && height == h public void setVisible(boolean b) post: b == true implies this JFrame is made visible and brought to the front

Example (note ‘import’) import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame blackWin, greenWin; public Driver() { blackWin = new JFrame("Mine"); blackWin.setBounds(10, 10, 100, 200); blackWin.setLayout(null); blackWin.setBackground(Color.black); blackWin.setVisible(true); greenWin = new JFrame("Yours"); greenWin.setBounds(150, 100, 100, 50); greenWin.setLayout(null); greenWin.ssetVisible(true); greenWin.setBackground(Color.green); } import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame blackWin, greenWin; public Driver() { blackWin = new JFrame("Mine"); blackWin.setBounds(10, 10, 100, 200); blackWin.setLayout(null); blackWin.setBackground(Color.black); blackWin.setVisible(true); greenWin = new JFrame("Yours"); greenWin.setBounds(150, 100, 100, 50); greenWin.setLayout(null); greenWin.ssetVisible(true); greenWin.setBackground(Color.green); }

The add method A Component can be added to any Container ◦Containers include Oval, Rectangle, JFrame ◦Adding a ‘child’ to a ‘parent’  Positions the child with respect to the parent  A child can either zero or 1 parent  If a child has no parent then it is not ‘seen’  Children are added in order, so can be hidden or obscured by other children

The JLabel JLabel -int x,y,width,height -Color background +JLabel(String) + void setText(String) + String getText(String) import java.awt.*; import javax.swing.JFrame; public class Driver { private JFrame win; private Label wiLabel; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 200); win.setLayout(null); win.setVisible(true); wiLabel = new JLabel("Wisconsin"); wiLabel.setBounds(10, 40, 120, 20); wiLabel.setForeground(Color.blue); wiLabel.repaint(); win.add(wiLabel, 0); } import java.awt.*; import javax.swing.JFrame; public class Driver { private JFrame win; private Label wiLabel; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 200); win.setLayout(null); win.setVisible(true); wiLabel = new JLabel("Wisconsin"); wiLabel.setBounds(10, 40, 120, 20); wiLabel.setForeground(Color.blue); wiLabel.repaint(); win.add(wiLabel, 0); } Note: must import java.awt.*

The Rectangle Rectangle -int x,y,width,height -Color background +Rectangle(int,int,int,int) import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private Rectangle box; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 200); win.setLayout(null); win.setVisible(true); box = new Rectangle(50, 50, 10, 10); box.setBackground(Color.magenta); box.repaint(); win.add(box, 0); } import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private Rectangle box; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 200); win.setLayout(null); win.setVisible(true); box = new Rectangle(50, 50, 10, 10); box.setBackground(Color.magenta); box.repaint(); win.add(box, 0); } Note: no import required for Rectangle, but Rectangle.class file must be in same folder as Driver.class.

The Oval Oval -int x,y,width,height -Color background +Oval(int,int,int,int) import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private Oval dot; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 200); win.setLayout(null); win.setVisible(true); dot = new Oval(50, 50, 10, 10); dot.setBackground(Color.magenta); dot.repaint(); win.add(dot, 0); } import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; private Oval dot; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 200); win.setLayout(null); win.setVisible(true); dot = new Oval(50, 50, 10, 10); dot.setBackground(Color.magenta); dot.repaint(); win.add(dot, 0); } Note: no import required for Rectangle, but Rectangle.class file must be in same folder as Driver.class.

Drawing rules for children Children are ‘ordered’ ◦Children at the beginning are on top ◦Children at the end are on the bottom Two ways of adding ◦add(Component) – adds at the end ◦add(Component, int) – adds at the given position Top layers may obscure lower layers Children are always ‘clipped’ to their parents

Repaint Repaint is sometimes needed to make sure that changes to an object are properly displayed Repaint should usually be used after making some change to a component.

Example import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win, bottomWindow; private Rectangle grayRect; private Oval redOval, whiteDot; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 100); win.setLayout(null); win.setBackground( Color.lightGray ); win.setVisible(true); grayRect = new Rectangle(40, 40, 40, 50); grayRect.setBackground( Color.darkGray ); win.add(grayRect, 0); whiteDot = new Oval(10, 10, 80, 80); whiteDot.setBackground( Color.white ); grayRect.add(whiteDot, 0); redOval = new Oval(60, -20, 100, 50); redOval.setBackground( Color.red ); win.add(redOval, 0); win.repaint(); } import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win, bottomWindow; private Rectangle grayRect; private Oval redOval, whiteDot; public Driver() { win = new JFrame("the window"); win.setBounds(10, 10, 200, 100); win.setLayout(null); win.setBackground( Color.lightGray ); win.setVisible(true); grayRect = new Rectangle(40, 40, 40, 50); grayRect.setBackground( Color.darkGray ); win.add(grayRect, 0); whiteDot = new Oval(10, 10, 80, 80); whiteDot.setBackground( Color.white ); grayRect.add(whiteDot, 0); redOval = new Oval(60, -20, 100, 50); redOval.setBackground( Color.red ); win.add(redOval, 0); win.repaint(); }

Experiment Write a program that draws this picture in the center of a 500 by 500 window. ◦The “box” is 200 by 200 ◦The “circle” is 100 by 100

Another Supplier Example When using ‘graphical’ components ◦The data is called a ‘model’ ◦The drawing is called a ‘component’ or ‘view’ ◦No class should have both data and view mixed together! Consider a BeerMug ◦Develop a BeerMugModel ◦Develop a BeerMugComponent

BeerMugModel BeerMugModel +BeerMugModel(int capacity) +void fill(int ozs) +void drink(int ozs) +int getLevel() +int getCapacity() +int getPercentFull() +int getTotalAmountDrunk() +boolean isEmpty() +boolean isFull() What is the UML Class Diagram for BeerMugModel?

BeerMugComponent Only task is to ‘draw’ a BeerMugModel! ◦Need to know the location (x,y) and dimension (w,h) ◦Possibly allow clients to choose color schemes (we won’t)

Component design Must define ratios ◦Given the width and the height of the mug, compute the dimensions and locations of everything else W W HH W/4 3*W/4 H/5 H/4