Post on 03-Jun-2018
8/12/2019 Cs423 Java Intro Bw
1/36
Intro to Java
Java Applications
Swing Class
Graphics
8/12/2019 Cs423 Java Intro Bw
2/36
Java Outline
Today
Intro and Applications
GUI
Client / Server
RMI, JDBC
2
8/12/2019 Cs423 Java Intro Bw
3/36
JAVA Implementation
3
Java program
Java program
VM
Java Cmds
Java program
VM
Java Cmds
Java program
VM
Java Cmds
SPARC PowerPC Intel
8/12/2019 Cs423 Java Intro Bw
4/36
Operations Environments
Applications program (~DOS window)
supports file access
Applet (browser)
no longer generally supported
supports Internet linkages.
4
8/12/2019 Cs423 Java Intro Bw
5/36
Building Java ApplicationsUsing 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
8/12/2019 Cs423 Java Intro Bw
6/36
Building Java ApplicationsUsing Java IDEs
NetBeans Available from netbeans.org
Eclipse Available from www.eclipse.org
jCreator le available from jcreator.com
jcreator proacademic license $35
jDeveloper available from www.oracle.com/technetwork/developer-
tools/jdev
many others
8/12/2019 Cs423 Java Intro Bw
7/36
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]);
}}
8/12/2019 Cs423 Java Intro Bw
8/36
MyHelloApp Implementation
8
MyHelloApp.java
MyHelloApp.class
VM
machine code
MyHelloApp.class
Java compiler
Java byte code
8/12/2019 Cs423 Java Intro Bw
9/36
Sample Java App
9
G:\Data\Java\MyHelloApp>javac MyHelloApp.java
G:\Data\Java\MyHelloApp>java MyHelloApp
Welcome to CS423The subject is JAVA!
G:\Data\Java\MyHelloApp>
8/12/2019 Cs423 Java Intro Bw
10/36
MyHelloApp.class
8/12/2019 Cs423 Java Intro Bw
11/36
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;
8/12/2019 Cs423 Java Intro Bw
12/36
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);}
8/12/2019 Cs423 Java Intro Bw
13/36
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); }
8/12/2019 Cs423 Java Intro Bw
14/36
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 ();
}}
8/12/2019 Cs423 Java Intro Bw
15/36
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>
8/12/2019 Cs423 Java Intro Bw
16/36
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 Windowswindows (??)
etc. Swing defines a common look and feel for
Java.
8/12/2019 Cs423 Java Intro Bw
17/36
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:" );
8/12/2019 Cs423 Java Intro Bw
18/36
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 );
}
}
8/12/2019 Cs423 Java Intro Bw
19/36
Product Output(s)
8/12/2019 Cs423 Java Intro Bw
20/36
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
8/12/2019 Cs423 Java Intro Bw
21/36
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);
8/12/2019 Cs423 Java Intro Bw
22/36
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 );
8/12/2019 Cs423 Java Intro Bw
23/36
Additional Swing Options
Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,
"Choose one", "Input", JOptionPane.INFORMATION_MESSAGE,
null, possibleValues, possibleValues[0]);
8/12/2019 Cs423 Java Intro Bw
24/36
Graphics in Applications
Must include a main()method
Must extend the AWT Frameclass, or swing
JFrameclass
24
8/12/2019 Cs423 Java Intro Bw
25/36
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);
}
8/12/2019 Cs423 Java Intro Bw
26/36
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();
}}
}
8/12/2019 Cs423 Java Intro Bw
27/36
HelloWorldWindow Output
8/12/2019 Cs423 Java Intro Bw
28/36
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
8/12/2019 Cs423 Java Intro Bw
29/36
Event Driven Programming
Operating System recognizes an event
Sends a signal to appropriate object
Object receives event notification and callsappropriate function
public void windowClosing(WindowEvent e) {
System.out.println ("Closing Window");
System.exit(0);
}
29
8/12/2019 Cs423 Java Intro Bw
30/36
Object Layout
BorderLayout (default for Frames)
FlowLayout (default for Panels)
GridLayout GridbagLayout
CardLayout
Fixed: object.reshape (x, y, width, height);
30
8/12/2019 Cs423 Java Intro Bw
31/36
nuTextTest Example
Linux
Windows
8/12/2019 Cs423 Java Intro Bw
32/36
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
8/12/2019 Cs423 Java Intro Bw
33/36
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);
}}
);
8/12/2019 Cs423 Java Intro Bw
34/36
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;
8/12/2019 Cs423 Java Intro Bw
35/36
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
8/12/2019 Cs423 Java Intro Bw
36/36
Summary
Java is:
Platform independent
Object-oriented
Dynamic
Flexibleused in many different domains
Command line or graphical