KOMPUTER GRAFIK FOR UAS
AddArea
import java.awt.*;
import java.awt.geom.*;
import java.applet.Applet;
public class AddArea extends Applet
{ public void paint(Graphics g)
{
Graphics2D g2 =
(Graphics2D)g;
//lingkaran pertama
Shape s1 = new Ellipse2D.Double(0, 0, 100,
100);
//lingkaran kedua
Shape s2 = new
Ellipse2D.Double(60, 0, 100, 100);
//pindah posisi kedua
kedua lingkaran dengan x=20 y=50
g2.translate(20, 50);
//area geometri pertama
Area a2 = new Area(s1);
//area geometri kedua
Area a1 = new Area(s2);
//gambar lingkaran
pertama
g2.draw(s1);
//gambar lingkaran kedua
g2.draw(s2);
//operasi seleksi
"intersect"
a1.add(a2);
//penentuan ketebalan dan
warna garis
BasicStroke bsThickLine =
new BasicStroke(5.0f);
g2.setStroke(bsThickLine);
g2.setPaint(Color.blue);
//gambar garis bidang hasil
operasi "intersect"
g2.draw(a1);
//penentuan warna untuk
mengisi bidang hasil operasi "intersect".
g2.setPaint(Color.green);
//pewarnaan bidang hasil
operasi "intersect".
g2.fill(a1);
}
}
Clear
import java.applet.Applet;
import java.awt.*;
import java.awt.Color;
public class Clear1 extends Applet
{
public void paint (Graphics
g)
{
Graphics2D g2 =
(Graphics2D) g;
BasicStroke bs = new
BasicStroke(9f);
g2.setStroke(bs);
g.setColor(Color.pink);
g.drawRect(20, 20, 180,
180);
g.setColor(Color.blue);
g.fillOval(20, 20, 180,
180);
g.clearRect(20, 20, 80,
80);
g.clearRect(120, 120, 80,
80);
}
}
ExclusiveOrArea
import java.awt.*;
import java.awt.geom.*;
import java.applet.Applet;
public class ExclusiveOrArea extends Applet
{ public void paint(Graphics g)
{
Graphics2D g2 =
(Graphics2D)g;
//lingkaran pertama
Shape s1 = new
Ellipse2D.Double(0, 0, 100, 100);
//lingkaran kedua
Shape s2 = new
Ellipse2D.Double(60, 0, 100, 100);
//pindah posisi kedua
kedua lingkaran dengan x=20 y=50
g2.translate(20, 50);
//area geometri pertama
Area a2 = new Area(s1);
//area geometri kedua
Area a1 = new Area(s2);
//gambar lingkaran
pertama
g2.draw(s1);
//gambar lingkaran kedua
g2.draw(s2);
//operasi seleksi "intersect"
a1.exclusiveOr(a2);
//penentuan ketebalan dan
warna garis
BasicStroke bsThickLine =
new BasicStroke(5.0f);
g2.setStroke(bsThickLine);
g2.setPaint(Color.blue);
//gambar garis bidang hasil operasi
"intersect"
g2.draw(a1);
//penentuan warna untuk
mengisi bidang hasil operasi "intersect".
g2.setPaint(Color.green);
//pewarnaan bidang hasil
operasi "intersect".
g2.fill(a1);
}
}
General Path
import java.awt.*;
import java.awt.geom.*;
public class General_Path extends java.applet.Applet
{
public void paint (Graphics
g)
{
super.paint(g);
g.setColor(Color.green);
for (int i=0; i<100;
i++)
{
g.drawLine(0+i*10, 0,
0+i*10, 1000);
g.drawLine(0, 0+i*10,
1000, 0+i*10);
}
Graphics2D g2d =
(Graphics2D) g;
g2d.setColor(Color.green);
g2d.setColor(new Color
(100,50,25));
GeneralPath gp = new
GeneralPath();
gp.moveTo(60,120);
gp.lineTo(80,120);
gp.quadTo(90,140,100,120);
gp.lineTo(160,120);
gp.quadTo(170,140,180,120);
gp.lineTo(200,120);
gp.curveTo(195,100,200,80,160,80);
gp.lineTo(110,80);
gp.lineTo(90,100);
gp.lineTo(60,100);
gp.lineTo(60,120);
BasicStroke grTebal = new
BasicStroke(5.0f);
g2d.setStroke(grTebal);
g2d.draw(gp);
}
}
GeneralPathWithPolyline
import java.applet.Applet;
import java.awt.*;
import java.awt.geom.*;
public class generalPathWithPolyline extends Applet
{
public void paint (Graphics
g)
{
Graphics2D g2d = (Graphics2D) g;
double titik_X[] =
{83.15661, 183.15661, 183.15661, 227.44233,
227.44233,
278.8709, 278.8709, 313.15661,
313.15661,
356.01376};
double titik_Y[] =
{315.29111, 315.29111, 192.43397, 192.43397,
315.29111,
315.29111, 99.576832, 99.576832,
315.29111,
315.29111};
GeneralPath p = new
GeneralPath();
// Titik Acuan atau titik
awal
p.moveTo(titik_X[0],
titik_Y[0]);
//Menghubungkan titik
lain dengan garis
for (int i = 1; i <
titik_X.length; i++)
{ p.lineTo(titik_X[i], titik_Y[i]); }
g2d.draw(p);
}
}
IntersectArea
import java.awt.*;
import java.awt.geom.*;
import java.applet.Applet;
public class IntersectArea extends Applet
{
public void paint(Graphics g)
{
Graphics2D g2 =
(Graphics2D)g;
//lingkaran pertama
Shape s1 = new
Ellipse2D.Double(0, 0, 100, 100);
//lingkaran kedua
Shape s2 = new
Ellipse2D.Double(60, 0, 100, 100);
//pindah posisi kedua
kedua lingkaran dengan x=20 y=50
g2.translate(20, 50);
//area geometri pertama
Area a2 = new Area(s1);
//area geometri kedua
Area a1 = new Area(s2);
//gambar lingkaran
pertama
g2.draw(s1);
//gambar lingkaran kedua
g2.draw(s2);
//operasi seleksi
"intersect"
a1.intersect(a2);
//penentuan ketebalan dan
warna garis
BasicStroke bsThickLine =
new BasicStroke(5.0f);
g2.setStroke(bsThickLine);
g2.setPaint(Color.blue);
//gambar garis bidang
hasil operasi "intersect"
g2.draw(a1);
//penentuan warna untuk
mengisi bidang hasil operasi "intersect".
g2.setPaint(Color.green);
//pewarnaan bidang hasil
operasi "intersect".
g2.fill(a1);
}
}
Rotasi
import java.awt.*;
import java.awt.Graphics2D;
import java.applet.*;
public class Rotasi extends Applet
{
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2d =
(Graphics2D) g;
g2d.setColor(Color.red);
g2d.fillRect(20, 20, 80,
50);
g2d.translate(170, 10);
g2d.rotate(Math.PI / 4);
g2d.setColor(Color.blue);
g2d.fillRect(20, 20, 80,
50);
}
}
Rotasi Radians
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.applet.Applet;
import java.awt.geom.*;
public class rotasiRadians extends Applet
{
public void paint(Graphics g)
{
Graphics2D g2d =
(Graphics2D) g;
Line2D gr2d1 = new Line2D.Double(0,0,200,0);
Line2D gr2d2 = new
Line2D.Double(0,0,0,200);
//Objek Pertama
g2d.setColor(Color.red);
g2d.draw(gr2d1);
g2d.draw(gr2d2);
g2d.fillRect(20, 20, 80,
50);
// Memindahkan Objek
Pertama ke tempt yang baru
// => translate(xbaru,
yBaru)
g2d.translate(170, 50);
//Proses merotasi objek
g2d.rotate(Math.toRadians(45));
//Objek baru yang telah
dirotasi
g2d.setColor(Color.blue);
g2d.draw(gr2d1);
g2d.draw(gr2d2);
g2d.fillRect(20, 20, 80,
50);
}
}
Rotasi Pi
import java.awt.*;
import java.awt.Graphics2D;
import java.applet.Applet;
import java.awt.geom.*;
public class rotasi_pi_2 extends Applet
{
public void paint(Graphics g)
{
Graphics2D g2d =
(Graphics2D) g;
//Objek Pertama pada
Sumbu Koordinat Awal
BasicStroke grTebal = new
BasicStroke(8.0f);
g2d.setStroke(grTebal);
g2d.translate(200,200);
Line2D gr2d1 = new
Line2D.Double(0,0,100,0);
Line2D gr2d2 = new
Line2D.Double(0,0,0,100);
g2d.setColor(Color.red);
g2d.draw(gr2d1);
g2d.draw(gr2d2);
//Objek Graphics diputar
g2d.rotate(Math.PI/4);
g2d.setColor(Color.green);
g2d.draw(gr2d1);
g2d.draw(gr2d2);
/*g2d.rotate(Math.PI/3);
g2d.setColor(Color.blue);
g2d.draw(gr2d1);
g2d.draw(gr2d2);
g2d.rotate(Math.PI/4);
g2d.setColor(Color.pink);
g2d.draw(gr2d1);
g2d.draw(gr2d2);
g2d.rotate(Math.PI/5);
g2d.setColor(Color.black);
g2d.draw(gr2d1);
g2d.draw(gr2d2);*/
}
}
Bunga (quis Komgraf)
import java.awt.*;
import java.awt.geom.*;
import java.applet.Applet;
public class bunga extends Applet
{ public void paint(Graphics g)
{
Graphics2D g2 =
(Graphics2D)g;
Shape s1 = new
Ellipse2D.Double(120, 50, 100, 100);
Shape s2 = new
Ellipse2D.Double(200, 50, 100, 100);
Shape s3 = new
Ellipse2D.Double(200, 100, 100, 100);
Shape s4 = new
Ellipse2D.Double(120, 100, 100, 100);
g2.translate(20, 50);
Area a1 = new Area(s1);
Area a2 = new Area(s2);
Area a3 = new Area(s3);
Area a4 = new Area(s4);
g2.draw(s1);
g2.draw(s2);
g2.draw(s3);
g2.draw(s4);
a1.add(a4);
a2.add(a3);
BasicStroke bsThickLine =
new BasicStroke(5.0f);
g2.setStroke(bsThickLine);
g2.setPaint(Color.black);
g2.draw(a1);
g2.draw(a2);
g2.draw(a3);
g2.draw(a4);
g2.setPaint(Color.green);
g2.fill(a1);
g2.setPaint(Color.red);
g2.fill(a2);
g2.setPaint(Color.yellow);
g2.fill(a3);
g2.setPaint(Color.blue);
g2.fill(a4);
}
}
- Gunakan kreatifitas untuk membuat sourcecode dari 5 object graphics2D yaitu (line, rect, oval, roundrect, arc). Dan gambarkan outputnya di kertas milimeter blok.
- Gambarkan output dan sebutkan ada berapa object Graphisc dari sourcecode di bawah ini dengan menggunakan kertas milimeter blok :
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Shapes2D extends JPanel
{ Point2D pt1, pt2;
Line2D line, line2;
Rectangle2D rect =
new Rectangle2D.Double();
RoundRectangle2D
roundr;
Ellipse2D oval,
oval2;
Arc2D arc, arc2;
QuadCurve2D qc =
new QuadCurve2D.Double();
public void
paintComponent(Graphics g)
{ Graphics2D g2 = (Graphics2D) g;
pt1 = new
Point2D.Double(33.133, 50);
pt2 = new
Point2D.Double(75, 39.12);
line = new
Line2D.Double(pt1, pt2);
g2.draw(line);
line2 = new
Line2D.Double( 33.133, 70, 75, 59.12);
g2.draw(line2);
//g.drawRect(x,
y, lebar, tinggi);
rect.setFrame(85, 50, 100.10, 75);
g2.fill(rect);
arc = new
Arc2D.Double(33.133, 170, 100, 100, 45, 90, 2);
g2.draw(arc);
arc2 = new
Arc2D.Double(233.133, 170, 100, 100, 45, 90, 2);
g2.setPaint(Color.green);
g2.fill(arc2);
}
public static void
main(String[] aarrgg)
{ JFrame f = new JFrame();
f.setSize(500,500);
f.getContentPane().add(new Shapes2D());
f.setVisible(true);
}
}
2 Comments
gambar outputnya ga bisa dibuka
ReplyDeletecoding aja mbak, pasti bisa tampil
Delete