Java CIS 260 Notes Part 3
Java CIS 260 Notes Part 3
More on Strings
Strings are objects which mean they are reference variables this
means that
String sX = “fred”;
String sY = “sam”;
if ( sX.equal(sY) )
{
System.out.println(“yes, they are equal”);
}
else
{
System.out.println(“no, they are not”);
}
// Pass String
// Show how strings are different when used with =
public class test3
{
System.out.println("");
2
System.exit( 0 );
} // end main
// modify strings
public static void modifyString( String sOne, String sTwo )
{
String sBubba = "end";
sOne = "Red";
sTwo = sBubba;
}
Graphics
Reading color values with int getRed(), int getBlue() and int
getGreen() or all at once with Color getColor().
JFrame
Create a class with a main method. This is our driver program that
will launch our window class by code like
Code Example)
// execute application
public static void main( String args[] )
{
ShowColors application = new ShowColors();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Fonts
Fonts can be examined and set via a variety of methods and fields
that work with the Font class. Please note that not all fonts are
available for all users, but Java does requires that (Serif,
Monospaced and SanSerif are available). Font sizes are specified in
points where one point is 1/72 of an inch
Reading Font Metrics provides “techie” info about a font with int
getAscent(), int getDescent(), getLeading(), int getHeight() as well
as FontMetrics getFontMetrics() and FontMetrics getFontMetrics( Font
) where FontMetrics is a class with the methods described for this
entry
Code Example)
// Using fonts.
import java.awt.*;
import javax.swing.*;
// execute application
public static void main( String args[] )
{
Fonts application = new Fonts();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Code Example)
// execute application
public static void main( String args[] )
{
Metrics application = new Metrics();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Drawing
Draw lines with void drawLine( intX1, intY1, int intX2, intY2)
Code Example)
g.setColor( Color.RED );
g.drawLine( 5, 30, 350, 30 );
g.setColor( Color.BLUE );
g.drawRect( 5, 40, 90, 55 );
g.fillRect( 100, 40, 90, 55 );
11
g.setColor( Color.CYAN );
g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
g.setColor( Color.YELLOW );
g.draw3DRect( 5, 100, 90, 55, true );
g.fill3DRect( 100, 100, 90, 55, false );
g.setColor( Color.MAGENTA );
g.drawOval( 195, 100, 90, 55 );
g.fillOval( 290, 100, 90, 55 );
// execute application
public static void main( String args[] )
{
LinesRectsOvals application = new LinesRectsOvals();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Code Example)
// execute application
public static void main( String args[] )
{
DrawArcs application = new DrawArcs();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
} // end class DrawArcs
13
Code Example)
g.drawPolygon( polygon1 );
g.fillPolygon( polygon2 );
// execute application
public static void main( String args[] )
{
DrawPolygons application = new DrawPolygons();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
Events
Event Source which object triggered (is affected by) the event
Event Listener is the code which waits to receive the event object.
It must both be registered with the event source as well as have code
that will handle the event in question. This is the portion we
write to handle events
The events listeners we can use are provided as interfaces that you
can implement in your code. Generally, this is done by the use of
inner classes. The event interface system is as follows
EventListener
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
TextListner
WindowListener
16
Inner/Anonymous Classes
Inner Classes and Anonymous Inner Classes are two ways we can use to
create our event classes. Both involve creating a class inside a
class (our GUI class)
JTextField
Code Example)
// set up GUI
public TextFieldTest()
{
super( "Testing JTextField and JPasswordField" );
JButton
Code Example)
// set up GUI
public ButtonTest()
{
super( "Testing Buttons" );
// create buttons
plainButton = new JButton( "Plain Button" );
container.add( plainButton );
Combining Both
// Creating test5
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// set up GUI
public test5()
{
super( "Testing Buttons" );
// create buttons
plainButton = new JButton( "Plain Button" );
container.add( plainButton );
FlowLayout places components left to right and then over onto the
“next line” when more space is needed. FlowLayout supports alignment
and components may be ordered using a second integer argument to the
add method. The Constructors are supported as follows with alignment
argument supporting FlowLayout.LEADING (left -> right),
FlowLayout.CENTER, or FlowLayout.TRAILING (right -> left). And, the
gap arguments specify the number of pixels to put between components,
default is 5
public FlowLayout()
Code Example)
new ActionListener()
{ // anonymous inner class
new ActionListener()
{ // anonymous inner class
setSize( 300, 75 );
setVisible( true );
public BorderLayout()
Code Example)
Code Example)
// set up GUI
public GridLayoutDemo()
{
super( "GridLayout Demo" );
// set up layouts
grid1 = new GridLayout( 2, 3, 5, 5 );
grid2 = new GridLayout( 3, 2 );
Code Example)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
pane.add(comboBoxPane, BorderLayout.PAGE_START);
pane.add(cards, BorderLayout.CENTER);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
Java Applets
Applets are downloaded from the web page they are embedded in. So
both the web page and the applet end up beign transferred to the
client before running
Applets need to load the JVM before they can run, which is why they
are uncommon now - the reason? You generally have at most 8 seconds
to grab the viewer’s attention or they will leave a page.
Applet Examples
Code1)
<html>
<applet code = "WelcomeApplet.class" width = "300" height = "45">
</applet>
</html>
Code2)
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.JApplet; // import class JApplet
<html>
<applet code = "WelcomeApplet2.class" width = "300" height = "60">
</applet>
</html>
Code3)
// Java packages
import java.awt.Graphics; // import class Graphics
35
import javax.swing.JApplet; // import class JApplet
<html>
<APPLET CODE = "WelcomeLines.class" WIDTH = "300" HEIGHT = "40">
</APPLET>
</html>
Code4)
// Java packages
import java.awt.Graphics; // import class Graphics
import javax.swing.*; // import package javax.swing
// add numbers
sum = number1 + number2;
<html>
<applet code = "AdditionApplet.class" width = "300" height = "50">
</applet>
</html>
37
Chapter 14: Streams, Files, File Access
Streams