Download presentation
Presentation is loading. Please wait.
Published byJeffry Brandon Mitchell Modified over 9 years ago
1
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical components b A picture or drawing must be digitized for storage on a computer b A picture is broken down into pixels, and each pixel is stored separately
2
2 Representing Color b A black and white picture can be stored using one bit per pixel (0 = white and 1 = black) b A color picture requires more information, and there are several techniques for representing a particular color b For example, every color can be represented as a mixture of the three primary colors Red, Green, and Blue b In Java, each color is represented by three numbers between 0 and 255 that are collectively called an RGB value
3
3 Coordinate Systems b Each pixel can be identified using a two-dimensional coordinate system b When referring to a pixel in a Java program, we use a coordinate system with the origin in the upper left corner Y X(0, 0) (112, 40) 112 40
4
4 Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) b An applet is a Java program that is intended to transported over the web and executed using a web browser b An applet can also be executed using the appletviewer tool of the Java Software Development Kit An applet doesn't have a main method b Instead, there are several special methods that serve specific purposes The paint method, for instance, is automatically executed and is used to draw the applets contents
5
5 Applets The paint method accepts a parameter that is an object of the Graphics class A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes b The class that defines the applet extends the Applet class b This makes use of inheritance, an object-oriented concept explored in more detail in Chapter 7 b See Einstein.java (page 93) Einstein.java
6
6 Applets b An applet is embedded into an HTML file using a tag that references the bytecode file of the applet class b It is actually the bytecode version of the program that is transported across the web b The applet is executed by a Java interpreter that is part of the browser
7
7 Drawing Shapes Let's explore some of the methods of the Graphics class that draw shapes in more detail b A shape can be filled or unfilled, depending on which method is invoked b The method parameters specify coordinates and sizes b Recall from Chapter 1 that the Java coordinate system has the origin in the upper left corner b Many shapes with curves, like an oval, are drawn by specifying its bounding rectangle b An arc can be thought of as a section of an oval
8
8 Drawing a Line X Y 10 20 150 45 page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or
9
9 Drawing a Rectangle X Y page.drawRect (50, 20, 100, 40); 50 20 100 40
10
10 Drawing an Oval X Y page.drawOval (175, 20, 50, 80); 175 20 50 80 boundingrectangle
11
11 The Color Class A color is defined in a Java program using an object created from the Color class The Color class also contains several static predefined colors b Every graphics context has a current foreground color b Every drawing surface has a background color b See Snowman.java (page 99-100) Snowman.java
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.