Download Using Predefined Classes and Objects - Slides | CMPU 101 and more Study notes Computer Science in PDF only on Docsity!
Chapter 3
Using Predefined Classes & Objects
CMPU-101: Problem-Solving and Abstraction
Marc Smith (sect. 52)
Vassar College
Spring 2007
1
Plan
Chapter 3:
Use predefined classes & objects
Chapters 4 and 5:
Define new instances
of predefined classes
Chapter 6:
Define new classes
A Sampling of Predefined Classes
Classes for:
• Reading & writing
• Files & directories
• Graphics & GUI
Specific classes (with intuitive purposes):
• Date
• Time
• Calendar
Anatomy of Statement to Print to the Screen
System.out.println("hello")
System.out
an instance of class PrintStream
the receiver
println
method applicable to PrintStreams
"hello"
actual parameter/argument to println
4
A First Java Program
Filename: First.java Compile & run source code in DrJava Compiling produces the file: First.class If class has a main method, run it by typing java First in the Interactions Pane If class has no main method, instantiate class and send messages to the object from the Interactions Pane. 5
First.java
// Written by Barry Soroka // // A first Java program. // ///////////////////////////////////////////////////////////////// class First { //--------------------------------------------------------------- public static void main ( String [] args ) throws Exception { System.out.println("hello"); } //--------------------------------------------------------------- } // end class First ///////////////////////////////////////////////////////////////// Comments are used to separate sections of code and to introduce program author and purpose White space and indentation make your program more readable...
FirstBad.java
// Program FirstBad class First { public static void main(String[ ] args ) throws Exception{ System . out . println( "hello");}} ... but this code would work just as well
DrJava Compiling & Executing
1. Open file: First.java
2. Compile by clicking on button “Compile All”
! compiling produces file: First.class
3. Execute by typing the following in the Interactions Pane
java First
! this works since class First has a main() method
12
Signatures & Overloading
signature = method name + parameter type(s)
println(String)
println()
println is overloaded because there are two forms,
differing only in parameters
! it is the name of the method that is overloaded
! two or more methods may share the same name within
a class, provided they have different signatures
13
Printing abc — Prog 0301
// Written by Barry Soroka // // Write out // abc // //////////////////////////////////////////////////////////////////////// class Prog 0301 { //------------------------------------------------------------------------- Prog 0301 () { System.out.println("abc"); } //------------------------------------------------------------------------- } // end class Prog 0301 ///////////////////////////////////////////////////////////////////////////
Exercise
Brainstorm different ways to print out abc using print and
println
GUI : Visible objects
Pixel — "picture element" — the smallest dot on a
computer screen
Java’s graphics libraries: awt & swing (overlap in classes)
Applet — a graphics program which requires a browser
Application — a stand-alone graphics program
16
Coordinates
This is not the coordinate system we’re accustomed to…
x
y
(0, 0) 17
Colors
On-screen image is specified by assigning a color to
each pixel in the image
RGB — Red, Green, Blue color channels (a triple)
Each color channel has an intensity in the range [0,255].
(R,G,B) Color ( 255 , 255 , 255 ) white ( 0 , 0 , 0 ) black
Each color is defined by a triple of numbers,
specifying intensity of red, green, and blue (RGB)
Color Java’s Built - In Color Name RGB Values black Color.BLACK ( 0 , 0 , 0 ) blue Color.BLUE^ (^0 ,^0 ,^255 ) cyan Color.CYAN ( 0 , 255 , 255 ) gray Color.GRAY^ (^128 ,^128 ,^128 ) dark gray Color.DARK_GRAY^ (^64 ,^64 ,^64 ) light gray Color.LIGHT_GRAY^ (^192 ,^192 ,^192 ) green Color.GREEN ( 0 , 255 , 0 ) magenta Color.MAGENTA ( 255 , 0 , 255 ) orange Color.ORANGE ( 255 , 200 , 0 ) pink Color.PINK^ (^255 ,^175 ,^175 ) red Color.RED^ (^255 ,^0 ,^0 ) white Color.WHITE^ (^255 ,^255 ,^255 ) yellow Color.YELLOW ( 255 , 255 , 0 )
JFrame
A JFrame is a rectangular graphical object with
width
height
upper-left corner
color
Java graphics work by placing elements onto JFrames
that are visible to the user.
Prog
24
public MyPanel()
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(700,450));
public void paintComponent ( Graphics page )
super.paintComponent(page);
page.drawRect(100,150,500,150);
page.fillOval(150,250,100,100);
page.fillOval(450,250,100,100);
25 Common geometric objects populate the JPanel. drawRect(x,y,width,height) fillRect(x,y,width,height) oval — bounding box drawOval(x,y,width,height) fillOval(x,y,width,height) setColor(Color.RED) drawLine(x 1 ,y 1 ,x 2 ,y 2 )
Prog 0304 — Display a JPEG Image
class MyPanel extends JPanel
public MyPanel()
this.setBackground(Color.WHITE);
this.setPreferredSize(new Dimension(400,300));
public void paintComponent ( Graphics page )
super.paintComponent(page);
Image image = new ImageIcon("bacon.jpg").getImage();
page.drawImage(image,50,50,this);
} // end class MyPanel
Prog 0305 — Diagonals
class MyPanel extends JPanel …
public void paintComponent ( Graphics page )
super.paintComponent(page);
int width = getWidth();
int height = getHeight();
page.setColor(Color.YELLOW);
page.drawLine(0,0,width,height);
page.drawLine(0,height,width,0);