Top Banner
Topic 4 : Graphics DDOOCP COMPILED BY : ER. PRADIP KHARBUJA
21

Topic 04: Graphics

Jan 18, 2015

Download

Education

Pradip Kharbuja

Slides
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Topic 04: Graphics

Topic 4 : GraphicsDDOOCP

COMPILED BY : ER. PRADIP KHARBUJA

Page 2: Topic 04: Graphics

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

Page 3: Topic 04: Graphics

Type Casting

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

› Explicit Conversion

COMPILED BY : ER. PRADIP KHARBUJA

Page 4: Topic 04: Graphics

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

Page 5: Topic 04: Graphics

Explicit Conversion

COMPILED BY : ER. PRADIP KHARBUJA

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

Page 6: Topic 04: Graphics

Character to Number and vice-versa

COMPILED BY : ER. PRADIP KHARBUJA

Page 7: Topic 04: Graphics

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.

Page 8: Topic 04: Graphics

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

Page 9: Topic 04: Graphics

CustomButton.java

COMPILED BY : ER. PRADIP KHARBUJA

Page 10: Topic 04: Graphics

Draw the following diagrams

COMPILED BY : ER. PRADIP KHARBUJA

Page 11: Topic 04: Graphics

Graphics Example Template

COMPILED BY : ER. PRADIP KHARBUJA

Page 12: Topic 04: Graphics

Draw a square

COMPILED BY : ER. PRADIP KHARBUJA

Page 13: Topic 04: Graphics

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

Page 14: Topic 04: Graphics

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

Page 15: Topic 04: Graphics

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

Page 16: Topic 04: Graphics

Draw a circle

COMPILED BY : ER. PRADIP KHARBUJA

Page 17: Topic 04: Graphics

Draw a polygon

COMPILED BY : ER. PRADIP KHARBUJA

Page 18: Topic 04: Graphics

Draw the following diagram

COMPILED BY : ER. PRADIP KHARBUJA

Page 19: Topic 04: Graphics

Draw the following diagram

COMPILED BY : ER. PRADIP KHARBUJA

Page 20: Topic 04: Graphics

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

Page 21: Topic 04: Graphics

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