Intro to Java Java Applications Swing Class Graphics.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Unit 3 Graphical User Interface (GUI) Dr. Magdi AMER.
Graphic User Interfaces Layout Managers Event Handling.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Carnegie Mellon University, MISM1 Java GUI programming and Java Threads GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann Thread.
Object Oriented Programming Java 1 GUI example taken from “Computing Concepts with Java 2” by Cay Horstmann GUI Programming.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Lecture 241 CS110 Lecture 25 Tuesday, May 4, 2004 Announcements –final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608 (easier than last.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Programming Task: Task 1 Controlled Assessment Practice.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Introduction to GUI in Java 1. Graphical User Interface Java is equipped with many powerful,easy to use GUI component such as input and output dialog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
OOP (Java): Simple/ OOP (Java) Objectives – –give some simple examples of Java applications and one applet 2. Simple Java Programs Semester.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
CSTP FS99CS423 (cotter)1 Java Graphics java.awt.*;
习 题 4.23 编写一个 applet ,读取一个矩形的边长,然后 用在 paint 方法中使用 drawString 方法画出用星组成 的空心矩形。程序应能画出边长从 1 到 20 的任何矩 形。
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
GUI Clients 1 Enterprise Applications CE00465-M Clients with Graphical User Interfaces.
Programming Fundamentals 2: Simple/ F II Objectives – –give some simple examples of Java applications and one applet 2. Simple Java.
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.
CS102 – GUI AWT & Swing Components & Containers, Layout Managers, Events & Listeners MVC design pattern. David Davenport.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
1/23: Java Modular Components Identifiers: naming requirements for Java variables & objects Stepping out of the MS-DOS window: The Java Modular Component:
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
GUI Graphical User Interface Each onscreen component and window is an object Object interaction makes communication and scoping challenging Event-driven.
1 Java Remote Method Invocation java.rmi.* java.rmi.registry.* java.rmi.server.*
Swing. Introduction to Swing What is Swing? “ Swing is a diverse collection of lightweight components that can be used to build sophisticated user interfaces.”
SD2054 Software Development. By the end of this lecture you should be able to: Advanced Graphics Programming create pull down menus create combo boxes.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Basics of GUI Programming Chapter 11 and Chapter 22.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
JOptionPane Class JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. While the JOptionPane.
SE-1020 Dr. Mark L. Hornick 1 Graphical User Interfaces.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.
Applets. 9/04/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L12: Applets Slide 2 Applets Usually.
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Swing and Events Chris North cs3724: HCI. Presentations nadine edwards, steve terhar Vote: UI Hall of Fame/Shame?
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
A Quick Java Swing Tutorial
Graphical User Interfaces
Welcome To java
A Java Program: // Fig. 2.1: Welcome1.java // Text-printing program.
“Form Ever Follows Function” Louis Henri Sullivan
Section 64 – Manipulating Data Using Methods – Java Swing
Ellen Walker Hiram College
Message, Input, Confirm, and Specialized Dialogs
CS18000: Problem Solving and Object-Oriented Programming
Message, Input, and Confirm Dialogs
Advanced Programming in Java
Constructors, GUI’s(Using Swing) and ActionListner
CS431 ws99 Half Text Half Graphics
F II 2. Simple Java Programs Objectives
Presentation transcript:

Intro to Java Java Applications Swing Class Graphics

Java Outline Today – Intro and Applications – GUI Client / Server RMI, JDBC 2

JAVA Implementation 3 Java program VM Java Cmds Java program VM Java Cmds Java program VM Java Cmds SPARCPowerPCIntel

Operations Environments Applications program (~DOS window) – supports file access Applet (browser) – no longer generally supported – supports Internet linkages. 4

Building Java Applications Using Java Development Kit Get a copy of the JDK ( etc.) Install the development kit (tools and classes) Create MyApp.java source file – text editor (notepad, etc.) – winedit, Café, etc. Compile.java file to create.class file – javac MyApp.java Run application (.class file) through java VM – java MyApp 5

Building Java Applications Using Java IDEs NetBeans – Available from netbeans.org Eclipse – Available from jCreator le – available from jcreator.com – jcreator pro – academic license $35 jDeveloper – available from tools/jdev many others

Sample Java App 7 G:\Data\Java\MyHelloApp>type MyHelloApp.java class MyHelloApp { public static void main (String argv[]) { String message[] = new String[2]; message[0] = "Welcome to CS423"; message[1] = "The subject is JAVA!"; System.out.println (message[0]); System.out.println (message[1]); }

MyHelloApp Implementation 8 MyHelloApp.java MyHelloApp.class VM machine code MyHelloApp.class Java compiler Java byte code

Sample Java App 9 G:\Data\Java\MyHelloApp>javac MyHelloApp.java G:\Data\Java\MyHelloApp>java MyHelloApp Welcome to CS423 The subject is JAVA! G:\Data\Java\MyHelloApp>

MyHelloApp.class

Get Input from User 11 import java.io.*; class InputApp { InputApp () { DataInputStream dis = new DataInputStream (System.in); String sIn = " "; String sOut = " "; File fIn; File fOut; FileInputStream fis; FileOutputStream fos; int iNumBytesRead; byte[] ab;

Get Input from User 12 System.out.println ("* *"); System.out.print ("Enter Source File Name Here: "); System.out.flush (); try {sIn = dis.readLine ();} catch (Exception e) {System.out.println ("Exception on input file name" + e);} System.out.print("Enter Destination File Name Here: "); System.out.flush (); try {sOut = dis.readLine ();} catch (Exception e) {System.out.println ("Exception on output file name" + e);}

Get Input from User 13 try { fIn = new File (sIn); fis = new FileInputStream (fIn); fOut = new File (sOut); fos = new FileOutputStream (fOut); ab = new byte[2048]; while ((iNumBytesRead = fis.read (ab)) != -1) { fos.write (ab, 0, iNumBytesRead); } fis.close (); fos.close (); } catch (Exception e) {System.out.println ("Exception during copy: " + e); }

Get Input from User 14 System.out.println ("Data copied from " + sIn + " to " + sOut); System.out.println ("* *"); } public static void main (String args[]) { new InputApp (); }

Get Input from User 15 G:\Data\Java\InputApp>java InputApp * * Enter Source File Name Here: books_db.txt Enter Destination File Name Here: My_list.txt Data copied from books_db.txt to My_list.txt * * G:\Data\Java\InputApp>

Swing Components Defined in package javax.swing – Pure Java components AWT components tied to local platform GUI – UNIX java windows look like X windows – Windows java windows look like Windows windows (??) – etc. Swing defines a common look and feel for Java.

Product.java // From Java: How to Program - Deitel and Deitel // Calculate the product of three integers import javax.swing.JOptionPane; public class Product { public static void main( String args[] ) { int x, y, z, result; String xVal, yVal, zVal; xVal = JOptionPane.showInputDialog( "Enter first integer:" ); yVal = JOptionPane.showInputDialog( "Enter second integer:" ); zVal = JOptionPane.showInputDialog( "Enter third integer:" );

Product.java x = Integer.parseInt( xVal ); y = Integer.parseInt( yVal ); z = Integer.parseInt( zVal ); result = x * y * z; JOptionPane.showMessageDialog( null, "The product is " + result ); System.exit( 0 ); }

Product Output(s)

JOptionPane Similar to windows message boxes Types of Option Panes: showConfirmDialog(null,message,title,optionType,msgType); Asks a confirming question, like yes/no/cancel. showInputDialog(message); Prompt for some input. showMessageDialog(null, message); Tell the user about something that has happened. showOptionDialog(....); The Grand Unification of the above three. 20

Additional Swing Options JOptionPane.showMessageDialog(null, "This is an alert", "Alert", JOptionPane.ERROR_MESSAGE); JOptionPane.showConfirmDialog(null, "Is that your final answer?", "Choice Dialog", JOptionPane.YES_NO_OPTION);

Additional Swing Options Object[] options = { "Lady", "Tiger" }; choice = JOptionPane.showOptionDialog(null, "What will it be -\nThe Lady or the Tiger?", "Decision", JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (choice == 0) JOptionPane.showMessageDialog( null, "You chose the Lady", "Result", JOptionPane.PLAIN_MESSAGE ); else JOptionPane.showMessageDialog( null, "You chose the Tiger", "Result", JOptionPane.PLAIN_MESSAGE );

Additional Swing Options Object[] possibleValues = { "First", "Second", "Third" }; Object selectedValue = JOptionPane.showInputDialog(null, "Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);

Graphics in Applications Must include a main() method Must extend the AWT Frame class, or swing JFrame class 24

HelloWorldWindow 25 import java.awt.*; import java.awt.event.*; public class HelloWorldWindow implements WindowListener { public HelloWorldWindow() { myFrame f=new myFrame(); f.addWindowListener (this); } public void windowClosing(WindowEvent e) { System.out.println ("Closing Window"); System.exit(0); }

HelloWorldWindow class myFrame extends Frame{ myFrame() { this.setSize(300, 200); this.show(); } public void paint(Graphics g) { String str = "A Windows version of Hello, World"; g.drawString(str, 25, 75); } public static void main(String[] args) { Frame f = new HelloWorldWindow(); f.resize(300, 200); f.show(); }

HelloWorldWindow Output

public void paint (Graphics g) Provides an initial presentation of Graphics Works on Graphics object – (like device context). – Place to store settings for screen output (text, images. etc.) Must be regenerated following changes. 28

Event Driven Programming Operating System recognizes an event Sends a signal to appropriate object Object receives event notification and calls appropriate function public void windowClosing(WindowEvent e) { System.out.println ("Closing Window"); System.exit(0); } 29

Object Layout BorderLayout (default for Frames) FlowLayout (default for Panels) GridLayout GridbagLayout CardLayout Fixed: – object.reshape (x, y, width, height); 30

nuTextTest Example Linux Windows

NuTextTest2.java import javax.swing.*; import java.awt.*; import java.awt.event.*; public class NuTextTest2 extends JFrame { public NuTextTest2() { setTitle("NuTextTest2"); Container p = getContentPane();; p.setLayout(new FlowLayout()); TickButton = new Button("Tick"); p.add(TickButton); TickButton.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { int minutes = Integer.parseInt(minuteField.getText()); minutes += 1; String min = String.valueOf(minutes); minuteField.setText(min); } } //end of ActionListener ); //end of TickButton

NuTextTest2.java SetTime = new Button("Set Time"); p.add(SetTime); SetTime.addActionListener( new ActionListener() { public void actionPerformed (ActionEvent e) { String tim = hourField.getText()+ ":" + minuteField.getText(); timeField.setText(tim); } );

NuTextTest2.java hourField = new TextField("12", 3); p.add(hourField); minuteField = new TextField("00", 3); p.add(minuteField); timeField = new TextField("", 12); p.add(timeField); setSize (400,100); show (); }//end of NuTextTest2 (constructor) private TextField hourField; private TextField minuteField; private TextField timeField; private Button TickButton; private Button SetTime;

NuTextTest2.java public static void main(String[] args) { NuTextTest2 app = new NuTextTest2(); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } ); }//end of main() }//end of program

Summary Java is: – Platform independent – Object-oriented – Dynamic – Flexible – used in many different domains – Command line or graphical