110 likes | 188 Views
Introductory Graphics. In this section we will learn how about how to draw graphics on the screen in Java: Drawing Lines The Graphics Screen Drawing Shapes Colours Filled Shapes Displaying Characters Comments. Drawing Lines. import java.awt.*;
E N D
Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java: Drawing Lines The Graphics Screen Drawing Shapes Colours Filled Shapes Displaying Characters Comments Introductory Graphics
Drawing Lines import java.awt.*; import java.applet.Applet; public class FirstLine extends Applet { public void paint(Graphics g) { g.drawLine(0, 0, 100, 100); } } Create a file FirstLine.java containing this: Create a file FirstLine.html containing this: <html> <head> <title>Web Page with Applet</title> </head> <body> <applet code="FirstLine.class" width=300 height=250 ></applet> </body> </html> CompileFirstLine.java and run the appletviewer on FirstLine.html Introductory Graphics
What does it mean? When the applet runs, every time the Java Virtual Machine decides the window needs to be redrawn (at the start, if you resize the window etc.) it calls a method called paint. You supply the code the that executes when paint is called as shown: import java.awt.*; import java.applet.Applet; public class FirstLine extends Applet { public void paint(Graphics g) { g.drawLine(0, 0, 100, 100); } } In a simple program like this there is only one window - however in a complicated program there may be many. The 'Graphics g' is a parameter being passed to paint to tell you which window to use. The g.drawLine(...) means draw a line on the window called g. Introductory Graphics
(0, 0) y The Graphics Screen Java graphics (like most languages) uses a coordinate system based on pixels. The whole screen is made up of many pixels which are small dots that can each be set to a separate colour. Modern displays support several different numbers of pixels : (Width X Height) 640 X 480, 800 X 600, 1024 X 768, 1152 X 864, . . . Each pixel has an x and y coordinate measured across and down from the top left hand corner (not like a conventional x-y coordinate system). x Introductory Graphics
Drawing a Line (x1, y1) g.drawLine (x1, y1, x2, y2); public void paint(Graphics g) { g.drawLine(0, 0, 100, 100); } (x2, y2) Introductory Graphics
Drawing Other Shapes Line: (x2, y2) g.drawLine (x1, y1, x2, y2 ); (x1, y1) width (x1, y1) Rectangle: height g.drawRect (x1, y1, width, height ); width (x1, y1) Oval (Circle or Ellipse): g.drawOval (x1, y1, width, height ); height arc angle (x1, y1) Arc: g.drawArc (x1, y1, width, height, angle1, angle ); angle1 height The Oval and Arc shapes are drawn inside an imaginary rectangle. width Introductory Graphics
Colours setBackground (colour); You can set the background colour using: g.setColor (colour ); You can set the colour of the next object you draw using: There are 13 standard colours as members of the Color class: Note: US Color not colour black ( 0, 0, 0) blue ( 0, 0, 255) cyan ( 0, 255, 255) darkGray ( 64, 64, 64) gray (128, 128, 128) green ( 0, 255, 0) lightGray (192, 192, 192) magenta (255, 0, 255) orange (255, 200, 0) pink (255, 175, 175) red (255, 0, 0) white (255, 255, 255) yellow (255, 255, 0) These are the amount of Red, Green and Blue on a scale of 0 to 255 Note: US gray not grey Color mycolour = new Color(R, G, B ); Or you can define your own using: setBackground(Color.lightGray); Color myGreen = new Color(0, 200, 0); g.setColor(myGreen); e.g. Introductory Graphics
Filled Shapes As well as the draw methods there are a set of methods for filling the shapes with identical parameters to those of the draw equivalents. width (x1, y1) Rectangle: height g.fillRect (x1, y1, width, height ); width (x1, y1) Oval (Circle or Ellipse): g.fillOval (x1, y1, width, height ); height arc angle (x1, y1) Arc: g.fillArc (x1, y1, width, height, angle1, angle ); height angle1 fillArc produces a pie shape rather than an arc. width Introductory Graphics
Filling Shapes Example import java.awt.*; import java.applet.Applet; public class FirstShapes extends Applet { public void paint(Graphics g) { setBackground(Color.lightGray); g.drawRect(30, 30, 80, 40); g.drawOval(120, 30, 50, 50); Color myGreen = new Color(0, 200, 0); g.setColor(myGreen); g.fillRect(30, 100, 80, 40); g.fillOval(120, 100, 50, 50); g.drawLine(30, 160, 130, 170); g.setColor(Color.black); g.drawArc(30, 180, 50, 50, 60, 40); g.setColor(Color.red); g.fillArc(120, 180, 50, 50, 60, 40); } } The statements are executed sequentially (unless we specify otherwise). The background is set, the unfilled rectangle are drawn using the default colour (black), the colour is set to myGreen and the filled rectangle and oval drawn, the colour is set back to black and the arc drawn and finally the colour is set to red and the pie drawn. Introductory Graphics
Displaying Characters We have already used drawString to display characters: g.drawString("Hello World!", 50, 50); The parameters are: g.drawString (string, x1, y1 ); The start of the string (x1, y1) The String Imaginary line on which the characters rest The string itself The simplest string is a sequence of characters enclosed within double-quote " " characters. Introductory Graphics
Comments You can annotate your programs with comments. These are statements or parts of statements in the file which help you or someone else understand the program but are ignored by the computer. There are two types: Single line comments - everything following a // is treated as a comment and ignored. This line is ignored This line is ignored from here onwards // Define my own colours Color myGreen = new Color(0, 200, 0); g.setColor(myGreen); g.fillOval(120, 100, 50, 50); // This oval will be green Multi-line comments - everything between a /* and a */ is treated as a comment and ignored. /* This is my first Applet * * Author: A.N.Other * * Date: 1st April 2000 */ Not necessary - only makes it look pretty All this is ignored Comments things that are not obvious but not those that are ! Introductory Graphics