Java Software Solutions - Chapter 2: Data and Expressions

Let's explore some other fundamental programming concepts

Chapter 2 focuses on:

character strings

primitive data

the declaration and use of variables

expressions and operator precedence

data conversions

accepting input from the user

Java applets

introduction to graphics

 

pptx97 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Ngày: 08/06/2015 | Lượt xem: 1350 | Lượt tải: 1download
Tóm tắt nội dung Java Software Solutions - Chapter 2: Data and Expressions, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
to read numeric data. //******************************************************************** import java.util.Scanner; public class GasMileage { //----------------------------------------------------------------- // Calculates fuel efficiency based on values entered by the // user. //----------------------------------------------------------------- public static void main (String[] args) { int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); continue Copyright © 2012 Pearson Education, Inc. continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } } Copyright © 2012 Pearson Education, Inc. continue System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print ("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); mpg = miles / gallons; System.out.println ("Miles Per Gallon: " + mpg); } } Sample Run Enter the number of miles: 328 Enter the gallons of fuel used: 11.2 Miles Per Gallon: 29.28571428571429 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes Copyright © 2012 Pearson Education, Inc. Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics and graphical user interfaces A picture or drawing must be digitized for storage on a computer A picture is made up of pixels (picture elements), and each pixel is stored separately The number of pixels used to represent a picture is called the picture resolution The number of pixels that can be displayed by a monitor is called the monitor resolution Copyright © 2012 Pearson Education, Inc. Representing Images A digitized picture with a small portion magnified: Copyright © 2012 Pearson Education, Inc. Coordinate Systems Each pixel can be identified using a two-dimensional coordinate system When referring to a pixel in a Java program, we use a coordinate system with the origin in the top-left corner Y X (0, 0) (112, 40) 112 40 Copyright © 2012 Pearson Education, Inc. Representing Color A black and white picture could be stored using one bit per pixel (0 = white and 1 = black) A colored picture requires more information; there are several techniques for representing colors Every color can be represented as a mixture of the three additive primary colors Red, Green, and Blue Each color is represented by three numbers between 0 and 255 that collectively are called an RGB value Copyright © 2012 Pearson Education, Inc. The Color Class A color in a Java program is represented as an object created from the Color class The Color class also contains several predefined colors, including the following: Object Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow RGB Value 0, 0, 0 0, 0, 255 0, 255, 255 255, 200, 0 255, 255, 255 255, 255, 0 Copyright © 2012 Pearson Education, Inc. Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes Copyright © 2012 Pearson Education, Inc. Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) A Java applet is a program that is intended to be transported over the Web and executed using a web browser An applet also can be executed using the appletviewer tool of the Java SDK An applet doesn't have a main method Instead, there are several special methods that serve specific purposes Copyright © 2012 Pearson Education, Inc. Applets The paint method is executed automatically whenever the applet’s contents are drawn 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 Copyright © 2012 Pearson Education, Inc. Applets We create an applet by extending the JApplet class The JApplet class is part of the javax.swing package This makes use of inheritance, which is explored in more detail in Chapter 8 See Einstein.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. //******************************************************************** import javax.swing.JApplet; import java.awt.*; public class Einstein extends JApplet { //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawRect (50, 50, 40, 40); // square page.drawRect (60, 80, 225, 30); // rectangle page.drawOval (75, 65, 20, 20); // circle page.drawLine (35, 60, 100, 120); // line page.drawString ("Out of clutter, find simplicity.", 110, 70); page.drawString ("-- Albert Einstein", 130, 100); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Einstein.java Author: Lewis/Loftus // // Demonstrates a basic applet. //******************************************************************** import javax.swing.JApplet; import java.awt.*; public class Einstein extends JApplet { //----------------------------------------------------------------- // Draws a quotation by Albert Einstein among some shapes. //----------------------------------------------------------------- public void paint (Graphics page) { page.drawRect (50, 50, 40, 40); // square page.drawRect (60, 80, 225, 30); // rectangle page.drawOval (75, 65, 20, 20); // circle page.drawLine (35, 60, 100, 120); // line page.drawString ("Out of clutter, find simplicity.", 110, 70); page.drawString ("-- Albert Einstein", 130, 100); } } The HTML applet Tag The Einstein Applet Copyright © 2012 Pearson Education, Inc. An applet is embedded into an HTML file using a tag that references the bytecode file of the applet The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes Copyright © 2012 Pearson Education, Inc. Drawing Shapes Let's explore some of the methods of the Graphics class that draw shapes in more detail A shape can be filled or unfilled, depending on which method is invoked The method parameters specify coordinates and sizes Shapes with curves, like an oval, are usually drawn by specifying the shape’s bounding rectangle An arc can be thought of as a section of an oval Copyright © 2012 Pearson Education, Inc. Drawing a Line X Y 10 20 150 45 page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or Copyright © 2012 Pearson Education, Inc. Drawing a Rectangle X Y page.drawRect (50, 20, 100, 40); 50 20 100 40 Copyright © 2012 Pearson Education, Inc. Drawing an Oval X Y page.drawOval (175, 20, 50, 80); 175 20 50 80 bounding rectangle Copyright © 2012 Pearson Education, Inc. Drawing an Arc An arc is defined by an oval, a start angle, and an arc angle: Copyright © 2012 Pearson Education, Inc. Drawing Shapes Every drawing surface has a background color Every graphics context has a current foreground color Both can be set explicitly See Snowman.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Snowman.java Author: Lewis/Loftus // // Demonstrates basic drawing methods and the use of color. //******************************************************************** import javax.swing.JApplet; import java.awt.*; public class Snowman extends JApplet { //----------------------------------------------------------------- // Draws a snowman. //----------------------------------------------------------------- public void paint (Graphics page) { final int MID = 150; final int TOP = 50; setBackground (Color.cyan); page.setColor (Color.blue); page.fillRect (0, 175, 300, 50); // ground page.setColor (Color.yellow); page.fillOval (-40, -40, 80, 80); // sun continued Copyright © 2012 Pearson Education, Inc. continued page.setColor (Color.white); page.fillOval (MID-20, TOP, 40, 40); // head page.fillOval (MID-35, TOP+35, 70, 50); // upper torso page.fillOval (MID-50, TOP+80, 100, 60); // lower torso page.setColor (Color.black); page.fillOval (MID-10, TOP+10, 5, 5); // left eye page.fillOval (MID+5, TOP+10, 5, 5); // right eye page.drawArc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillRect (MID-15, TOP-20, 30, 25); // top of hat } } Copyright © 2012 Pearson Education, Inc. continued page.setColor (Color.white); page.fillOval (MID-20, TOP, 40, 40); // head page.fillOval (MID-35, TOP+35, 70, 50); // upper torso page.fillOval (MID-50, TOP+80, 100, 60); // lower torso page.setColor (Color.black); page.fillOval (MID-10, TOP+10, 5, 5); // left eye page.fillOval (MID+5, TOP+10, 5, 5); // right eye page.drawArc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillRect (MID-15, TOP-20, 30, 25); // top of hat } } Summary Chapter 2 focused on: character strings primitive data the declaration and use of variables expressions and operator precedence data conversions accepting input from the user Java applets introduction to graphics Copyright © 2012 Pearson Education, Inc. 

File đính kèm:

  • pptxLESSON_02_Data and Expressions.pptx
Tài liệu liên quan