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.

Slides:



Advertisements
Similar presentations
Chapter 13 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.
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?
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.
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.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
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.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
©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.
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.
Graphical User Interface (GUI) Programming IV. Lecture Objectives Exploring more GUI programming elements in Java Using icons in GUIs Using scroll bars.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
©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.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
Unit 11 Object-oriented programming: Graphical user interface Jin Sa.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
© 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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
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 ________.
© 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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
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.
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
JAVA 2 Design and programming of GUI
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Presentation transcript:

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 inserted is not fixed As a result, some text may become invisible as the window is overfilled A JScrollPane object can be added to a JTextArea (or JFrame or JPanel) to enable the user to access all window contents via scroll bars

Scroll bars A JScrollPane is constructed using the text area (or other component) it applies to as its argument: JTextArea commentBox = new JTextArea(50, 20); JScrollPane viewBox = new JScrollPane(commentBox); The JScrollPane object can then be added to a JFrame or JPanel using the add method

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 JFrame or JPanel) 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

Example public class BlueSquare extends JFrame { // instance variables describe position & size of square private int x = 20, y = 40, size = 100; // constructor not shown – just sets size & location of JFrame public void paint (Graphics g) { g.setColor(Color.BLUE); g.fillRect(x,y,size,size); } public static void main (String[] args) { BlueSquare b = new BlueSquare(); b.setVisible(true); }

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 repaint() method The paint() method is called automatically when a window is made visible If changes need to be made to the window’s appearance, the window must be redrawn This operation is accomplished by the repaint() method

Icons An icon is a small picture, typically displayed as part of a GUI component such as a button, menu item, or label In Java, an icon is an instance of the ImageIcon class, and is constructed with a digital picture file, such as a.gif or.jpg Example: ImageIcon pic = new ImageIcon(“pic.gif”);

Adding icons to GUI components To add an icon to a JLabel, JButton or JMenuItem, you can use the setIcon method: JButton button = new JButton(“”); button.setIcon(pic); Alternatively, you can construct the component using an icon argument instead of String argument: JButton button = new JButton(pic); In both instances above, a String should be associated with the JButton by using the setActionCommand() method

Placing icons in a window To use an existing drawing (as opposed to one you create yourself), you can use the paintIcon method of an ImageIcon object The method takes 4 arguments: –The component on which to paint the icon (e.g. a JPanel or JFrame) –A Graphics object –The x and y coordinates of the upper left corner of the bounding rectangle