Computer Graphics (ITEC-613) Java Lab Handout
BS(IT)
P-IV First Semester 2016
Lab Handout: 2
Graphics Programming in JAVA
By: Dr. Zeeshan Bhatti
Sample Program
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.geom.*;
public
class Java2DFrame extends javax.swing.JFrame {
public Java2DFrame() {
initComponents();
}
/**
* This is the method where the graphic
objects are drawn.
*
*/
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Line2D lin = new Line2D.Float(100, 100,
250, 260);
g2.draw(lin);
g2.draw(new Rectangle2D.Double(10, 50,
100, 100));
g2.draw(new
RoundRectangle2D.Double(200, 200, 100, 100,
50, 50));
}
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setBounds(0,0,400,400);
this.setLayout(null);
pack();
}
/**
* Starts the program
*
*/
public static void main(String args[]) {
new
Java2DFrame().setVisible(true);
}
}
Drawing
Geometric Primitives in JAVA
The
Java 2D™ API provides several classes that define common geometric objects such
as points, lines, curves, and rectangles. These geometry classes are part of
the
The
The
All examples
represented in this section create geometries by using java.awt.geom
package.The
PathIterator
interface defines methods for
retrieving elements from a path.The
Shape
interface provides a set of
methods for describing and inspecting geometric path objects. This interface is
implemented by the GeneralPath
class and other geometry
classes.java.awt.geom
and then render them by using
the Graphics2D
class. To begin you obtain a Graphics2D
object, for example by casting
theGraphics
parameter of the paint()
method.public void paint (Graphics g) {
Graphics2D g2 = (Graphics2D) g;
...
}
Point
The
Point
class creates a point
representing a location in (x,y) coordinate
space. The subclassesPoint2D.Float
and Point2D.Double
provide correspondingly float
and double precision for storing the coordinates of the point.
//Create Point2D.Double
Point2D.Double point = new Point2D.Double(x, y);
Line
The
Line2D
class represents a line segment
in (x, y) coordinate space. The Line2D.Float
andLine2D.Double
subclasses specify lines in
float and double precision. For example:// draw Line2D.Double
g2.draw(new Line2D.Double(x1, y1, x2, y2));
|
This class includes several
setLine()
methods to define the endpoints
of the line.Aternatively, the endpoints of the line could be specified by using the constructor for the
Line2D.Float
class as follows:Line2D.Float(float X1, float Y1, float X2, float Y2)
Line2D.Float(Point2D p1, Point2D p2)
Curves
1. Quadratic Curve Segment
The
QuadCurve2D
class implements the Shape
interface. This class
represents a quadratic parametric curve segment in (x, y) coordinate space. The QuadCurve2D.Float
and QuadCurve2D.Double
subclasses specify a quadratic
curve in float and double precision.// create new QuadCurve2D.Float
QuadCurve2D q = new QuadCurve2D.Float();
// draw QuadCurve2D.Float with set coordinates
q.setCurve(x1, y1, ctrlx, ctrly, x2, y2);
g2.draw(q);
|
2. Cubic Curve Segment
The
CubicCurve2D
class also implements the Shape
interface. This class represents
a cubic parametric curve segment in (x, y) coordinate space. CubicCurve2D.Float
and CubicCurve2D.Double
subclasses specify a cubic
curve in float and double precision.
// create new CubicCurve2D.Double
CubicCurve2D c = new CubicCurve2D.Double();
// draw CubicCurve2D.Double with set coordinates
c.setCurve(x1, y1, ctrlx1,
ctrly1, ctrlx2, ctrly2, x2, y2);
g2.draw(c);
|
Rectangle
TheRectangle2D
class represents a rectangle
defined by a location (x, y) and dimension (w x h). TheRectangle2D.Float
and Rectangle2D.Double
subclasses specify a rectangle
in float and double precision. For example:// draw Rectangle2D.Double
g2.draw(new Rectangle2D.Double(x, y,
rectwidth,
rectheight));
|
The
RoundRectangle2D
class represents a rectangle
with rounded corners defined by a location (x, y), a dimension (w x h), and the
width and height of the corner arc. The RoundRectangle2D.Float
andRoundRectangle2D.Double
subclasses specify a round
rectangle in float and double precision.The rounded rectangle is specified with following parameters:
- Location
- Width
- Height
- Width of the corner arc
- Height of the corner acr
To set
the location, size, and arcs of a
RoundRectangle2D
object, use the method setRoundRect(double a, double
y, double w, double h, double arcWidth, double arcHeight)
. For example:
// draw RoundRectangle2D.Double
g2.draw(new RoundRectangle2D.Double(x, y,
rectwidth,
rectheight,
10, 10));
|
Ellipse
The
Ellipse is fully
defined by a location, a width and a height. For example:Ellipse2D
class represents an ellipse
defined by a bounding rectangle. The Ellipse2D.Float
andEllipse2D.Double
subclasses specify an ellipse
in float and double precision.// draw Ellipse2D.Double
g2.draw(new Ellipse2D.Double(x, y,
rectwidth,
rectheight));
|
Arc
To
draw a piece of an ellipse, you use the
The Arc2D
class. This class represents an
arc defined by a bounding rectangle, a start angle, an angular extent, and a
closure type. The Arc2D.Float
and Arc2D.Double
subclasses specify an ellipse
in float and double precision.Arc2D
class defines the following
three types of arcs, represented by corresponding constants in this class:
OPEN, PIE and CHORD.- Directly, by coordinates
- By supplied
Point2D
andDimension2D
- By copying an existing
Arc2D
Also,
you can use the
setArcByCenter
method to specify an arc from a
center point, given by its coordinates and a radius.// draw Arc2D.Double
g2.draw(new Arc2D.Double(x, y,
rectwidth,
rectheight,
90, 135,
Arc2D.OPEN));
|
This comment has been removed by the author.
ReplyDeleteSome working frameworks naturally conceal record exentions and I prescribe fixing that if you're planing on working in the realm of outline and programing as its basic for you to dependably realize what document sorts are before you. Java
ReplyDeleteThese handsouts by Sir Zeeshan Bhatti are very important in order to learn the basic of java programming. Please upload its pdf file so that students could download it later.
ReplyDeleteWeb surfers are jagged on information overload. If they don't see what they would like to see on your property page or a landing page, they bounce. small business web design melbourne
ReplyDeletesmm panel
ReplyDeleteSmm Panel
iş ilanları
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
servis
Tiktok Jeton Hilesi İndir