Chapter 5.2
Chapter 5.2
2 Graphics Programming
5th Unit
Mr. R. M. Patil
SY CO, JPR
2023-24
Graphics Programming
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).
//Drawing Lines
import java.awt.*;
import java.applet.*;
/*
<applet code=“appl" width=300 Height=250>
</applet>
*/
public class appl 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);
}
}
Output
:
Rectangles
The drawRect() and fillRect() methods display
an outlined and filled rectangle, respectively.
Syntax:
void drawRect(int top, int left, int width, int height);
Syntax:
void drawRoundRect(int top, int left, int width, int height, int Xdiam, int Ydiam);
void fillRoundRect(int top, int left, int width, int height, int Xdiam, int Ydiam);
/*
<applet code=“appl" width=500 Height=500>
</applet>
*/
S-16, 4 Marks
import java.awt.*;
import java.applet.*;
/*
<applet code=“appl" width=300 Height=300>
</applet>
*/
g.setColor(Color.blue);
g.drawString(“Hello Third Year Students”, 80,160);
}
}
Ellipse, Circle
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);
Circle
Arcs
An arc is a part of oval.
Arcs can be drawn with drawArc() and fillArc() methods.
Syntax:
void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
Syntax:
void drawArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
void fillArc(int top, int left, int width, int height, int startAngle, int sweepAngle)
/*
<applet code="Arcs" width=300 Height=300>
</applet>
*/
Syntax:
/*
<applet code="Polygon" width=300 Height=300>
</applet>
*/
S-16, 4 Marks
import java.awt.*;
import java.applet.*;
/*
<applet code="Polygon" width=300 Height=300>
</applet>
*/
- S-18, 4 Marks
Colors
Color can be controlled by accessing the Color class.
Available Colors
red
yellow
blue
orange
pink
cyan
magenta
black
white
gray
lightGray
darkGray
Colors
- W-14,W-15, 4 Marks
- 4 Marks
Fonts Class ( W-14 )
- W-14, 8 Marks
The font class states fonts, which are used to render text in a
visible way.
Variable defined by font class – Name, pointSize, size, style
Variable Meaning
1.protected String name Name of the font
2.protected int size Size of the font in points
3.protected int style Font style
4.protected float pointSize Point size of the font in float
There are four styles for displaying fonts in Java: plain, bold, italic,
and bold italic.
Three class constants are used to represent font styles:
Example –
g.setFont(f);
}
Font Methods – S-16, W-16, S-18
Methods Meaning
getFamily() Returns the family of the font.
Returns a font object from the system properties
getFont()
list.
getFontName() Gets the logical name of the font.
getName() The full font name.
getSize() The point size for this font.
getStyle() Returns the style of this font.
It returns an array containing a one point size
getAllFonts() instance of all fonts available in this Graphics
Environment.
getAvailableFont It returns an array containing the name of all
FamilyName() fonts families in the Graphics Environment.
import java.awt.*;
import java.applet.*;
/*
<applet code=“fontexample" width=300 Height=300>
</applet>
*/
public class fontexample extends Applet
{
public void paint(Graphics g)
{
GraphicsEnvironment e =GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontnames = e.getAvailableFontFamilyNames();
g.drawString(“Fonts available on this platform: “,20,20);
int y=20;
for (int i = 0; i < fontnames.length; i++)
{
g.drawString(fontnames[i],20,y);
y+=20;
}
}
}