0% found this document useful (0 votes)
22 views28 pages

Advance Desktop Applications - Lecture 8

Advanced desktop applications lecture 8

Uploaded by

mirzadsami2116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views28 pages

Advance Desktop Applications - Lecture 8

Advanced desktop applications lecture 8

Uploaded by

mirzadsami2116
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Desktop Application Development

MOHAMMAD SALIM HAMDARD

KABUL UNIVERSITY Monday, July 29, 2024 1


Lecture 9
GRAPHICS PROGRAMMING

KABUL UNIVERSITY Monday, July 29, 2024 2


OUTCOMES

Should know about Graphics Coordinate System


Should Learn about java.awt.Graphics Class
Drawing Different Shapes on Applet Screen
Practical work
JAVA GRAPHICS PROGRAMMING

Graphics is one of the most important features of Java. Java applets


can be written to draw lines, arcs, figures, images and text in different
fonts and styles. Different colors can also be incorporated in display.
The Graphics Class:
The graphics class defines a number of drawing functions, Each shape
can be drawn edge-only or filled. To draw shapes on the screen, we
may call one of the methods available in the graphics class. The most
commonly used drawing methods included in the graphics class are
listed in coming slides. To draw a shape, we only need to use the
appropriate method with the required arguments.

KABUL UNIVERSITY Monday, July 29, 2024 4


METHODS OF GRAPHICS CLASS

Sr. No Method Description


1. drawArc() Draws a hollow arc
2. drawLine() Draws a straight line
3. drawOval() Draws a hollow oval
4. drawPolygon() Draws a hollow polygon
5. drawRect() Draws a hollow rectangle
6. Draws a hollow rectangle
drawRoundRect()
with rounded corners
7. drawString() Display a text string
8. FillArc() Draws a filled arc
9 fillOval()
Draws a filled Ova
KABUL UNIVERSITY Monday, July 29, 2024 5
Sr. No Method Description
10. fillPolygon() Draws a filled Polygon
11. fillRect() Draws a filled rectangle
12. fillRoundRect() Draws a filled rectangle
with rounded corners
13. getColor() Retrieves the current
drawing color
14. getFont() Retrieves the currently
used font
15 setColor() Sets the drawing color
16 setFont() Sets the font

KABUL UNIVERSITY Monday, July 29, 2024 6


HOW TO SET LOCATION

The upper-left corner of computer screen has coordinates (0,0) it means both
x and y coordinate has values 0.

KABUL UNIVERSITY Monday, July 29, 2024 7


LINE

Lines are drawn by means of the drawLine() method.


Syntax
void drawLine(int startX, int startY, int endX, int endY)
drawLine() displays a line in the current drawing color that begins at
(start X, start Y) and ends at (endX, end Y).

KABUL UNIVERSITY Monday, July 29, 2024 8


CODE

package GraphicsProgramming;
import java.awt.*;
import java.applet.*;

public class LinesEx1 extends Applet {


public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
g.drawLine(40,25,250,180);
g.drawLine(5,290,80,19);
}

KABUL UNIVERSITY Monday, July 29, 2024 9


KABUL UNIVERSITY Monday, July 29, 2024 10
RECTANGLE

The drawRect() and fillRect() methods display an outlined and filled


rectangle, respectively.

Syntax
void drawRect(int top, int left, int width, int height)
void fillRect(int top, int left, int width, int height)

The upper-left corner of the rectangle is at(top,left). The dimensions


of the rectangle are specified by width and height.
Use drawRoundRect() or fillRoundRect() to draw a rounded rectangle.
A rounded rectangle has rounded corners.

KABUL UNIVERSITY Monday, July 29, 2024 11


CODE
package GraphicsProgramming;
import java.awt.*;
import java.applet.*;
public class RectangleEx2 extends Applet
{
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);

g.drawRoundRect(190,10,60,50,15,15);

g.fillRoundRect(70,90,140,100,30,40);
}

KABUL UNIVERSITY Monday, July 29, 2024 12


KABUL UNIVERSITY Monday, July 29, 2024 13
CIRCLE AND ELLIPSE

The Graphics class does not contain any method for circles or ellipses.
To draw an ellipse, use drawOval(). To fill an ellipse, use fillOval().

Syntax
void drawOval(int top, int left, int width, int height)
void fillOval(int top, int left, int width, int height)

To draw a circle, specify a square as the bounding rectangle i.e get


height = width.

KABUL UNIVERSITY Monday, July 29, 2024 14


CODE
package GraphicsProgramming;
import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses" width=300
Height=300>
</applet> */
public class OvalEx3 extends Applet{
public void paint(Graphics g)
{
g.drawOval(10,10,60,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}

}
KABUL UNIVERSITY Monday, July 29, 2024 15
KABUL UNIVERSITY Monday, July 29, 2024 16
DRAWING ARCS

An arc is a part of oval. Arcs can be drawn with draw Arc() and
fillArc() methods.

Syntax
void drawArc(int top, int left, int width, int height, int startAngle, int
arcAngle)

void fillArc(int top, int left, int width, int height, int startAngle, int
arcAngle)

KABUL UNIVERSITY Monday, July 29, 2024 17


CODE
package GraphicsProgramming;
import java.awt.*;
import java.applet.*;
/*
<applet code="Arcs" width=300
Height=300>
</applet>
public class ArcsEx4 extends Applet {
public void paint(Graphics g)
{
g.drawArc(10,40,70,70,0,75);
g.fillArc(100,40,70,70,0,90);

g.drawArc(10,100,70,80,0,175);

g.drawArc(200,80,80,80,0,180);
}

}
KABUL UNIVERSITY Monday, July 29, 2024 18
KABUL UNIVERSITY Monday, July 29, 2024 19
POLYGON

Polygons are shapes with many sides. It may be considered 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 line, and so on. Use drawPolygon() and fillPolygon() to draw
arbitrarily shaped figures.
Syntax
void drawPolygon(int x[] , int y[], int numPointer)
void fillPolygon(int x[] , int y[], int numPointer)

KABUL UNIVERSITY Monday, July 29, 2024 20


CODE EX1 package GraphicsProgramming;
import java.awt.*;
import java.applet.*;
/*
<applet code="Polygon" width=300
Height=300>
</applet>
public class PolygonEx5 extends Applet {
public void paint(Graphics g)
{
int
xpoints[]={30,200,30,200};
int
ypoints[]={30,30,180,180};
int num=4;

g.drawPolygon(xpoints,ypoints,num);
}

KABUL UNIVERSITY
} Monday, July 29, 2024 21
KABUL UNIVERSITY Monday, July 29, 2024 22
CODE EX2
package GraphicsProgramming;
import java.awt.*;
import java.applet.*;
public class PolygonEx6 extends Applet {
public void paint(Graphics g)
{
int xs[]={100,160,220};
int ys[]={60,20,60};
g.fillPolygon(xs,ys,3);
g.drawRect(100, 60, 120, 120);
g.drawRect(140, 120, 30, 60);
}

KABUL UNIVERSITY Monday, July 29, 2024 23


KABUL UNIVERSITY Monday, July 29, 2024 24
DRAWING EMOJI
package GraphicsProgramming;
import java.applet.*;
import java.awt.*;
/*<applet code ="Smiley" width=600
height=600>
</applet>*/
public class SmilyFaceEx7 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.YELLOW);
g.fillOval(80, 60, 140, 140);
g.setColor(Color.BLACK);
g.fillOval(120, 120, 15, 15);
g.fillOval(170, 120, 15, 15);
g.drawArc(130, 160, 50, 20, 180,
180);
}}
KABUL UNIVERSITY Monday, July 29, 2024 25
KABUL UNIVERSITY Monday, July 29, 2024 26
KABUL UNIVERSITY Monday, July 29, 2024 27
Questions?

KABUL UNIVERSITY Monday, July 29, 2024 28

You might also like