1 / 18

Introduction to Programming, CST1205

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.

chaela
Download Presentation

Introduction to Programming, CST1205

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming, CST1205 Poppy Pickard, Deane C2-4 pp7@bolton.ac.uk 01204 903 407

  2. 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.

  3. 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

  4. 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

  5. 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

  6. 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

  7. “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

  8. 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);

  9. Consider a method call: g.drawLine(0,0,100,100)“actual parameters or arguments” • doSomething( ); has no parameters

  10. The Paint Method • “Signature Line” • public void paint (Graphics g) { Objectname Classtype security returntype methodname

  11. 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

  12. 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

  13. For example: drawArc ( 6 parameters) • X position • Y position • Width • Height • Starting point (360 degree of Clock) • Total Angle of Arc

  14. 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)

  15. 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);

  16. 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); } }

  17. Add some text… • g.drawString (“ Hello World “, 30, 40); Y position Horizontal X Concatenation + operator • g.drawString(“Hi” + “there” + “Mike” ,100,100);

  18. Comments (Types) //single line comments /* This is an example of a multi line comment */ /** Special JDK comment used for manualsnot in J++ JBuilder */

More Related