Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved 3.1.2 javax.swing.JFrame - int x - int y - int width - int.

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

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.
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?
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.
1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
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.
©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.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
Graphics Images – PictureBox control Drawing graphics - Graphics object Multimedia controls PictureBox control Image property – select image Choose how.
Object-Oriented Programming in Java. Object-Oriented Programming Objects are the basic building blocks in an O-O program. – A program consists of one.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Graphics and Multimedia Part #2
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
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.
© 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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
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.
Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.
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.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
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.
Classes. Preparation Scene so far has been background material and experience –Computing systems and problem solving –Variables –Types –Input and output.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Clicker quick questions 10/17/13 CSE 1102 Fall 2013.
© 2006 Pearson Addison-Wesley. All rights reserved DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()
© 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.
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.
Images Part 11 dbg. 2 Images The form and several controls can display a number of different types of image files in the BackgroundImage Property. These.
© 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.
Adobe Photoshop T.Ahlam Algharasi. Adobe Photoshop Adobe Photoshop is a seriously powerful photo and image edit ( treating and manipulation, compositing,
© 2006 Pearson Addison-Wesley. All rights reserved Design by Prototype Programmers often approach a problem by creating a series of prototypes. Each.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Lecture 4.1 More About Methods - Using a Prototyping Approach.
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.
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.
Computer Science 209 Graphics and GUIs.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
AWT Components and Containers
A+ Computer Science METHODS.
A+ Computer Science METHODS.
Presentation transcript:

Lecture 3.1 Using Graphics with JFrame

© 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int height - Color backColor «constructor» + JFrame(String) «update» + void add(java.awt.Component, int) + void remove(java.awt.Component) + void repaint( ) + void setBackground(java.awt.Color) + void setBounds(int, int, int, int) + void setLayout(java.awt.LayoutManager) + void setVisible(boolean)...

© 2006 Pearson Addison-Wesley. All rights reserved JFrame Class Specifications Invariant A JFrame object … is a rectangular window placed upon a computer display. has a visible region that is width pixels wide, height pixels high, and with a background color of backColor. is positioned so that its upper left corner is x pixels from the left and y pixels from the top of the display (Below is a JFrame with backColor of gray on a white display.)

© 2006 Pearson Addison-Wesley. All rights reserved JFrame Class Specifications (continued) Constructor Methods 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

© 2006 Pearson Addison-Wesley. All rights reserved JFrame Class Specifications (continued) Update Methods public void setBackground(java.awt.Color c ) post: backColor == c public void setLayout(java.awt.LayoutManager m ) pre: m == null (for our purposes) post: added objects are rearranged via m public void setVisible(boolean b ) post: b == true implies this JFrame is made visible and brought to the front

© 2006 Pearson Addison-Wesley. All rights reserved An Example 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.setVisible(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.setVisible(true); greenWin.setBackground(Color.green); } Note the import Code Pattern for JFrame initialization 1) 2) 3) 4) String constants require double quotes.

© 2006 Pearson Addison-Wesley. All rights reserved public void add(java.awt.Component pic, int j) pre: j == 0 for best results post: image pic will be drawn upon this JFrame method specification java.awt.Label The options for Component arguments include... Line Oval Rectangle author-written and many others

© 2006 Pearson Addison-Wesley. All rights reserved java.awt.Label - int x - int y - int width - int height - Color backColor - Color foreColor «constructor» + Label(String) «update» + void repaint( ) + void setBackground(java.awt.Color) + void setForeground(java.awt.Color) + void setBounds(int, int, int, int)... Code Pattern for Label use 1) instantiate: new Label 2) set x, y & dimensions: setBounds 3) add to some canvas 4) repaint( ) Example 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 Label("Wisconsin"); wiLabel.setBounds(10, 40, 120, 20); wiLabel.setForeground(Color.blue); wiLabel.repaint(); win.add(wiLabel, 0); } imports both Color & Label

© 2006 Pearson Addison-Wesley. All rights reserved Rectangle - int x - int y - int width - int height - Color backColor «constructor» + Rectangle(int, int, int, int) «update» + void add(java.awt.Component, int) + void repaint( ) + void setBackground(java.awt.Color) + void setLocation(int, int) + void setSize(int, int)... Code Pattern for Rectangle use 1) instantiate: new Rectangle 2) setBackground 3) add to some canvas 4) repaint( ) Example 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.

© 2006 Pearson Addison-Wesley. All rights reserved A graphical object (excepting JFrame ) only becomes visible if it is add ed to some other visible object (called the underlying, or parent, container). Drawing Rules 1) A container might be a JFrame, Oval, Rectangle, (others). 3) Last add to the same container appears in front. 4) An add ed object is clipped to the bounding border of its container. for container. add( object, 0 ); 2) Each object can be added to no more than one container.

© 2006 Pearson Addison-Wesley. All rights reserved A call to repaint() is needed (sometimes) to cause an object to become visible. Example... square = new Rectangle(30, 40, 50, 50); window.add(square, 0); square.repaint(); circle = new Oval(0, 0, 20, 20); square.add(circle, 0); circle.repaint(); or... square = new Rectangle(30, 40, 50, 50); window.add(square, 0); circle = new Oval(0, 0, 20, 20); square.add(circle, 0); window.repaint();

© 2006 Pearson Addison-Wesley. All rights reserved import java.awt.Color; import javax.swing.JFrame; public class Driver { private JFrame win; 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; 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(); } Example