Topic 04: Graphics

Post on 18-Jan-2015

573 Views

Category:

Education

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Slides

Transcript

Topic 4 : GraphicsDDOOCP

COMPILED BY : ER. PRADIP KHARBUJA

Type Casting

› Java is a strongly typed language. This is a very good feature as the programmer is warned that certain combinations of variable or object types are incompatible thus preventing many run time errors.

› But there are some situations when it is desirable to change the type of a variable or object. This process is known as casting.

› Such an action may lead to the loss of information.

› For example, if the double value 12.98 is cast to an integer type then the result would be 12 (the fractional part is lost).

COMPILED BY : ER. PRADIP KHARBUJA

Type Casting

› Implicit Conversion– automatically converts the one data type into another

› Explicit Conversion

COMPILED BY : ER. PRADIP KHARBUJA

Implicit Conversion

› An automatic type conversion will be used if the following two conditions are met:

1. The two types are compatible.

2. The destination type is larger than the source type.

• byte –> short –> int –> long –> float –> double

COMPILED BY : ER. PRADIP KHARBUJA

Explicit Conversion

COMPILED BY : ER. PRADIP KHARBUJA

› Manual conversion done when the source type is larger than the destination type.

Character to Number and vice-versa

COMPILED BY : ER. PRADIP KHARBUJA

String to Number and vice-versa

COMPILED BY : ER. PRADIP KHARBUJA

› The first example is wrong. This is all to do with the distinction between primitive and reference data types.

› Integer and Double are the class. These are Wrapper Class.

› Wrapper classes are used to convert any data type into an object.

Object to Object Casting

› Do you remember JPasswordField is a child of JTextField?

› Since JTextField is parent, JPasswordField is child, we can do :

COMPILED BY : ER. PRADIP KHARBUJA

CustomButton.java

COMPILED BY : ER. PRADIP KHARBUJA

Draw the following diagrams

COMPILED BY : ER. PRADIP KHARBUJA

Graphics Example Template

COMPILED BY : ER. PRADIP KHARBUJA

Draw a square

COMPILED BY : ER. PRADIP KHARBUJA

Class java.awt.Graphics

› It is the abstract base class.

1. void setColor(Color c)

2. void setFont(Font font)

3. void drawLine(int x1, int y1, int x2, int y2)

4. void drawRect(int x, int y, int width, int height)

5. void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight)

6. void drawOval(int x, int y, int width, int height)

7. void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle)

COMPILED BY : ER. PRADIP KHARBUJA

Class java.awt.Graphics

8. void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)

9. void drawString(String str, int x, int y)

10.void fillRect(int x, int y, int width, int height)

11.void fillRoundRect(int x, int y, int width, int height, int arcWidth, intarcHeight)

12.void fillOval(int x, int y, int width, int height)

13.void fillArc(int x, int y, int width, int height, int startAngle, intarcAngle)

14.void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)

COMPILED BY : ER. PRADIP KHARBUJA

java.awt.Graphics2D

› void drawString(String str, int x, int y)

› void setStroke(Stroke s)

› void translate(int x, int y)

› void rotate(double angle)

COMPILED BY : ER. PRADIP KHARBUJA

Draw a circle

COMPILED BY : ER. PRADIP KHARBUJA

Draw a polygon

COMPILED BY : ER. PRADIP KHARBUJA

Draw the following diagram

COMPILED BY : ER. PRADIP KHARBUJA

Draw the following diagram

COMPILED BY : ER. PRADIP KHARBUJA

Questions

› Explain the difference between casting a string to an integer and using the Integer.parseInt method.

› Assume you are given an array of the numbers 30, 50, 20 and 10. Outline the process by which you could generate a bar chart from this data.

› Explain what is meant by implicit conversion with regards to converting primitive data types to Strings in Java, and provide an example of how to do this in Java.

COMPILED BY : ER. PRADIP KHARBUJA

References

› http://stackoverflow.com/questions/4049580/what-is-the-difference-between-typecasting-and-typeconversion-in-c-or-java

› http://way2java.com/casting-operations/data-type-casting-type-conversion/

› http://www.java2s.com/Book/Java/0020__Language-Basics/Type_Conversion_and_Casting.htm

› http://way2java.com/java-lang/wrapper-classes/

› http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html

› http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html

COMPILED BY : ER. PRADIP KHARBUJA

top related