180 likes | 263 Views
Introduction to Programming, CST1205. Poppy Pickard, Deane C2-4 pp7@bolton.ac.uk 01204 903 407. Two types of programs. Applet - Part of web page (can test on web page but need browser or applet viewer) Application - free standing program purpose inventory, billing, payroll etc. etc.
E N D
Introduction to Programming, CST1205 Poppy Pickard, Deane C2-4 pp7@bolton.ac.uk 01204 903 407
Two types of programs • Applet - Part of web page (can test on web page but need browser or applet viewer) • Application - free standing program • purpose • inventory, billing, payroll etc. etc.
Programs to help use Java • Eclipse (Editor and SDK) • J2SE SDK (Java 2, Standard Edition, Software Development Kit) this provides the following: • Compiler - create Class file (from source) • Linker - pulls in libraries • Appletviewer - to view Applets
The Java Language • The language is built from classes. • Classes contain attributes and methods • Related classes can be grouped together in a package. eg the awt (abstract windows toolkit) Packages can be imported to make them available to a class Warnings • Case must match (uppercase/lowercase) Greeting.java NOT greeting.java • File name must be same as class file name • Spelling - braces - punctuation
Text of a Java Program This contains one class called Greeting, inside the class is one method called paint. The program is saved in a file called Greeting.java import java.awt.*; import java.applet.Applet; public class Greeting extends Applet { public void paint (Graphics g) { g.drawString(“Hello” , 50, 50); }//end paint }//end class
HTML File Greeting.htm (other names possible) <title> Web page applet </title> <applet code = “Greeting.class” width = 300 height = 200> </applet> This is then saved and then viewed using an AppletViewer
“A first Picture” import java.awt.*; import java.applet.Applet; public class FirstLine extends Applet { public void paint (Graphics g) { g.drawLine (0,0,100,100); }// end paint }// end paint
Alter old HTML or Write New One <title> Web page applet </title> <applet code = “FirstLine.class” width = 300 height = 200> </applet> START 0,0 300,0 100,100 END 300,200 0,200 g.drawLine(0,0,100,100);
Consider a method call: g.drawLine(0,0,100,100)“actual parameters or arguments” • doSomething( ); has no parameters
The Paint Method • “Signature Line” • public void paint (Graphics g) { Objectname Classtype security returntype methodname
public = security, who can “see” modify method • void = return “nothing” this time… • paint = name of method (could be anything) • (Graphics = Class type • g = name of particular object of Graphics class) • { begin/start of content of method • } // end method
Methods for Drawing • Rectangles: drawRect(…); fillRect(…); • Ovals: drawOval(…); fillOval(…); • Arcs: drawArc(… ) fillArc(…); • Line: drawLine(…); • Details about the input parameters for of these methods can be found in the Learning Aid: Producing graphics using AWT
For example: drawArc ( 6 parameters) • X position • Y position • Width • Height • Starting point (360 degree of Clock) • Total Angle of Arc
g.drawArc( 100,200,200,150,180,90 ) x start, y start, x diameter, y diameter, start angle, arc angle (100, 200) 200 180° 90 degree (12:00 o’clock) 150 180 degree(9:00 o’clock) 0 degree (3:00 o’clock ) 270 degree ( 6:00 o’clock)
COLORS STANDARD 13 • 256 * 256 * 256 (Lots of possible) • black blue cyan darkGray • gray green lightGray magenta • orange pink red white • yellow Color Commands • setBackground(Color.lightGray) ;//note not associated with Graphic object g • g.setColor(Color.red);
Sequence of “Graphics” Applet import java.awt.*; import java applet.Applet; public class FirstShapes extends Applet { public void paint (Graphics g) { g.drawRect (30,30,80,40); g.drawOval(120,30,50,50); g.setColor(Color.black); g.fillRect(30,100,80,40); g.drawLine (30,160,130,170); g.drawArc (30,180,50,50,60,40); g.fillArc(120,180,50,50,60,40); } }
Add some text… • g.drawString (“ Hello World “, 30, 40); Y position Horizontal X Concatenation + operator • g.drawString(“Hi” + “there” + “Mike” ,100,100);
Comments (Types) //single line comments /* This is an example of a multi line comment */ /** Special JDK comment used for manualsnot in J++ JBuilder */