Top Banner
Intro to Java Java Applications Swing Class Graphics
36

Intro to Java Java Applications Swing Class Graphics.

Jan 13, 2016

Download

Documents

Dorthy Shields
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to Java Java Applications Swing Class Graphics.

Intro to Java

Java ApplicationsSwing Class

Graphics

Page 2: Intro to Java Java Applications Swing Class Graphics.

Java Outline

• Today– Intro and Applications– GUI

• Client / Server • RMI, JDBC

2

Page 3: Intro to Java Java Applications Swing Class Graphics.

JAVA Implementation

3

Java program

Java program

VM

Java Cmds

Java program

VM

Java Cmds

Java program

VM

Java Cmds

SPARC PowerPC Intel

Page 4: Intro to Java Java Applications Swing Class Graphics.

Operations Environments

• Applications program (~DOS window)– supports file access

• Applet (browser) – no longer generally supported– supports Internet linkages.

4

Page 5: Intro to Java Java Applications Swing Class Graphics.

Building Java Applications Using Java Development Kit

• Get a copy of the JDK (www.oracle.com, 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

Page 6: Intro to Java Java Applications Swing Class Graphics.

Building Java Applications Using Java IDEs

• NetBeans– Available from netbeans.org

• Eclipse– Available from www.eclipse.org

• jCreator le– available from jcreator.com– jcreator pro – academic license $35

• jDeveloper– available from www.oracle.com/technetwork/developer-

tools/jdev• many others

Page 7: Intro to Java Java Applications Swing Class Graphics.

Sample Java App

7

G:\Data\Java\MyHelloApp>type MyHelloApp.javaclass 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]); } }

Page 8: Intro to Java Java Applications Swing Class Graphics.

MyHelloApp Implementation

8

MyHelloApp.java

MyHelloApp.class

VM

machine code

MyHelloApp.class

Java compiler

Java byte code

Page 9: Intro to Java Java Applications Swing Class Graphics.

Sample Java App

9

G:\Data\Java\MyHelloApp>javac MyHelloApp.java

G:\Data\Java\MyHelloApp>java MyHelloAppWelcome to CS423The subject is JAVA!

G:\Data\Java\MyHelloApp>

Page 10: Intro to Java Java Applications Swing Class Graphics.

MyHelloApp.class

Page 11: Intro to Java Java Applications Swing Class Graphics.

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;

Page 12: Intro to Java Java Applications Swing Class Graphics.

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);}

Page 13: Intro to Java Java Applications Swing Class Graphics.

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); }

Page 14: Intro to Java Java Applications Swing Class Graphics.

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 (); } }

Page 15: Intro to Java Java Applications Swing Class Graphics.

Get Input from User

15

G:\Data\Java\InputApp>java InputApp*-------------------------------------------*Enter Source File Name Here: books_db.txtEnter Destination File Name Here: My_list.txtData copied from books_db.txt to My_list.txt*-------------------------------------------*

G:\Data\Java\InputApp>

Page 16: Intro to Java Java Applications Swing Class Graphics.

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.

Page 17: Intro to Java Java Applications Swing Class Graphics.

Product.java

// From Java: How to Program - Deitel and Deitel// Calculate the product of three integersimport 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:" );

Page 18: Intro to Java Java Applications Swing Class Graphics.

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 );

}

}

Page 19: Intro to Java Java Applications Swing Class Graphics.

Product Output(s)

Page 20: Intro to Java Java Applications Swing Class Graphics.

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

Page 21: Intro to Java Java Applications Swing Class Graphics.

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);

Page 22: Intro to Java Java Applications Swing Class Graphics.

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 );

Page 23: Intro to Java Java Applications Swing Class Graphics.

Additional Swing Options

Object[] possibleValues = { "First", "Second", "Third" };

Object selectedValue = JOptionPane.showInputDialog(null,

"Choose one", "Input", JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);

Page 24: Intro to Java Java Applications Swing Class Graphics.

Graphics in Applications

• Must include a main() method• Must extend the AWT Frame class, or swing JFrame class

24

Page 25: Intro to Java Java Applications Swing Class Graphics.

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); }

Page 26: Intro to Java Java Applications Swing Class Graphics.

HelloWorldWindowclass 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();

}}

}

Page 27: Intro to Java Java Applications Swing Class Graphics.

HelloWorldWindow Output

Page 28: Intro to Java Java Applications Swing Class Graphics.

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

Page 29: Intro to Java Java Applications Swing Class Graphics.

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

Page 30: Intro to Java Java Applications Swing Class Graphics.

Object Layout

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

30

Page 31: Intro to Java Java Applications Swing Class Graphics.

nuTextTest Example

Linux

Windows

Page 32: Intro to Java Java Applications Swing Class Graphics.

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

Page 33: Intro to Java Java Applications Swing Class Graphics.

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); } } );

Page 34: Intro to Java Java Applications Swing Class Graphics.

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;

Page 35: Intro to Java Java Applications Swing Class Graphics.

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

Page 36: Intro to Java Java Applications Swing Class Graphics.

Summary

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