SlideShare a Scribd company logo
1 of 12
Thank You!
L ogistics
E d i t t h i s t e x t h e r e
JAVA
Seminar
Basic Graphics
and Animation Presented By :
Prakash Kumar
(MCA/25023/22)
Introduction
One of the most important features of java is its
ability to draw graphics. We can write Java applets
that draw lines, figures of different shapes, images
and text in different fonts and styles. We can also
incorporate different colors in display.
Every applet has its own area of the screen known as
canvas, where it creates its display. Java Coordinate
system has the origin (0,0) in the upper-left corner.
Positive x values are to the right and positive y values
are to the bottom. The values of the coordinates x
and y are in pixels.
The Graphics Class
Java’s Graphics class includes methods for drawing many
different types of shapes, from simple lines to polygons
to text in a variety of fonts. To draw a shape on the
screen, we may call one of the methods available in the
Graphics class. To draw a shape, we only need to use the
appropriate method with the required arguments.
Drawing Methods of the Graphics Class:
Method Description
clearRect() Erases a rectangular area of the canvas
copyArea() Copies a rectangular area of the canvas to
another area
drawArc() Draws a hallow arc
drawLine() Draws a Straight Line
Method Description
drawOval() Draws a hollow oval
drawPolygon() Draws a hollow polygon
drawRect() Draws a hollow rectangle
drawRoundRect() Draws a hollow rounded corner rectangle
drawstring() Display a text string
fillArc() Draws a filled arc
fillOval() Draws a filled oval
fillPolygon() Draws a filled polygon
fillRect() Draws a filled rectangle
fillRoundRect() Draws a filled rounded corner rectangle
getColor() Retrieves the current drawing color
getFont() Retrieves the currently used font
setColor() Sets the drawing color
setFont() Sets the font
(Contd.)
Line
s
Line is the simplest shape we can draw with the
graphics class. The drawLine() method takes two pair
of coordinates, (x1, y1) and (x2, y2) as arguments and
draws a line between them.
For example, the following statement draws a straight
line from the coordinate point (20, 20) to (70, 70) :
g.drawLine (20, 20, 70, 70);
The g is the Graphics object passed to the method.
Rectangles
We can draw a rectangle using the drawRect() method. This
method takes four arguments.
The first two represent the x and y coordinates of the top-left
corner of the rectangle, and the remaining two represent the
width and the height of the rectangle.
For example, the following statement draws a rectangle starting
at (10, 60) having a width of 40 pixels and a height of 30 pixels.
The drawRect() method draws only the outline of a box.
g.drawRect (10 , 60, 40 , 30);
(x , y) width
height
Rectangle
Circles and Ellipses
The graphics class does not have any method for circles or
ellipses. The drawOval() method can be used to draw a
circle or an ellipse. Ovals are just like rectangles with overly
rounded corners.
The drawOval() method takes four arguments. The first two
represents the top-left corner of the imaginary rectangle
and the other two represent the width and height of the oval
itself. If the width and the height are same then the oval
becomes a circle.
Height
width
Oval within an imaginary rectangle
Drawing Arcs
An arc is a part of an oval. The drawArc() designed to
draw arcs takes six arguments.
The first four are the same as the arguments for
drawOval() method and the last two represent the
starting angle of the arc and the number of degrees
(sweep angle) around the arc.
90˚
180˚ 0˚
270˚
Arc as a part of an oval
Drawing Polygons
Polygons are shapes with many sides. A polygon may be
considered as a set of lines connected together.
The end of the first line is the beginning of the second line,
the end of the second line is the beginning of the third,
and so on. This suggests that we can draw a polygon with n
sides using the drawLine() method n times in succession.
For Ex: To draw a Polygon with 3 sides (triangle) having
vertices (10, 20), (170, 40) and (80, 140).
public void paint (Graphics g) {
g.drawLine(10, 20, 170, 40)
g.drawLine(170, 40, 80, 140)
g.drawLine(80, 140, 10, 20)
}
(10, 20)
(170, 40)
(80, 140)
A polygon with three sides
We can draw polygon more conveniently using the
drawPolygon() method of graphics class. This method takes
3 arguments:
 An array of integers containing x coordinates
 An array of integers containing y coordinates
 An integer for the total number of points
The polygon above can also be drawn as:
public void paint (Graphics g) {
int xPoints [] = {10, 170, 80, 10};
int yPoints [] = {20, 40, 140, 20};
g.drawPolygon (xPoints, yPoints, nPoints);
}
//Applet for drawing a human face
import java.awt.*;
import java.applet.*;
public class Face extends Applet
{
public void paint (Graphics g)
{
g.drawOval (40, 40, 120, 150);
g.drawOval (57, 75, 30, 20);
g.drawOval (110, 75, 30,20);
g.filloval (68, 81, 10, 10);
g.filloval (121, 81, 10, 10) ;
g.drawOval (85, 100, 30, 30);
g.fillAro (60, 125, 80, 40, 180, 180);
g.drawOval (25, 92, 15, 30);
g.drawOval (160, 92, 15, 30);
}
}
Output
Thank You
Reference :- Programming with JAVA
Sixth Edition, E Balagurusamy

More Related Content

What's hot

What's hot (20)

Applets
AppletsApplets
Applets
 
Segments in Graphics
Segments in GraphicsSegments in Graphics
Segments in Graphics
 
HOMOGENEOUS CO-ORDINATES IN COMPUTER GRAPHICS PPT
HOMOGENEOUS CO-ORDINATES IN COMPUTER GRAPHICS PPTHOMOGENEOUS CO-ORDINATES IN COMPUTER GRAPHICS PPT
HOMOGENEOUS CO-ORDINATES IN COMPUTER GRAPHICS PPT
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Character generation
Character generationCharacter generation
Character generation
 
Object oriented approach in python programming
Object oriented approach in python programmingObject oriented approach in python programming
Object oriented approach in python programming
 
Applet programming
Applet programming Applet programming
Applet programming
 
Generics
GenericsGenerics
Generics
 
Java I/O
Java I/OJava I/O
Java I/O
 
Web controls
Web controlsWeb controls
Web controls
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Graphics software and standards
Graphics software and standardsGraphics software and standards
Graphics software and standards
 
PYTHON FEATURES.pptx
PYTHON FEATURES.pptxPYTHON FEATURES.pptx
PYTHON FEATURES.pptx
 
3D Display Method
3D Display Method3D Display Method
3D Display Method
 
Introduction to Java -unit-1
Introduction to Java -unit-1Introduction to Java -unit-1
Introduction to Java -unit-1
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
Scan line method
Scan line methodScan line method
Scan line method
 

Similar to Basic Graphics in Java

Fundamentals of Multimedia - Vector Graphics.pdf
Fundamentals of Multimedia - Vector Graphics.pdfFundamentals of Multimedia - Vector Graphics.pdf
Fundamentals of Multimedia - Vector Graphics.pdf
FatihahIrra
 
computer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdfcomputer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdf
SyedSajjadShah3
 

Similar to Basic Graphics in Java (20)

Bt9301, computer graphics
Bt9301, computer graphicsBt9301, computer graphics
Bt9301, computer graphics
 
Bt9301, computer graphics
Bt9301, computer graphicsBt9301, computer graphics
Bt9301, computer graphics
 
Fundamentals of Multimedia - Vector Graphics.pdf
Fundamentals of Multimedia - Vector Graphics.pdfFundamentals of Multimedia - Vector Graphics.pdf
Fundamentals of Multimedia - Vector Graphics.pdf
 
Chirantan (java)
Chirantan   (java)Chirantan   (java)
Chirantan (java)
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Shi.pdf
Shi.pdfShi.pdf
Shi.pdf
 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphics
 
paper
paperpaper
paper
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 
Geometric model & curve
Geometric model & curveGeometric model & curve
Geometric model & curve
 
raster algorithm.pdf
raster algorithm.pdfraster algorithm.pdf
raster algorithm.pdf
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
canvas.pptx
canvas.pptxcanvas.pptx
canvas.pptx
 
computer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdfcomputer graphics lab manual 2013-converted.pdf
computer graphics lab manual 2013-converted.pdf
 
Unit-1 basics of computer graphics
Unit-1 basics of computer graphicsUnit-1 basics of computer graphics
Unit-1 basics of computer graphics
 
JDK and AWT
JDK and AWTJDK and AWT
JDK and AWT
 
testpang
testpangtestpang
testpang
 
Output primitives computer graphics c version
Output primitives   computer graphics c versionOutput primitives   computer graphics c version
Output primitives computer graphics c version
 
Computer graphics notes 2 tutorials duniya
Computer graphics notes 2   tutorials duniyaComputer graphics notes 2   tutorials duniya
Computer graphics notes 2 tutorials duniya
 
canvas_1.pptx
canvas_1.pptxcanvas_1.pptx
canvas_1.pptx
 

Recently uploaded

DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DrGurudutt
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Lovely Professional University
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
ENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu sweENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu swe
MohammadAliNayeem
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
Kamal Acharya
 
Lecture_8-Digital implementation of analog controller design.pdf
Lecture_8-Digital implementation of analog controller design.pdfLecture_8-Digital implementation of analog controller design.pdf
Lecture_8-Digital implementation of analog controller design.pdf
mohamedsamy9878
 

Recently uploaded (20)

Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdfDR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
DR PROF ING GURUDUTT SAHNI WIKIPEDIA.pdf
 
Electrical shop management system project report.pdf
Electrical shop management system project report.pdfElectrical shop management system project report.pdf
Electrical shop management system project report.pdf
 
Planetary Gears of automatic transmission of vehicle
Planetary Gears of automatic transmission of vehiclePlanetary Gears of automatic transmission of vehicle
Planetary Gears of automatic transmission of vehicle
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
ENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu sweENCODERS & DECODERS - Digital Electronics - diu swe
ENCODERS & DECODERS - Digital Electronics - diu swe
 
School management system project report.pdf
School management system project report.pdfSchool management system project report.pdf
School management system project report.pdf
 
Lecture_8-Digital implementation of analog controller design.pdf
Lecture_8-Digital implementation of analog controller design.pdfLecture_8-Digital implementation of analog controller design.pdf
Lecture_8-Digital implementation of analog controller design.pdf
 
Online book store management system project.pdf
Online book store management system project.pdfOnline book store management system project.pdf
Online book store management system project.pdf
 
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
RM&IPR M5 notes.pdfResearch Methodolgy & Intellectual Property Rights Series 5
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
Top 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering ScientistTop 13 Famous Civil Engineering Scientist
Top 13 Famous Civil Engineering Scientist
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
 
Arduino based vehicle speed tracker project
Arduino based vehicle speed tracker projectArduino based vehicle speed tracker project
Arduino based vehicle speed tracker project
 
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbineLow rpm Generator for efficient energy harnessing from a two stage wind turbine
Low rpm Generator for efficient energy harnessing from a two stage wind turbine
 
Lect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptxLect_Z_Transform_Main_digital_image_processing.pptx
Lect_Z_Transform_Main_digital_image_processing.pptx
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 

Basic Graphics in Java

  • 1. Thank You! L ogistics E d i t t h i s t e x t h e r e JAVA Seminar Basic Graphics and Animation Presented By : Prakash Kumar (MCA/25023/22)
  • 2. Introduction One of the most important features of java is its ability to draw graphics. We can write Java applets that draw lines, figures of different shapes, images and text in different fonts and styles. We can also incorporate different colors in display. Every applet has its own area of the screen known as canvas, where it creates its display. Java Coordinate system has the origin (0,0) in the upper-left corner. Positive x values are to the right and positive y values are to the bottom. The values of the coordinates x and y are in pixels.
  • 3. The Graphics Class Java’s Graphics class includes methods for drawing many different types of shapes, from simple lines to polygons to text in a variety of fonts. To draw a shape on the screen, we may call one of the methods available in the Graphics class. To draw a shape, we only need to use the appropriate method with the required arguments. Drawing Methods of the Graphics Class: Method Description clearRect() Erases a rectangular area of the canvas copyArea() Copies a rectangular area of the canvas to another area drawArc() Draws a hallow arc drawLine() Draws a Straight Line
  • 4. Method Description drawOval() Draws a hollow oval drawPolygon() Draws a hollow polygon drawRect() Draws a hollow rectangle drawRoundRect() Draws a hollow rounded corner rectangle drawstring() Display a text string fillArc() Draws a filled arc fillOval() Draws a filled oval fillPolygon() Draws a filled polygon fillRect() Draws a filled rectangle fillRoundRect() Draws a filled rounded corner rectangle getColor() Retrieves the current drawing color getFont() Retrieves the currently used font setColor() Sets the drawing color setFont() Sets the font (Contd.)
  • 5. Line s Line is the simplest shape we can draw with the graphics class. The drawLine() method takes two pair of coordinates, (x1, y1) and (x2, y2) as arguments and draws a line between them. For example, the following statement draws a straight line from the coordinate point (20, 20) to (70, 70) : g.drawLine (20, 20, 70, 70); The g is the Graphics object passed to the method.
  • 6. Rectangles We can draw a rectangle using the drawRect() method. This method takes four arguments. The first two represent the x and y coordinates of the top-left corner of the rectangle, and the remaining two represent the width and the height of the rectangle. For example, the following statement draws a rectangle starting at (10, 60) having a width of 40 pixels and a height of 30 pixels. The drawRect() method draws only the outline of a box. g.drawRect (10 , 60, 40 , 30); (x , y) width height Rectangle
  • 7. Circles and Ellipses The graphics class does not have any method for circles or ellipses. The drawOval() method can be used to draw a circle or an ellipse. Ovals are just like rectangles with overly rounded corners. The drawOval() method takes four arguments. The first two represents the top-left corner of the imaginary rectangle and the other two represent the width and height of the oval itself. If the width and the height are same then the oval becomes a circle. Height width Oval within an imaginary rectangle
  • 8. Drawing Arcs An arc is a part of an oval. The drawArc() designed to draw arcs takes six arguments. The first four are the same as the arguments for drawOval() method and the last two represent the starting angle of the arc and the number of degrees (sweep angle) around the arc. 90˚ 180˚ 0˚ 270˚ Arc as a part of an oval
  • 9. Drawing Polygons Polygons are shapes with many sides. A polygon may be considered as a set of lines connected together. The end of the first line is the beginning of the second line, the end of the second line is the beginning of the third, and so on. This suggests that we can draw a polygon with n sides using the drawLine() method n times in succession. For Ex: To draw a Polygon with 3 sides (triangle) having vertices (10, 20), (170, 40) and (80, 140). public void paint (Graphics g) { g.drawLine(10, 20, 170, 40) g.drawLine(170, 40, 80, 140) g.drawLine(80, 140, 10, 20) }
  • 10. (10, 20) (170, 40) (80, 140) A polygon with three sides We can draw polygon more conveniently using the drawPolygon() method of graphics class. This method takes 3 arguments:  An array of integers containing x coordinates  An array of integers containing y coordinates  An integer for the total number of points The polygon above can also be drawn as: public void paint (Graphics g) { int xPoints [] = {10, 170, 80, 10}; int yPoints [] = {20, 40, 140, 20}; g.drawPolygon (xPoints, yPoints, nPoints); }
  • 11. //Applet for drawing a human face import java.awt.*; import java.applet.*; public class Face extends Applet { public void paint (Graphics g) { g.drawOval (40, 40, 120, 150); g.drawOval (57, 75, 30, 20); g.drawOval (110, 75, 30,20); g.filloval (68, 81, 10, 10); g.filloval (121, 81, 10, 10) ; g.drawOval (85, 100, 30, 30); g.fillAro (60, 125, 80, 40, 180, 180); g.drawOval (25, 92, 15, 30); g.drawOval (160, 92, 15, 30); } } Output
  • 12. Thank You Reference :- Programming with JAVA Sixth Edition, E Balagurusamy

Editor's Notes

  1. Links: https://www.pexels.com/photo/black-sail-ship-on-body-of-water-906982/
  2. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  3. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  4. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  5. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  6. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  7. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  8. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  9. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  10. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/
  11. https://www.pexels.com/photo/man-in-bubble-jacket-holding-tablet-computer-4484078/