Transcript ppt

Java Programs
1
project file
– with an extension of .mcp
– contains information that CodeWarrior needs to run
the program
 >=
1 source files
– have an extension of .java
 Project
stationery
– predefined collection of libraries, options, and
sample code for creating different types of programs
Assignment 1
 Goals:
learn about method calls; integer
expressions; Java graphics
 Structure
– read about all of the above
– run a Java program
– modify the Java program
 Stationery:
CUCS Graphics Application
Basic Graphics Model
(0,0)
(0,1)
(0,2)
(0,3)
(1,0)
(1,1)
(1,2)
(1,3)
(2,0) ...
(2,1) ...
(2,2) …
(2,3) ...
(col, row)
Window is viewed as a rectangular grid of point, called pixels.
Number of rows and columns is limited only by the size of the
drawing window.
It’s possible to request objects to be drawn outside the visible
window.
setColor
// Set the color to use in future drawing operations to c. The
// possible values for c are Color.black, Color.blue, Color.cyan,
// Color.magenta, Color.darkGray, Color.gray, Color.green,
// Color.lightGray, Color.orange, Color.pink, Color.red,
// Color.white, and Color.yellow.
public void setColor(Color c)
Example call: setColor(Color.red);
drawRect
// Draw the outline of a rectangle with width w and height h, with
// the left-top corner at point (x,y), using the current color.
public class drawRect(int x, int y, int w, int h)
Example:
drawRect (15, 20, 5, 5);
// Fill the rectangle that would be drawn by drawRect(x,y,w,h);
// with the current color
public class fillRect(int x, int y, int w, int h)
drawOval
// Draw a circle an ellipse that fits exactly within the rectangle
// with top-left corner (x,y), width w, and height h. Use the
// current color. If h=w, the rectangle is a square, so the
// ellipse is a circle
public drawOval(int x, int y, int w, int h)
Example call:
// Draw a circle with center (15,20) and radius 5
drawOval(10, 15, 10, 10);
// Fill the ellipse that would be drawn by drawOval(x,y,w,h); with
// the current color.
public fillOval(int x, int y, int w, int h)
drawLine
// Draw a line from (x1, y1) to (x2, y2), using the current color.
public void drawLine(int x1, int y1, int x2, int y2)
Example call: // Draw a line from (1,5) to (2,10)
drawLine(1, 5, 2, 10);
drawString
// Write String s, using this current color, with the baseline of
// the first character of s point (x, y)
public class drawString(String s, int x, int y)
Example:
drawString(“Hi”, 100, 150);
CUCSDrawing Class
// Class CUCSDrawing: a simple graphics window.
public class CUCSDrawing extends Frame
{
// Method paint is called by the system whenever the
// drawing window needs to be refreshed, including when it is
// first created and when it is brought to the front after
// being hidden by overlapping windows.
// This example draws a black circle and square with red
// titles.
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawOval(15,30,30,30);
g.drawRect(77,30,40,30);
g.setColor(Color.red);
g.drawString("circle",15,80);
g.drawString("rectangle",75,80);
}
}
“main” program
//
//
//
//
Main program for simple Graphics Applications. The program creates the
drawing window and sets its size and title. The actual drawing is done
by the paint method in class Drawing. The drawing window is moved down
the screen slightly so it won't overlap the System.in window if used.
public class CUCSGraphicsApplication
{
public static void main(String args[])
{
CUCSDrawing d = new CUCSDrawing();
d.resize(200,150);
d.move(0,75);
d.setTitle("Drawing");
d.show();
d.toFront();
}
}
Simple integer expressions
 drawOval
( 10+20, 5-2, 2*30+4, 2*(14-10) );
 operator precedence
–
–
–
–
–
–
–
unary operators: +, -, ++, --, and !
*, /, %
+, <, >, <=, >=
==, !=
&&
||
Boolean expressions
 evaluated
just like arithmetic expressions
 evaluate to either TRUE or FALSE
exp1
true
true
false
false
exp2
true
false
true
false
exp1 || exp2
exp && exp2
!exp1
Examples
Assume count = 0, limit = 10;
(count == 0) && (limit < 20)
count == 0 && limit < 20
(limit > 20) || (count < 5)
!(count == 12)
(count == 1) && (x < y)
(count < 10) || (x < y)
Examples
!( ((count < 10) || (x < y)) && (count >= 0) )
((limit/count) > 7) || (limit < 20)
(limit < 20) || ((limit/count) > 7)
((limit/count) > 7) && (limit < 0)
(limit < 0) || ((limit/count) > 7)
(5 && 7) + (!6)