java programs u 1 project file –with an extension of.mcp –contains information that codewarrior...

14
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

Upload: octavia-barton

Post on 23-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

Java ProgramsJava 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

Page 2: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

Assignment 1Assignment 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

Page 3: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

Basic Graphics ModelBasic Graphics Model

(0,0) (1,0) (2,0) ...(0,1) (1,1) (2,1) ...(0,2) (1,2) (2,2) …(0,3) (1,3) (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.

Page 4: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

setColorsetColor

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

Page 5: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

drawRectdrawRect

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

Page 6: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

drawOvaldrawOval

// 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 circlepublic drawOval(int x, int y, int w, int h)

Example call: // Draw a circle with center (15,20) and radius 5drawOval(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)

Page 7: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

drawLinedrawLine

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

Page 8: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

drawStringdrawString

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

Page 9: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

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

}}

Page 10: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

“main” program“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();

}}

Page 11: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

Simple integer expressions Simple integer expressions drawOval ( 10+20, 5-2, 2*30+4, 2*(14-10) ); operator precedence

– unary operators: +, -, ++, --, and !– *, /, %– +, -– <, >, <=, >=– ==, !=– &&– ||

Page 12: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

Boolean expressionsBoolean expressions evaluated just like arithmetic expressions evaluate to either TRUE or FALSE

exp1 exp2 exp1 || exp2 exp && exp2 !exp1true true true false false true false false

Page 13: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

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

Page 14: Java Programs u 1 project file –with an extension of.mcp –contains information that CodeWarrior needs to run the program u >= 1 source files –have an extension

ExamplesExamples

!( ((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)