CG Lab
CG Lab
Ans:
package cgi;
import java.util.*;
import java.awt.*;
import javax.swing.*;
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);
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);
Q7MidPointCircle(){
setSize(600,500);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel7);
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 Q8Ellipse(){
setTitle("Ellipse");
setSize(600,700);
setLocation(200,200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
g.fillOval(x+xc,y+yc, r,r);
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);
xc=sc.nextInt();
yc=sc.nextInt();
a=sc.nextInt();
b=sc.nextInt();
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.*;
Graphics2D g2=(Graphics2D)g;
//g2.setColor(Color.BLACK);
g2.draw(rect);
//g2.rotate(Math.toRadians(-30));
g2.translate(20,20);
g2.draw(rect);
RotatRect(g);
Q9Translate(){
setLocation(100,100);
setSize(500,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
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.*;
Graphics2D g2=(Graphics2D)g;
g2.draw(rect);
g2.rotate(Math.toRadians(-30));
g2.draw(rect);
RotatRect(g);
Q10RotateClockwise(){
setLocation(100,100);
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
RotatePanel panel=new RotatePanel();
add(panel);
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.*;
Graphics2D g2=(Graphics2D)g;
g2.draw(rect);
g2.scale(2,2);
g2.draw(rect);
Scale(g);
Q11Scale(){
setLocation(100,100);
setSize(700,600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
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.*;
Graphics2D g2=(Graphics2D)g;
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);
Q12Primitives(){
setLocation(100,100);
setSize(850,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
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.*;
Graphics2D g2=(Graphics2D)g;
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);
Primitives(g);
}
}
Q13Graphics2D(){
setLocation(100,100);
setSize(850,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
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.*;
Graphics2D g2=(Graphics2D)g;
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);
}
}
Q14DashedLines(){
setSize(700,500);
setLocation(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
s.setVisible(true);
Output:
Soid line
package cgi;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
Graphics2D g2=(Graphics2D)g;
g2.setStroke(stroke);
g2.setColor(Color.RED);
g2.draw(line);
g2.translate(0, 40);
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);
Q14SolidLines (){
setSize(700,500);
setLocation(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
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.*;
Graphics2D g2=(Graphics2D)g;
g2.setColor(Color.ORANGE);
g2.fill(rect);
g2.setColor(Color.RED);
g2.fill(c);
Primitives(g);
}
public class Q15FillGraphic2D extends JFrame{
Q15FillGraphic2D (){
setLocation(100,100);
setSize(850,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
frame.setVisible(true);
Output:
16.Write a swing app to draw following vehicle using Graphics2D library.
Ans:
*/
package cgi;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
Graphics2D g2=(Graphics2D)g;
g2.setStroke(stroke);
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);
}
DrawCar(g);
Q16Vehicle (){
setSize(400,400);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
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.*;
Graphics2D g2=(Graphics2D)g;
g2.setStroke(stroke);
g2.setColor(Color.RED);
g2.draw(line);
g2.translate(0, 40);
g2.setStroke(stroke1);
g2.setColor(Color.BLUE);
g2.draw(line1);
g2.setStroke(stroke2);
g2.setColor(Color.BLUE);
g2.draw(line2);
solidLines(g);
Q17Logo (){
setSize(300,300);
setLocation(100,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
add(panel);
}
l.setVisible(true);
Output: