1.00 Lecture 17. Swing

Similar documents
INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

CS 335 Lecture 06 Java Programming GUI and Swing

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

GUIs with Swing. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2012

5.17 GUI. Xiaoyi Jiang Informatik I Grundlagen der Programmierung

GUI Components: Part 2

How to Convert an Application into an Applet.

Programming with Java GUI components

How To Write A Program For The Web In Java (Java)

Graphical User Interfaces

Mouse Event Handling (cont.)


Java is commonly used for deploying applications across a network. Compiled Java code

During the process of creating ColorSwitch, you will learn how to do these tasks:

How To Build A Swing Program In Java.Java.Netbeans.Netcode.Com (For Windows) (For Linux) (Java) (Javax) (Windows) (Powerpoint) (Netbeans) (Sun) (

JIDE Action Framework Developer Guide

Swing. A Quick Tutorial on Programming Swing Applications

Java GUI Programming. Building the GUI for the Microsoft Windows Calculator Lecture 2. Des Traynor 2005

public class Craps extends JFrame implements ActionListener { final int WON = 0,LOST =1, CONTINUE = 2;

Homework/Program #5 Solutions

DHBW Karlsruhe, Vorlesung Programmieren, Remote Musterlösungen

Petrel TIPS&TRICKS from SCM

method is never called because it is automatically called by the window manager. An example of overriding the paint() method in an Applet follows:

Using A Frame for Output

Lab 1A. Create a simple Java application using JBuilder. Part 1: make the simplest Java application Hello World 1. Start Jbuilder. 2.

Assignment # 2: Design Patterns and GUIs

Advanced Network Programming Lab using Java. Angelos Stavrou

11. Applets, normal window applications, packaging and sharing your work

How To Program In Java (Ipt) With A Bean And An Animated Object In A Powerpoint (For A Powerbook)

Essentials of the Java(TM) Programming Language, Part 1

Event processing in Java: what happens when you click?

The Basic Java Applet and JApplet

Essentials of the Java Programming Language

Adobe InDesign Creative Cloud

Nexawebホワイトペーパー. Developing with Nexaweb ~ Nexaweb to Improve Development Productivity and Maintainability

Canterbury Maps Quick Start - Drawing and Printing Tools

Java applets. SwIG Jing He

// Correntista. //Conta Corrente. package Banco; public class Correntista { String nome, sobrenome; int cpf;

Creating and Using Links and Bookmarks in PDF Documents

file://c:\dokumente und Einstellungen\Marco Favorito\Desktop\ScanCmds.html

Eclipse with Mac OSX Getting Started Selecting Your Workspace. Creating a Project.

UML Class Diagrams (1.8.7) 9/2/2009

Extending Desktop Applications to the Web

The following is an overview of lessons included in the tutorial.

How Scala Improved Our Java

Tutorial Reference Manual. Java WireFusion 4.1

Tutorial: Time Of Day Part 2 GUI Design in NetBeans

Updox, LLC

CREATING A 3D VISUALISATION OF YOUR PLANS IN PLANSXPRESS AND CORTONA VRML CLIENT

Karsten Lentzsch JGoodies SWING WITH STYLE

Interactive Programs and Graphics in Java

TUTORIAL 4 Building a Navigation Bar with Fireworks

Introduction to Java Applets (Deitel chapter 3)

LAYOUT MANAGERS. Layout Managers Page 1. java.lang.object. java.awt.component. java.awt.container. java.awt.window. java.awt.panel

WebViewer User Guide. version PDFTron Systems, Inc. 1 of 13

Photoshop- Image Editing

Epson Brightlink Interactive Board and Pen Training. Step One: Install the Brightlink Easy Interactive Driver

Microsoft Word defaults to left justified (aligned) paragraphs. This means that new lines automatically line up with the left margin.

Using NetBeans IDE for Desktop Development. Geertjan Wielenga

Hypercosm. Studio.

Microsoft Word Quick Reference Guide. Union Institute & University

Java Appletek II. Applet GUI

WEEK 2 DAY 14. Writing Java Applets and Java Web Start Applications

Sharing Presentations, Documents, and Whiteboards

Data Visualization. Prepared by Francisco Olivera, Ph.D., Srikanth Koka Department of Civil Engineering Texas A&M University February 2004

Dashboard Designer. Introduction Guide. Basic step by step guide to creating a Dashboard. June 2012 V1.2

CONSTRUCTING SINGLE-SUBJECT REVERSAL DESIGN GRAPHS USING MICROSOFT WORD : A COMPREHENSIVE TUTORIAL

3D Viewer. user's manual _2

mouse (or the option key on Macintosh) and move the mouse. You should see that you are able to zoom into and out of the scene.

Construction of classes with classes

Artificial Intelligence. Class: 3 rd

Instructions for Creating a Poster for Arts and Humanities Research Day Using PowerPoint

Fundamentals of Java Programming

3D-GIS in the Cloud USER MANUAL. August, 2014

Animazione in Java. Animazione in Java. Problemi che possono nascere, e loro soluzione. Marco Ronchetti Lezione 1

After you complete the survey, compare what you saw on the survey to the actual questions listed below:

Java SE 6 Update 10. la piattaforma Java per le RIA. Corrado De Bari. Sun Microsystems Italia Spa. Software & Java Ambassador

SMART Board Tips & Tricks (version 9.0) Getting Started. SMART Tools vs. SMART Notebook software

Getting Started with Excel Table of Contents

History OOP languages Year Language 1967 Simula Smalltalk

Adobe Illustrator CS5 Part 1: Introduction to Illustrator

Principles of Software Construction: Objects, Design and Concurrency. GUIs with Swing. toad Spring 2013

Java (12 Weeks) Introduction to Java Programming Language

USER MANUAL SlimComputer

Windows PowerShell Essentials

Smart Connection 9 Element Labels

Fireworks CS4 Tutorial Part 1: Intro

Understand the Sketcher workbench of CATIA V5.

SolidWorks Tutorial 3 MAGNETIC BLOCK

Lession: 2 Animation Tool: Synfig Card or Page based Icon and Event based Time based Pencil: Synfig Studio: Getting Started: Toolbox Canvas Panels

Green = 0,255,0 (Target Color for E.L. Gray Construction) CIELAB RGB Simulation Result for E.L. Gray Match (43,215,35) Equal Luminance Gray for Green

CREATING EXCEL PIVOT TABLES AND PIVOT CHARTS FOR LIBRARY QUESTIONNAIRE RESULTS

Part 1 Foundations of object orientation

How To Change Your Site On Drupal Cloud On A Pcode On A Microsoft Powerstone On A Macbook Or Ipad (For Free) On A Freebie (For A Free Download) On An Ipad Or Ipa (For

STATGRAPHICS Online. Statistical Analysis and Data Visualization System. Revised 6/21/2012. Copyright 2012 by StatPoint Technologies, Inc.

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

Here's the code for our first Applet which will display 'I love Java' as a message in a Web page

To Begin Customize Office

Go to: URL:

Transcription:

1.00 Lecture 17 Introduction to Swing Reading for next time: Big Java: sections 12.1-12.6 Swing Package of user interface classes for windows, menus, scroll bars, buttons, etc. Independent of hardware and operating system (as long as they can paint a window) Swing gains independence but loses performance by not relying on native drawing calls Has Windows, Mac, other look and feel options Supersedes Java Abstract Window Toolkit (AWT) though it still uses many non-drawing classes from that library. You will usually: import java.awt import javax.swing 1

The 3 Flavors of GUI Objects Top Level Windows: are containers that are not contained by any other containers; they can be iconified or dragged and interact with the native windowing system Example: JFrame, JDialog Containers: some JComponents are designed to hold other components, not to present info or interact with the user Examples: JPanel, JScrollPane JComponents: present information or interact with the user Examples: labels (JLabel JLabel), buttons (JButton JButton), text fields (JTextField JTextField) JFrame and JDialog are not Jcomponents Anatomy of a JFrame Look and Feel, platform dependent Interacts with the window system JFrame has a contentpane, which is the container that will hold your content 2

0,0 Coordinates y Screen x x y Submit Clear Frame Measured in pixels (e.g. 640 by 480, 1024 by 768, etc.) By tradition, upper left hand corner is origin (0,0) X axis goes from left to right, y from top to bottom Exercise 1: Empty JFrame // Download, read and run this program import javax.swing public class SwingTest { public static void main(string[] args) ) { // Create new frame JFrame frame= new JFrame(); // Tells program to exit when user closes this frame frame.setdefaultcloseoperation(jframe.exit_on_close); // Frame has 0 default size; give it a size frame.setsize(400, 300); // setsize(int x, int y) // Frame invisible by default; make it visible frame.setvisible(true); // main() ends but Swing thread stays alive // Run the program; see what it draws 3

Frames, Panes and Panels JFrame contentpane, obtained from frame x y Text message paint Component (on panel) or JLabel (on panel) Button JPanel (on pane, can be many) JButton (on panel, can be many) Exercise 2: Panel with Color // Download, read and run this program import java.awt import javax.swing public class SwingTest2 { public static void main(string args[]) { JFrame frame = new JFrame( Welcome to 1.00 ); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(500,500); Container contentpane= frame.getcontentpane(); // No J // We never draw on a JFrame.. Instead we update // components that are added to its contentpane JPanel panel = new JPanel(); panel.setbackground(color.pink); // Add panel to the contentpane of the JFrame contentpane.add(panel); frame.show(); 4

More Complex Drawing The JFrame in the preceding example contained a simple, colored JPanel 13 predefined colors: Color.x where x is Color.x orange, pink, cyan, magenta, yellow, black, blue, white, gray, lightgray, darkgray,, red, green You can create your own colors (see text, section 4.5) If we want to accomplish more complex drawing, we need to build new UI components. These components are created by extending existing Swing classes, like JPanel or JComponent by: overriding the base class paintcomponent() method and/or adding members (JPanel JPanel, JButton, etc.) and methods to the base class Frames, Panes and Panels Top-level window is a Java frame (JFrame) JFrame is container for user interface elements, primarily panes (contentpane) Programs draw UI components on panes Container contentpane= getcontentpane(); JButton q= new JButton( Quit ); // Buttons, text boxes, etc. contentpane.add(q); To draw on a pane, use inheritance: Create a subclass of JPanel or other component to do what you want Redefine (override) the paintcomponent method in your subclass paintcomponent has Graphics object as argument Graphics object stores data on fonts and colors, and has drawing methods that you can use Bring up class WelcomePanel in Eclipse: 5

Exercise 3: WelcomePanel // Download WelcomePanel import java.awt import javax.swing public class WelcomePanel extends JPanel { public void paintcomponent(graphics g) { // Have JPanel paintcomponent do default operations // such as background color, etc. super.paintcomponent(g); g.drawstring( Welcome to 1.00", 125, 150); // The last two arguments of drawstring indicate // that the message should be drawn at (x,y) = // (125,150) Exercise 3: Panel with Text Modify SwingTest2 main to use this new component. Change: JPanel panel = new JPanel(); to: WelcomePanel panel = new WelcomePanel(); Remove: panel.setbackground(color.pink); Run SwingTest2 main again. This time we ll see a JFrame containing our new component. 6

Graphics Graphics class can draw lines, ellipses, etc. Very limited: single thickness, no rotation, etc. Java2D library is much more functional Uses Graphics2D, not Graphics objects Some casting is needed (but little thought) There are two versions of the Java2D library: double and float. Use the double version: Rectangle2D.Double() Line2D.Double(), etc. (Remember these from inner classes?) You can drop the.double part sometimes; see examples Exercise 4: Drawing To utilize the Graphics2D package, we ll create a new UI component, CirclePanel, that uses Graphics2D calls in its paintcomponent method. To access the Graphics2D package, we must import the following: import java.awt.geom Bring up class CirclePanel in Eclipse: 7

Exercise 4: Drawing, p.2 import javax.swing import java.awt import java.awt.geom // For 2D classes public class CirclePanel extends JPanel { public void paintcomponent(graphics g) { { super.paintcomponent(g); Graphics2D g2= (Graphics2D) g; int x = 200, y = 150, w = 100, h = 100; // Ellipse: top left corner(x,y), width = height Ellipse2D circle= new Ellipse2D.Double(x,y,w,h); g2.setpaint(color.blue); g2.draw(circle); Exercise 4: Drawing, p.3 Substitute CirclePanel for WelcomePanel in SwingTest2 main When you run SwingTest2 main, you should see a JFrame containing a panel that paints a blue outline of a circle. Add code to CirclePanel s paintcomponent method so that it: Creates one Ellipse2D, Rectangle2D, and Line2D object. Makes a different colored object Makes a filled object Draws some text using Graphics2D.drawString 8

Exercise 4: Drawing, p.4 Constructor Summary: all arguments are doubles, w is width, h is height, x,y are coordinates corresponding to upper left corner new Ellipse2D.Double(x,y,w,h) new Rectangle.Double(x,y,w,h) new Line2D.Double(x1,y1,x2,y2) Code paintcomponent in stages: add one 2D object at a time and check if it works as expected To change the color that objects are painted, invoke the setpaint method on g2, your Graphics2D object. setpaint can take a single Color argument (e.g.,color.blue) To draw a filled object, invoke the fill method on g2, your Graphics2D object. fill can take a single Ellipse2D or Rectangle2D object as its argument. Fonts Standard constructor: Font myfont = new Font( String name, int style, int size ); Font name: safe approach is to use a logical font name, one of "SansSerif SansSerif", "Serif", "Monospaced" Monospaced", "Dialog", "DialogInput" DialogInput", "Symbol" Four font styles are present: Font.y where y is Font.PLAIN, Font.BOLD, Font.ITALIC Font.BOLD + Font.ITALIC Size is point size; 12 corresponds to standard printed text 9

Exercise 5: Font // Download the following program import javax.swing import java.awt public class FontPanel extends JPanel { private Font[] fonts= { new Font("Monospaced", ", Font.PLAIN, 24) ; public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2= (Graphics2D) g; int basey= = 50; Font f; for (int( i= 0; i < fonts.length; i++) { f= fonts[i]; g2.setfont(f); g2.drawstring(f.getfontname() + " " + + f.getsize(), 50, basey); basey += f.getsize() + 20; Exercise 5: Font, p.2 Substitute FontPanel for CirclePanel in SwingTest2 main When you run SwingTest2 main, you should see a JFrame containing a panel with the text Monospaced.plain 24 Add 3 new Font objects to the Font[] fonts Do this using the Font constructor on the previous slide with new combinations of Font names, Font styles, and Font sizes. 10

Building with Components As simple as our first application is, we can build some interesting variations with little additional code. JLabels can hold images as well as or instead of text. A contentpane has 5 zones where you can add a component. Components are placed using the BorderLayout class North West Center East South A Simple Image Viewer, 1 import javax.swing import java.awt import java.net public class ImageView extends JFrame { private URL source; private String title; public ImageView( String u, String t ) { title = t; try { source = new URL( u ); catch ( MalformedURLException e ) { System.out.println( ( "Bad URL " + source ); System.exit( 1 ); 11

A Simple Image Viewer, 2 // Constructor, continued (usual approach in Swing) setdefaultcloseoperation( ( EXIT_ON_CLOSE ); Container contentpane= getcontentpane(); // Make a label of the image from the URL ImageIcon image = new ImageIcon( source ); JLabel imagelabel = new JLabel( image, SwingConstants.CENTER ); contentpane.add(imagelabel, BorderLayout.CENTER ); // Make a 2nd label of the title JLabel titlelabel = new JLabel( title, SwingConstants.CENTER ); contentpane.add( titlelabel, BorderLayout.SOUTH ); pack(); A Simple Image Viewer, 3 public static void main( String [] args ) { String url = " server "; String title = "The New Stata Center"; ImageView view = new ImageView( url, title ); view.show(); 12