0% found this document useful (0 votes)
53 views27 pages

CG Lab

The document discusses implementing various graphics primitives and transformations using Java Graphics2D API in Swing applications. It provides code snippets to draw shapes like circles, ellipses, rectangles and lines. It also shows how to apply transformations like translation, rotation, scaling to graphics objects. Methods to draw dashed and styled lines with different line styles are also presented.

Uploaded by

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

CG Lab

The document discusses implementing various graphics primitives and transformations using Java Graphics2D API in Swing applications. It provides code snippets to draw shapes like circles, ellipses, rectangles and lines. It also shows how to apply transformations like translation, rotation, scaling to graphics objects. Methods to draw dashed and styled lines with different line styles are also presented.

Uploaded by

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

7.Implement mid point algorithm to draw circle given its radius and center.

Ans:

package cgi;

import java.util.*;

import java.awt.*;

import javax.swing.*;

class MyPanel7 extends JPanel{

private int r=100,xc=300,yc=200,x,y,p;

public void drawCircle(Graphics g,int xc,int yc,int x,int y){

g.drawOval(x+xc,y+yc,2,2);

g.drawOval(x+xc,-y+yc,2,2);

g.drawOval(-x+xc,y+yc,2,2);

g.drawOval(-x+xc,-y+yc,2,2);

g.drawOval(y+xc,x+yc,2,2);

g.drawOval(y+xc,-x+yc,2,2);

g.drawOval(-y+xc,x+yc,2,2);

g.drawOval(-y+xc,-x+yc,2,2);

public void paint(Graphics g){

p=1-r;

x=0;

y=r;

drawCircle(g,xc,yc,x,y);

while(x<y){

x=x+1;

if(p<0){

p=p+(2*x)+1;
}

else{

y=y-1;

p=p+(2*x)-(2*y)+1;

drawCircle(g,xc,yc,x,y);

public class Q7MidPointCircle extends JFrame{

Q7MidPointCircle(){

setSize(600,500);

setLocation(200,200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

MyPanel7 panel7=new MyPanel7();

add(panel7);

public static void main(String[] args) {

Q7MidPointCircle mpc=new Q7MidPointCircle();

mpc.setVisible(true);

}
Output
8.Implement aid point algorithm to draw ellipse given its primary, secondary
axis and center.
Ans:

package cgi;

import javax.swing.*;

import java.awt.*;

import java.util.*;

public class Q8Ellipse extends JFrame {

private int xc=0,yc=0,a,b,r=2;

public Q8Ellipse(){

setTitle("Ellipse");

setSize(600,700);

setLocation(200,200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

public void drawEllipse(Graphics g,int x,int y){

g.fillOval(x+xc,y+yc, r,r);

g.fillOval(x+xc, -y+yc, r,r);

g.fillOval(-x+xc, y+yc, r,r);

g.fillOval(-x+xc, -y+yc, r,r);

public void paint(Graphics g){

int x,y,p;

x=0;

y=b;

p=b*b-a*a*b;
drawEllipse(g,x,y);

System.out.println(x);

System.out.println(y);

System.out.println(p);

while(b*b*x<a*a*y){

x++;

if(p<0)

p=p+2*b*b*x+b*b;

else{

y=y-1;

p=p+2*b*b*x+b*b-2*a*a*y;

}}

drawEllipse(g,x,y);

System.out.println(x);

System.out.println(y);

System.out.println(p);

while(y>=0){

y--;

if(p>0)

p=p-2*b*b*y+a*a;

else{

x++;

p=p+2*b*b*x+a*a-2*a*a*y;

drawEllipse(g,x,y);

System.out.println(x);
System.out.println(y);

System.out.println(p);

public void setData(){

Scanner sc=new Scanner(System.in);

System.out.println("Enter center of ellipse");

xc=sc.nextInt();

yc=sc.nextInt();

System.out.println("Enter primary and secondary axis");

a=sc.nextInt();

b=sc.nextInt();

public static void main(String[] args) {

Q8Ellipse frame=new Q8Ellipse();

frame.setData();

frame.setVisible(true);

}
9.Draw a rectangle with given x,y, width and height then translate it by(20,20).
Ans:

package cgi;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

class TranslatePanel extends JPanel{

public void RotatRect(Graphics g){

Graphics2D g2=(Graphics2D)g;

Rectangle2D rect=new Rectangle2D.Double(150,150,100,80);

//g2.setColor(Color.BLACK);

g2.draw(rect);

//g2.rotate(Math.toRadians(-30));

g2.translate(20,20);

g2.draw(rect);

public void paint(Graphics g){

RotatRect(g);

public class Q9Translate extends JFrame{

Q9Translate(){

setLocation(100,100);

setSize(500,400);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);

TranslatePanel panel=new TranslatePanel();

add(panel);

public static void main(String[] args) {

Q9Translate frame=new Q9Translate();

frame.setVisible(true);

Output:
10.Draw a rectangle with given x,y, width and height then rotate it by 30 degree
clock wise.
Ans:

package cgi;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

class RotatePanel extends JPanel{

public void RotatRect(Graphics g){

Graphics2D g2=(Graphics2D)g;

Rectangle2D rect=new Rectangle2D.Double(150,150,100,80);

g2.draw(rect);

g2.rotate(Math.toRadians(-30));

g2.draw(rect);

public void paint(Graphics g){

RotatRect(g);

public class Q10RotateClockwise extends JFrame {

Q10RotateClockwise(){

setLocation(100,100);

setSize(400,400);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);
RotatePanel panel=new RotatePanel();

add(panel);

public static void main(String[] args) {

Q10RotateClockwise frame=new Q10RotateClockwise();

frame.setVisible(true);

Output:
11.Draw a rectangle with given x,y. width and height then scale it by(2,2).
Ans:

package cgi;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

class ScalePanel extends JPanel{

public void Scale(Graphics g){

Graphics2D g2=(Graphics2D)g;

Rectangle2D rect=new Rectangle2D.Double(150,150,100,80);

g2.draw(rect);

g2.scale(2,2);

g2.draw(rect);

public void paint(Graphics g){

Scale(g);

public class Q11Scale extends JFrame{

Q11Scale(){

setLocation(100,100);

setSize(700,600);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

ScalePanel panel=new ScalePanel();


add(panel);

public static void main(String[] args) {

Q11Scale frame=new Q11Scale();

frame.setVisible(true);

Output:
12.Write a swing app to draw primitives such as lines, rectangle, circle, ellipse
and triangle using Graphics2D library.
Ans:

package cgi;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

class PrimitivePanel extends JPanel{

public void Primitives(Graphics g){

Graphics2D g2=(Graphics2D)g;

Rectangle2D rect=new Rectangle2D.Double(130,130,110,80);

Line2D l=new Line2D.Double(80,80,80,300);

Ellipse2D e=new Ellipse2D.Float(420,130,160,120);

GeneralPath gp=new GeneralPath();

Ellipse2D cir=new Ellipse2D.Float(270,130,120,120);

gp.moveTo(700, 80);

gp.lineTo(630,200);

gp.lineTo(780,200);

gp.lineTo(700,80);

g2.draw(l);

g2.draw(rect);

g2.draw(e);

g2.draw(gp);

g2.draw(cir);

}
public void paint(Graphics g){

Primitives(g);

public class Q12Primitives extends JFrame{

Q12Primitives(){

setLocation(100,100);

setSize(850,400);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

PrimitivePanel panel=new PrimitivePanel();

add(panel);

public static void main(String[] args) {

Q12Primitives frame=new Q12Primitives();

frame.setVisible(true);

Output:
13. Write a swing app to draw triangle Cubic curve, quadratic curve using
Graphics2D library.
Ans:

package cgi;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

class GraphicPanel extends JPanel{

public void Primitives(Graphics g){

Graphics2D g2=(Graphics2D)g;

CubicCurve2D c=new CubicCurve2D.Double(100,150,400,40,175,250,500,150);

QuadCurve2D q=new QuadCurve2D.Double(450,250,250,250,350,350);

GeneralPath gp=new GeneralPath();

gp.moveTo(700, 80);

gp.lineTo(630,200);

gp.lineTo(780,200);

gp.lineTo(700,80);

g2.draw(gp);

g2.draw(c);

g2.draw(q);

public void paint(Graphics g){

Primitives(g);

}
}

public class Q13Graphics2D extends JFrame{

Q13Graphics2D(){

setLocation(100,100);

setSize(850,400);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

GraphicPanel panel=new GraphicPanel();

add(panel);

public static void main(String[] args) {

Q13Graphics2D frame=new Q13Graphics2D();

frame.setVisible(true);

Output:
14. Write a swing app to draw different styled lines using Graphics2D library.
Ans:

Dashed line

package cgi;

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class MPanel24 extends JPanel{

public void paint(Graphics g){

Graphics2D g2=(Graphics2D)g;

Point2D point1=new Point2D.Double(100,100);

Point2D point2=new Point2D.Double(300,270);

Line2D line=new Line2D.Double(point1,point2);

Stroke stroke=new BasicStroke


(8,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER,1,new float[]{10,20,3,20},0);

g2.setStroke(stroke);

g2.setColor(Color.RED);

g2.draw(line);

g2.translate(0,20);

stroke=new BasicStroke
(8,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER,1,new float[]{25,25},0);

g2.setStroke(stroke);

g2.setColor(Color.GREEN);

g2.draw(line);

}
}

public class Q14DashedLines extends JFrame{

Q14DashedLines(){

setSize(700,500);

setLocation(100,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

MPanel24 panel=new MPanel24();

add(panel);

public static void main(String[] args) {

Q14DashedLines s=new Q14DashedLines();

s.setVisible(true);

Output:
Soid line

package cgi;

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class MPanel29 extends JPanel{

public void solidLines(Graphics g){

Graphics2D g2=(Graphics2D)g;

Point2D point1=new Point2D.Double(100,100);

Point2D point2=new Point2D.Double(300,220);

Line2D line=new Line2D.Double(point1,point2);

Stroke stroke=new BasicStroke


(10,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);

g2.setStroke(stroke);

g2.setColor(Color.RED);

g2.draw(line);

g2.translate(0, 40);

stroke=new BasicStroke (15,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);

g2.setStroke(stroke);

g2.setColor(Color.BLUE);

g2.draw(line);

g2.translate(0, 40);

stroke=new BasicStroke(20,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);

g2.setStroke(stroke);

g2.setColor(Color.GREEN);

g2.draw(line);

}
public void paint(Graphics g){

solidLines(g);

public class Q14SolidLines extends JFrame{

Q14SolidLines (){

setSize(700,500);

setLocation(100,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

MPanel29 panel=new MPanel29();

add(panel);

public static void main(String[] args) {

Q14SolidLines s=new Q14SolidLines();

s.setVisible(true);

Output:
15. Write a swing app to fill rectangle, Cubic curve with different colors using
Graphics2D library.
Ans:

package cgi;

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import javax.swing.*;

class GraphicFillPanel extends JPanel{

public void Primitives(Graphics g){

Graphics2D g2=(Graphics2D)g;

Rectangle2D rect=new Rectangle2D.Double(600,100,190,100);

CubicCurve2D c=new CubicCurve2D.Double(100,150,400,40,175,250,500,150);

g2.setColor(Color.ORANGE);

g2.fill(rect);

g2.setColor(Color.RED);

g2.fill(c);

public void paint(Graphics g){

Primitives(g);

}
public class Q15FillGraphic2D extends JFrame{

Q15FillGraphic2D (){

setLocation(100,100);

setSize(850,400);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

GraphicFillPanel panel=new GraphicFillPanel();

add(panel);

public static void main(String[] args) {

Q15FillGraphic2D frame=new Q15FillGraphic2D ();

frame.setVisible(true);

Output:
16.Write a swing app to draw following vehicle using Graphics2D library.
Ans:

and open the template in the editor.

*/

package cgi;

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class GraphicPanel1 extends JPanel{

public void DrawCar(Graphics g){

Graphics2D g2=(Graphics2D)g;

Stroke stroke=new BasicStroke


(6,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);

g2.setStroke(stroke);

GeneralPath gp=new GeneralPath();

gp.moveTo(50, 130);

gp.lineTo(100,130);

gp.quadTo(120,160,140,130);

gp.lineTo(210,130);

gp.quadTo(240,160,250,130);

gp.lineTo(280,130);

gp.curveTo(280,90,270,85,220,75);

gp.lineTo(140,75);

gp.lineTo(100,100);

gp.lineTo(50,100);

gp.lineTo(50,130);

g2.draw(gp);
}

public void paint(Graphics g){

DrawCar(g);

public class Q16Vehicle extends JFrame{

Q16Vehicle (){

setSize(400,400);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

GraphicPanel1 panel=new GraphicPanel1();

add(panel);

public static void main(String[] args) {

Q16Vehicle frame=new Q16Vehicle();

frame.setVisible(true);

Output:
17.Select ant one logo given below and write a Swing app to generate the
selected logo using Graphics2D library.

Ans:

package cgi;

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

class MPanel23 extends JPanel{

public void solidLines(Graphics g){

Graphics2D g2=(Graphics2D)g;

Point2D point1=new Point2D.Double(100,100);

Point2D point2=new Point2D.Double(200,100);

Line2D line=new Line2D.Double(point1,point2);

Stroke stroke=new BasicStroke


(30,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);

g2.setStroke(stroke);

g2.setColor(Color.RED);

g2.draw(line);

Point2D point3=new Point2D.Double(100,100);

Point2D point4=new Point2D.Double(190,100);

Line2D line1=new Line2D.Double(point3,point4);

Stroke stroke1=new BasicStroke


(30,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);

g2.translate(0, 40);
g2.setStroke(stroke1);

g2.setColor(Color.BLUE);

g2.draw(line1);

Point2D point5=new Point2D.Double(100,100);

Point2D point6=new Point2D.Double(100,150);

Line2D line2=new Line2D.Double(point5,point6);

Stroke stroke2=new BasicStroke


(30,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER);

g2.setStroke(stroke2);

g2.setColor(Color.BLUE);

g2.draw(line2);

public void paint(Graphics g){

solidLines(g);

public class Q17Logo extends JFrame{

Q17Logo (){

setSize(300,300);

setLocation(100,100);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setLocationRelativeTo(null);

MPanel23 panel=new MPanel23();

add(panel);
}

public static void main(String[] args) {

Q17Logo l=new Q17Logo();

l.setVisible(true);

Output:

You might also like