Topic 4 : Graphics
DDOOCP
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 com...
Type Casting
› Implicit Conversion
– automatically converts the one data type
into another
› Explicit Conversion
COMPILED ...
Implicit Conversion
› An automatic type conversion will be used if the following two
conditions are met:
1. The two types ...
Explicit Conversion
COMPILED BY : ER. PRADIP KHARBUJA
› Manual conversion done when the source type is larger than the des...
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...
Object to Object Casting
› Do you remember JPasswordField is a child of JTextField?
› Since JTextField is parent, JPasswor...
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 draw...
Class java.awt.Graphics
8. void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
9. void drawString(String str, int ...
java.awt.Graphics2D
› void drawString(String str, int x, int y)
› void setStroke(Stroke s)
› void translate(int x, int y)
...
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 ...
References
› http://stackoverflow.com/questions/4049580/what-is-the-
difference-between-typecasting-and-typeconversion-in-...
Upcoming SlideShare
Loading in …5
×

Topic 04: Graphics

2,433 views

Published on

Slides

Published in: Education
  • Be the first to comment

  • Be the first to like this

Topic 04: Graphics

  1. 1. Topic 4 : Graphics DDOOCP COMPILED BY : ER. PRADIP KHARBUJA
  2. 2. 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
  3. 3. Type Casting › Implicit Conversion – automatically converts the one data type into another › Explicit Conversion COMPILED BY : ER. PRADIP KHARBUJA
  4. 4. 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
  5. 5. Explicit Conversion COMPILED BY : ER. PRADIP KHARBUJA › Manual conversion done when the source type is larger than the destination type.
  6. 6. Character to Number and vice-versa COMPILED BY : ER. PRADIP KHARBUJA
  7. 7. 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.
  8. 8. 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
  9. 9. CustomButton.java COMPILED BY : ER. PRADIP KHARBUJA
  10. 10. Draw the following diagrams COMPILED BY : ER. PRADIP KHARBUJA
  11. 11. Graphics Example Template COMPILED BY : ER. PRADIP KHARBUJA
  12. 12. Draw a square COMPILED BY : ER. PRADIP KHARBUJA
  13. 13. 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
  14. 14. 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, int arcHeight) 12.void fillOval(int x, int y, int width, int height) 13.void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 14.void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) COMPILED BY : ER. PRADIP KHARBUJA
  15. 15. 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
  16. 16. Draw a circle COMPILED BY : ER. PRADIP KHARBUJA
  17. 17. Draw a polygon COMPILED BY : ER. PRADIP KHARBUJA
  18. 18. Draw the following diagram COMPILED BY : ER. PRADIP KHARBUJA
  19. 19. Draw the following diagram COMPILED BY : ER. PRADIP KHARBUJA
  20. 20. 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
  21. 21. 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.ht ml › http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D .html COMPILED BY : ER. PRADIP KHARBUJA

×