Assigment 7 : Make 3D Object with Java Programming

Java 3D is a scene graph based 3D application programming interface (API) for the Java platform. It runs atop either OpenGL or Direct3D. Since version 1.2, Java 3D has been developed under the Java Community Process. A Java 3D scene graph is a directed acyclic graph (DAG).
Compared to other solutions, Java 3D is not only a wrapper around these graphics APIs, but an interface that encapsulates the graphics programming using a true object-oriented approach. Here a scene is constructed using a scene graph that is a representation of the objects that have to be shown. This scene graph is structured as a tree containing several elements that are necessary to display the objects. Additionally, Java 3D offers extensive spatialized sound support.

in this section , i show you about my computer graphic project., this model about Watercraft made with java, and you can download or view here :

[spoiler title=”lukisan 3d kapal”]

package tugas7;

import java.applet.Applet;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.geometry.Box;

import com.sun.j3d.utils.geometry.Cone;
import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.NormalGenerator;
import com.sun.j3d.utils.geometry.Triangulator;
import com.sun.j3d.utils.universe.SimpleUniverse;
import java.awt.Frame;
import javax.media.j3d.GeometryArray;
import javax.media.j3d.Shape3D;
import javax.vecmath.Point3f;
import javax.vecmath.Point4f;

/**
*
* @author Haris
*/
public class lukisan_kapal_3d extends Applet {

// Deklarasi Nama Animasi
TransformGroup pusat;
TransformGroup t_dek;
TransformGroup meriam;
TransformGroup cerobong;
TransformGroup lambungdepan;
TransformGroup matahari;
Transform3D Trans = new Transform3D();
Vector3f Vector = new Vector3f();
Cylinder Silinder;
Sphere lingkaran1;
Sphere lingkaran2;
Cone lambd;
Box lambung, dek, cb1, cb2, cb3;

Transform3D transform(float x, float y, float z) {
Vector.set(x, y, z);
Trans.set(Vector);
return Trans;
}

Appearance apr() {
// Membuat Warna
Color3f Red = new Color3f(0.0f, 0.9f, 0.0f);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(0.0f, 0.0f, 0.0f);
Material material = new Material(Red, black, Red, white, 64);
Appearance appearance = new Appearance();
appearance.setMaterial(material);
// appearance.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_BACK,0.0f));
return appearance;
}

Appearance apr(float x, float y, float z) {
// Membuat Warna
Color3f Red = new Color3f(x, y, z);
Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
Color3f white = new Color3f(0.0f, 0.0f, 0.0f);
Material material = new Material(Red, black, Red, white, 64);
Appearance appearance = new Appearance();
appearance.setMaterial(material);
// appearance.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_BACK,0.0f));
return appearance;
}

// Inti Program
void GambarOrang() {
pusat = new TransformGroup();
pusat.setTransform(transform(0.0f, 0f, 0.0f));

lambung();
dek_depan();
dek_belakang();
cerobong(-0.5f, 0.5f, 0f);
cerobong(-0.3f, 0.5f, 0f);
cerobong(-0.1f, 0.5f, 0f);

meriam(0.25f, 0.35f, 0.20f);
meriam(0.25f, 0.35f, -0.20f);
meriam(0.0f, 0.35f, 0.20f);
meriam(0.0f, 0.35f, -0.20f);
meriam(0.5f, 0.35f, 0.20f);
meriam(0.5f, 0.35f, -0.20f);
meriam(-0.25f, 0.35f, 0.20f);
meriam(-0.25f, 0.35f, -0.20f);
meriam(-0.5f, 0.35f, 0.20f);
meriam(-0.5f, 0.35f, -0.20f);

t_dek = new TransformGroup();
t_dek.setTransform(transform(0.0f, 0.35f, 0.0f));
dek = new Box(0.8f, 0.15f, 0.3f, apr());
t_dek.addChild(dek);
pusat.addChild(t_dek);

// Membuat Badan
matahari = new TransformGroup();
matahari.setTransform(transform(2f, 2.900f, 0.0f));
lingkaran1 = new Sphere(0.70f, 1, 500, apr());
matahari.addChild(lingkaran1);
pusat.addChild(matahari);

}

public void komponenbox(TransformGroup pusat, float x, float y, float z, Box box) {
TransformGroup a = new TransformGroup();
a.setTransform(transform(x, y, z));
a.addChild(box);
pusat.addChild(a);

}

public void meriam(float x, float y, float z) {
meriam = new TransformGroup();
meriam.setTransform(transform(x, y, z));
lingkaran2 = new Sphere(0.15f, 1, 500, apr(1.2f, 0f, 0f));
meriam.addChild(lingkaran2);
pusat.addChild(meriam);
}

public void cerobong(float x, float y, float z) {
cerobong = new TransformGroup();
cerobong.setTransform(transform(x, y, z));
Silinder = new Cylinder(0.1f, 0.9f, 1, 500, 1, apr());
cerobong.addChild(Silinder);
pusat.addChild(cerobong);
}

public void lambung() {
TransformGroup xccc1 = new TransformGroup();
Vector.set(0.0f, 1.7f, 0.7f);
Trans.set(Vector);
xccc1.setTransform(Trans);
Point3f[] lambunge = {
new Point3f(0.9f, -1.5f, -0.2f),
new Point3f(-0.9f, -1.5f, -0.2f),
new Point3f(-0.9f, -2f, -0.7f),
new Point3f(0.9f, -2f, -0.7f),
new Point3f(0.9f, -1.5f, -1.2f),
new Point3f(-0.9f, -1.5f, -1.2f)
};
//The four faces (triangles) of the tetrahedron.
int titik_lambung[] = {
0, 1, 2, 3,
2, 5, 4, 3,
1, 0, 4, 5
};
GeometryInfo gii = new GeometryInfo(GeometryInfo.QUAD_ARRAY);
gii.setCoordinates(lambunge);
gii.setCoordinateIndices(titik_lambung);
NormalGenerator ngr = new NormalGenerator();
ngr.generateNormals(gii);
GeometryArray ga = gii.getGeometryArray();

//Generate the tetrahedron as a Shape.
Shape3D lambung_kapal = new Shape3D(ga, apr(0.9f, 0.9f, 0.0f));
xccc1.addChild(lambung_kapal);
pusat.addChild(xccc1);
}

public void dek_depan( ) {
TransformGroup xccc = new TransformGroup();

xccc.setTransform(transform(0.0f, 1.7f, 0.7f));

Point3f[] tetrahedronCoordinates = {
new Point3f(0.9f, -1.5f, -0.2f),
new Point3f(2f, -1.5f, -0.7f),
new Point3f(0.9f, -1.5f, -1.2f),
new Point3f(0.9f, -2f, -0.7f)
};
//The four faces (triangles) of the tetrahedron.
int coordIndices[] = {
3, 1, 0,
0, 2, 3,
0, 1, 2,
1, 3, 2
};
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
gi.setCoordinates(tetrahedronCoordinates);
gi.setCoordinateIndices(coordIndices);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
GeometryArray te = gi.getGeometryArray();

//Generate the tetrahedron as a Shape.
Shape3D tetrahedron = new Shape3D(te, apr(0.5f, 0.2f, 0.2f));
xccc.addChild(tetrahedron);

pusat.addChild(xccc);
}
public void dek_belakang( ) {
TransformGroup xccc = new TransformGroup();

xccc.setTransform(transform(0.0f, 1.7f, 0.7f));

Point3f[] tetrahedronCoordinates = {
new Point3f(-0.9f, -1.5f, -0.2f),
new Point3f(-1.5f, -1.5f, -0.7f),
new Point3f(-0.9f, -1.5f, -1.2f),
new Point3f(-0.9f, -2f, -0.7f)
};
//The four faces (triangles) of the tetrahedron.
int coordIndices[] = {
0,1,3,
3,2,0,
2,1,0,
2,3,1
};
GeometryInfo gi = new GeometryInfo(GeometryInfo.TRIANGLE_ARRAY);
gi.setCoordinates(tetrahedronCoordinates);
gi.setCoordinateIndices(coordIndices);
NormalGenerator ng = new NormalGenerator();
ng.generateNormals(gi);
GeometryArray te = gi.getGeometryArray();

//Generate the tetrahedron as a Shape.
Shape3D tetrahedron = new Shape3D(te, apr(0.2f, 0.3f, 0.2f));
xccc.addChild(tetrahedron);

pusat.addChild(xccc);
}

BranchGroup createSceneGraph() {
// Membuat Putaran
BranchGroup objRoot = new BranchGroup();

// Membuat TransformGroup
TransformGroup objScale = new TransformGroup();
Transform3D scaleTrans = new Transform3D();
scaleTrans.set(1 / 3.5f);
objScale.setTransform(scaleTrans);
objRoot.addChild(objScale);

// Membuat TransformGroup dan initialize
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(objTrans);

// Menambahkan Method Gambar Orang
GambarOrang();
objTrans.addChild(pusat);
BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);

Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f));
bg.setApplicationBounds(bounds);
objTrans.addChild(bg);

// Mengatur Mouse
MouseRotate mr = new MouseRotate();
mr.setTransformGroup(objTrans);
mr.setSchedulingBounds(bounds);
mr.setFactor(0.007);
objTrans.addChild(mr);

// Mengatur Terang
Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
Vector3f light1Direction = new Vector3f(0.0f, -0.2f, -1.0f);

DirectionalLight light1 = new DirectionalLight(light1Color,
light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);

return objRoot;
}

public lukisan_kapal_3d() {
setLayout(new GridLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("center", canvas3D);
BranchGroup scene = createSceneGraph();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public static void main(String[] args) {
Frame frame = new MainFrame(new lukisan_kapal_3d(), 1200, 600);
frame.setLocationRelativeTo(null);
}
}

[/spoiler]

Advertisements

Tugas 6 : Membuat Lukisan kapal 2D java

Di dalam bahasa pemrograman java kita bisa membuat grafik baik 2d maupun yang 3d.,
contohnya gambar di bawah ini :

untuk Source codenya disimpan dalam file  lukisan_kapal.java :


package tugas6;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* @author Haris
*/
public class lukisan_kapal extends JApplet {

public static void main(String s[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new lukisan_kapal();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}

public void init() {
JPanel panel = new Pan();
getContentPane().add(panel);
}

class Pan extends JPanel {

public Pan() {
setPreferredSize(new Dimension(1300, 600));
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g1 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;
// nama dan nim
kotak(g2);
GradientPaint paint1 = new GradientPaint(100, 100, new Color(130, 36, 2), 250, 100, new Color(253, 149, 51));
Font font = new Font("Comic Sans ms", Font.BOLD, 25);
g1.setPaint(paint1);
g1.setFont(font);
g1.drawString("Haris Budi Erwanto", 50, 500);
g1.drawString("( 10-65-0006 )", 65, 550);

garisdda(g2, 0, 350, 325, 350);
garisdda(g2, 625, 350, 1300, 350);

garisdda(g2, 350, 400, 600, 400);
garisdda(g2, 600, 400, 650, 300);
garisdda(g2, 300, 300, 350, 400);
garisdda(g2, 300, 300, 650, 300);

kotak_brass(g2, 350, 250, 600, 300);
bunder(g2, 1150, 100, 75);
body(g2);
garisdda(g2, 950, 100, 1050, 100);
garisdda(g2, 960, 25, 1060, 50);
garisdda(g2, 960, 175, 1060, 150);
garisdda(g2, 980, 250, 1080, 175);
budermidpoint(g2, 375, 275, 20);
budermidpoint(g2, 430, 275, 20);
budermidpoint(g2, 485, 275, 20);
budermidpoint(g2, 540, 275, 20);
awane(g2, -10, 50);
awane(g2, 90, 1);
awane(g2, 210, 50);
awane(g2, 310, 1);
awane(g2, 410, 50);
awane(g2, 510, 1);
awane(g2, 610, 50);
awane(g2, 710, 1);
}

public void body(Graphics2D g) {
GeneralPath gp = new GeneralPath();
GradientPaint p = new GradientPaint(0, 350, Color.gray, 50, 30, Color.black);
g.setPaint(p);

gp.moveTo(300, 300);
gp.lineTo(650, 300);
gp.lineTo(600, 400);
gp.lineTo(350, 400);
gp.closePath();
g.fill(gp);
}

public void kotak_brass(Graphics2D gr, int x_awal, int y_awal, int x_akhir, int y_akhir) {
GeneralPath gp = new GeneralPath();
Shape s = new Rectangle2D.Double(x_awal, y_awal, 250, 50);
GradientPaint p = new GradientPaint(0, 350, Color.green, 50, 30, Color.black);
gr.setPaint(p);
gr.fill(s);
int Dx = Math.abs(x_akhir - x_awal);
int Dy = Math.abs(y_akhir - y_awal);
int parameter = (2 * Dy) - Dx;
String status = "Kotak_BM";
this.repaint();
double g = x_awal, h = y_awal;
while (x_awal < x_akhir) {
if (parameter < 0) {
parameter += (2 * Dy);
} else {
y_awal++;
parameter = parameter + (2 * Dy) - (2 * Dx);
}
gp.moveTo(x_awal, y_awal);
x_awal++;
}
gp.lineTo(g, y_awal);
gp.lineTo(g, h);
gp.lineTo(x_awal, h);
gp.lineTo(x_awal, y_awal);
gp.closePath();
gr.draw(gp);}

public void garisdda(Graphics2D g, int x1, int y1, int x2, int y2) {

double m = (double) (y2 - y1) / (x2 - x1);
double y = (double) y1;
int iy;
for (int x = x1; x <= x2; x++) {
iy = (int) Math.round(y);
g.drawLine(x, iy, x, iy);
y += m;
}
}
}

public void kotak(Graphics2D g) {
Shape s = new Rectangle2D.Double(0, 0, 1300, 350);
GradientPaint p = new GradientPaint(0, 350, new Color(0, 255, 255), 50, 30, new Color(70, 51, 204));
g.setPaint(p);
g.fill(s);
Shape s1 = new Rectangle2D.Double(0, 350, 1300, 250);
GradientPaint r = new GradientPaint(0, 350, Color.white, 1300, 600, Color.blue);
g.setPaint(r);
g.fill(s1);
}

void budermidpoint(Graphics2D g, int x1, int y1, int r) {
Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);
g.setColor(Color.red);
g.fill(s);
int x = 0, y = r, p = 1 - r;
int xctr = x1, yctr = y1;
g.drawLine(xctr + x, yctr + y, xctr + x, yctr + y);
g.drawLine(xctr - x, yctr + y, xctr - x, yctr + y);
g.drawLine(xctr + x, yctr - y, xctr + x, yctr - y);
g.drawLine(xctr - x, yctr - y, xctr - x, yctr - y);
g.drawLine(xctr + y, yctr + x, xctr + y, yctr + x);
g.drawLine(xctr - y, yctr + x, xctr - y, yctr + x);
g.drawLine(xctr + y, yctr - x, xctr + y, yctr - x);
g.drawLine(xctr - y, yctr - x, xctr - y, yctr - x);
while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}

public void bunder(Graphics2D g, int x1, int y1, int r) {
Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);
GradientPaint p = new GradientPaint(x1, y1, Color.orange, x1 + 200, y1 + 50, Color.red);
g.setPaint(p);
g.fill(s);
}

public void awane(Graphics2D g2, int x, int y) {
GeneralPath gp = new GeneralPath();
gp.moveTo(30 + x, 80 + y);
gp.curveTo(-5 + x, 60 + y, 22 + x, 15 + y, 40 + x, 42 + y);
gp.curveTo(37 + x, 31 + y, 53 + x, 31 + y, 58 + x, 43 + y);
gp.curveTo(60 + x, 7 + y, 116 + x, 4 + y, 100 + x, 34 + y);
gp.curveTo(150 + x, 29 + y, 150 + x, 89 + y, 120 + x, 90 + y);
gp.curveTo(100 + x, 120 + y, 95 + x, 97 + y, 87 + x, 100 + y);
gp.curveTo(71 + x, 120 + y, 35 + x, 100 + y, 35 + x, 70 + y);
gp.closePath();
gp.closePath();
g2.setPaint(new GradientPaint(0 + x, 20 + y, Color.lightGray, 0 + x, 150 + y, Color.white));
g2.fill(gp);
g2.setColor(Color.white);
g2.draw(gp);
}

}

Tugas 5 : mengenal algoritma dalam membentuk grafik komputer

Dalam pembentukan grafik yang cepat dan efisien maka dibutuhkan algoritma yang tersendiri,seperti pembuatan lingkaran, polygon garis , DLL. dalam grafika komputer ada 3 algoritma yaitu DDA (Digital  Differential  Analyzer), Breseinhem , dan algoritma mid point.

Algoritma  Digital  Differential  Analyzer  (DDA).

Algoritma  Digital  Differential  Analyzer  (DDA)  adalah  algoritma  pembentukan  garis  berdasarkan  perhitungan  dx maupun  dy  dengan menggunakan  rumus  dy  = m.dx.  Garis  dibuat  dengan  menentukan  dua  endpoint  yaitu  titik  awal  dan  titik  akhir.  Setiap  koordinat titik yang membentuk garis diperoleh dari perhitungan kemudian dikonversikan  menjadi nilai integer.

Keuntungan  dari  algoritma  ini  adalah  tidak  perlu  menghitung  koordinat  berdasarkan  persamaan  yang  lengkap  (menggunakan  metode  offset).  Sedangkan  kerugiannya adalah adanya akumulasi Round-off errors,  sehingga garis akan melenceng  dari garis lurus, selain itu operasi round-off juga menghabiskan waktu. Continue reading

Tugas 4 : Membuat Grafik Primitive Dengan Algoritma (DDA,Bresenhem, Midpoint)

never mind

Sebelum kita lebih jauh membahas tentang Grafik primitive , apa sih DDA, Brasenhem dan mid point itu ? adalah suatu metode pembentukan grafik dengan algoritma tertentu ,bisa dilihat lebih lanjut di sini .,

 

implementasi Algoritma DDA dalam Garis :

[spoiler title=”Garis DDA”]

package DDA;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class garis extends JPanel {

    private int x0, y0, x1, y1;

    public garis() {
    }

    public garis(int x0, int y0, int x1, int y1) {
        setX0(x0);
        setY0(y0);
        setX1(x1);
        setY1(y1);
    }

    public void paintLine(int x0, int y0, int x1, int y1) {
        JFrame frame = new JFrame("Garis DDA");
        frame.add(new garis(x0, y0, x1, y1));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
//		g2d.setColor(Color.blue);
        int dx, dy, steps;
        int xInc, yIncrement, x, y;
        dx = x1 - x0;
        dy = y1 - y0;
        if (Math.abs(dx) > Math.abs(dy)) {
            steps = dx;
        } else {
            steps = dy;
        }
        xInc = dx / steps;
        yIncrement = dy / steps;
        x = x0;
        y = y0;
        g2d.drawLine(Math.round(x), Math.round(y), Math.round(x), Math.round(y));
        for (int i = 0; i < steps; i++) {
            x += xInc;
            y += yIncrement;
            g2d.drawLine(Math.round(x), Math.round(y), Math.round(x), Math.round(y));
        }

    }

    public int getX0() {
        return x0;
    }

    public void setX0(int x0) {
        this.x0 = x0;
    }

    public int getY0() {
        return y0;
    }

    public void setY0(int y0) {
        this.y0 = y0;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public static void main(String[] s) {
        int x0 = Integer.parseInt(JOptionPane.showInputDialog("X0"));
        int y0 = Integer.parseInt(JOptionPane.showInputDialog("Y0"));
        int x1 = Integer.parseInt(JOptionPane.showInputDialog("X1"));
        int y1 = Integer.parseInt(JOptionPane.showInputDialog("Y1"));
        garis ln = new garis(x0, y0, x1, y1);
        ln.paintLine(x0, y0, x1, y1);

    }
}

[/spoiler]

Implementasi Algoritma Brasenhem Pada garis :

[spoiler title=”Garis Brasenhem”]

package Brass;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class garis_bras extends JPanel {
    private int x0,y0,x1,y1 ;
//    xa =10;ya=10;xb=200;yb=10;

    public garis_bras() {
//        repaint();
    }

    public garis_bras(int x0, int y0, int x1, int y1) {
        setX0(x0);
        setY0(y0);
        setX1(x1);
        setY1(y1);
    }

    public void paintLine (int x0, int y0, int x1, int y1){
        JFrame frame = new JFrame("Bressenham");
//        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new  garis_bras( x0,y0,x1,y1));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

	/**
	 *
	 */
//	private static final long serialVersionUID = 1L;

    @Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setColor(Color.blue);
		int dx,dy,x,y, xEnd, p;
		dx = Math.abs(getX0() - getX1());
		dy = Math.abs(getY0() - getY1());
		p = 2 * dy - dx;
		if ( getX0() > getX1() ){
			x = getX1();
			y = getY1();
			xEnd = getX0();
		}
		else{
			x = getX0();
			y = getY0();
			xEnd = getX1();
		}

		g2d.drawLine(Math.round(x), Math.round(y),Math.round(x),Math.round(y));
		while ( x < xEnd){
			x += 1 ;

			if (p < 0){
				p += 2 * dy;
			}
			else{
				y +=1;
				p += 2* (dy - dx);
			}

			g2d.drawLine(Math.round(x), Math.round(y),Math.round(x),Math.round(y));
		}

	}

    public int getX0() {
        return x0;
    }

    public void setX0(int x0) {
        this.x0 = x0;
    }

    public int getY0() {
        return y0;
    }

    public void setY0(int y0) {
        this.y0 = y0;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }
    public static void main(String[] s) {

       garis_bras ln = new garis_bras();
        ln.paintLine(100, 100, 200,100);
    }
}

[/spoiler]

Implementasi Midpoint pada Elips dan Lingkaran :

[spoiler title=”Elips Mid point”]

package midpoint;

import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class elips_mid extends JPanel{
    private int Rx, Ry, X1, Y1;

    public elips_mid() {
    }

    public elips_mid(int X1, int Y1, int Rx, int Ry) {
        setRx(Rx);
        setRy(Ry);
        setX1(X1);
        setY1(Y1);

    }

    public void paintEllipse (int X1, int Y1, int Rx, int Ry){
        JFrame frame = new JFrame("Ellipse");
        frame.add(new elips_mid(X1, Y1, Rx, Ry));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    @Override
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        int xCentre = 0, yCentre = 0, p, px, py, x = 0, y = 0, Ry2, Rx2, twoRx2, twoRy2;

        g2d.drawLine(xCentre+x, yCentre+y, xCentre+x, yCentre+y);
        g2d.drawLine(xCentre-x, yCentre+y, xCentre-x, yCentre+y);
        g2d.drawLine(xCentre+x, yCentre-y, xCentre+x, yCentre-y);
        g2d.drawLine(xCentre-x, yCentre-y, xCentre-x, yCentre-y);

        Ry2 = Ry * Ry;
	Rx2 = Rx * Rx;
	twoRy2 = 2 * Ry2;
	twoRx2 = 2 * Rx2;

        x = 0;
	y = Ry;
	p = (int) Math.round(Ry2 - Rx2 * Ry + (0.25 * Rx2));
	px = 0;
	py = twoRx2 * y;

        //radius 1
        while(px= 0){
                y = y - 1;
                py = py - twoRx2;
            }
            g2d.drawLine(x+X1, y+Y1, x+X1, y+Y1);
            g2d.drawLine(x*(-1)+X1, y+Y1, x*(-1)+X1, y+Y1);
            g2d.drawLine(x*(-1)+X1, y*(-1)+Y1, x*(-1)+X1, y*(-1)+Y1);
            g2d.drawLine(x+X1, y*(-1)+Y1, x+X1, y*(-1)+Y1);

            if (p < 0){
               p = p + Ry2 + px;
             }else p = p + Ry2 + px - py;
         }
         //radius 2
         p = (int) Math.round(Ry2*(x + 0.5)*(x + 0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2);
            while (y > 0){
            y = y - 1;
            py = py - twoRx2;

            if (p  0){
                p = p + Rx2 - py;
            }else p = p + Rx2 - py + px;

        }

    }

    public int getRx() {
        return Rx;
    }

    public void setRx(int Rx) {
        this.Rx = Rx;
    }

    public int getRy() {
        return Ry;
    }

    public void setRy(int Ry) {
        this.Ry = Ry;
    }

    public int getX1() {
        return X1;
    }

    public void setX1(int X1) {
        this.X1 = X1;
    }

    public int getY1() {
        return Y1;
    }

    public void setY1(int Y1) {
        this.Y1 = Y1;
    }
    public static void main(String[] s) {
        int radx = Integer.parseInt(JOptionPane.showInputDialog("Radius titik x"));
        int rady = Integer.parseInt(JOptionPane.showInputDialog("Radius titik x"));
        int x = Integer.parseInt(JOptionPane.showInputDialog("X"));
        int y = Integer.parseInt(JOptionPane.showInputDialog("Y"));
        elips_mid ln = new elips_mid(radx, rady, x, y);
        ln.paintEllipse(radx, rady, x, y);

    }
}

[/spoiler]

[spoiler title=”Lingkaran Midpoint”]


package midpoint;

import java.awt.Graphics;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ling_mid extends JPanel{

    private int xCenter;
    private int yCenter;
    private int radius;

    public ling_mid() {
    }

    public ling_mid(int xCenter, int yCenter, int radius) {
        setXCenter(xCenter);
        setYCenter(yCenter);
        setRadius(radius);
    }

    public void paintCircle (int xCenter, int yCenter, int rad){
        JFrame frame = new JFrame("Circle Mid Point ");
        frame.add(new ling_mid(xCenter, yCenter, rad));
        frame.setSize(250, 200);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    @Override
	public void paintComponent(Graphics g){

//		xCenter=50;yCenter=50;radius=40;
		int p,x,y;
		p=x=y=0;

		y = radius;

		g.drawLine( xCenter + x, yCenter + y, xCenter + x, yCenter + y);
		g.drawLine( xCenter - x, yCenter + y, xCenter - x, yCenter + y);
		g.drawLine( xCenter + x, yCenter - y, xCenter + x, yCenter - y);
		g.drawLine( xCenter - x, yCenter - y, xCenter - x, yCenter - y);
		g.drawLine( xCenter + y, yCenter + x, xCenter + y, yCenter + x);
		g.drawLine( xCenter - y, yCenter + x, xCenter - y, yCenter + x);
		g.drawLine( xCenter + y, yCenter - x, xCenter + y, yCenter - x);
		g.drawLine( xCenter - y, yCenter - x, xCenter - y, yCenter - x);

		p = 1 - radius;
		  while (x < y){
			if (p < 0){
			   x = x+1;
			}
			else{
			     x = x+1;
			     y = y-1;
			}

			if (p < 0){
				  p = p+2*x+1;
			}
			else{
			   p = p+2*(x-y)+1;
			}

			g.drawLine(xCenter + x, yCenter + y,xCenter + x, yCenter + y);
			g.drawLine(xCenter - x, yCenter + y,xCenter - x, yCenter + y);
			g.drawLine(xCenter + x, yCenter - y,xCenter + x, yCenter - y);
			g.drawLine(xCenter - x, yCenter - y, xCenter - x, yCenter - y);
			g.drawLine(xCenter + y, yCenter + x, xCenter + y, yCenter + x);
			g.drawLine(xCenter - y, yCenter + x,xCenter - y, yCenter + x);
			g.drawLine(xCenter + y, yCenter - x,xCenter + y, yCenter - x);
			g.drawLine(xCenter - y, yCenter - x,xCenter - y, yCenter - x);
		  }
	}

    public int getXCenter() {
        return xCenter;
    }

    public void setXCenter(int xCenter) {
        this.xCenter = xCenter;
    }

    public int getYCenter() {
        return yCenter;
    }

    public void setYCenter(int yCenter) {
        this.yCenter = yCenter;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }
    public static void main(String[] s) {
        int x = Integer.parseInt(JOptionPane.showInputDialog("X0"));
        int y = Integer.parseInt(JOptionPane.showInputDialog("Y0"));
        int rad = Integer.parseInt(JOptionPane.showInputDialog("Y1"));
        ling_mid ln = new ling_mid(x, y, rad);
        ln.paintCircle(x, y, rad);
    }
}

[/spoiler]

Tugas 3 : Membuat Kurva & Spline Java 2D

 

Penggunaan kurva pada bidang informatika sangatlah dibutuhkan terlebih untuk membuat grafik,baik dalam desain ataupun untuk kepentingan kalkualasi,dan Membuat Kurva di java cukup mudah, kita hanya menggunakan kelas yang ada/ disediakan oleh Java yaitu quadcurve untuk kurva 2 derajat, dan cubiccurve untuk kurva 3 derajat.,dan hasil yang bisa di dapatkan kurang lebih seperti di bawah ini :

  • Untuk melihat kode ,di bawah ada spoiler >> Klik Show
  • Source code bisa di download di sini

Kurva Derajat 1

Kurva derajat 2

Kurva derajat 3

 

Spline

Kurva yang bisa di kontrol

[spoiler title=”Source code Kurva Bisa dilihat disini”]

package grafkom;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.JFrame;

/**
*
* @author Haris
*/
public class tugas3 extends javax.swing.JFrame {

public tugas3() {
initComponents();
}
public void derajat1(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine(100, 100, 250, 200);
}
public void derajat2(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
Shape sh = new QuadCurve2D.Double(100, 200, 150, 100,200 , 200);
g2d.draw(sh);
}
public void derajat3(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
Shape sh = new CubicCurve2D.Double(50,200, 100,100,150, 200, 200, 200);
Shape sh1 = new CubicCurve2D.Double(200,200, 250,200,300, 100, 350, 200);
g2d.draw(sh);
g2d.draw(sh1);
}
public void spline(Graphics g) {
super.paintComponents(g);
Graphics2D g2 = (Graphics2D) g; //Kelas Graphics2D adalah kelas dasar untuk rendering grafis di java 2D. Pendeklarasian ini diperlukan untuk mendapatkan akses kesemua operasi.
GeneralPath gp = new GeneralPath();

gp.moveTo(80, 200);
gp.curveTo(150, 175, 350, 150, 325, 175);
gp.quadTo(200, 175, 300, 125);
gp.curveTo(150, 150, 300, 125, 375, 150);
g2.draw(gp);
}
public void kurva (){
kontrol kn = new kontrol();
JFrame frame = new JFrame(“Curve Control Point”);
frame.getContentPane().add(new Curve2());
frame.setSize(350, 200);
frame.setVisible(true);
}

@SuppressWarnings(“unchecked”)
//
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton7 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“Grafkom : Kurva Dan SPline”);

jButton1.setText(“Derajat 1”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText(“Derajat 3”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText(“Derajat 2”);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton7.setText(“Curve Control”);
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton7ActionPerformed(evt);
}
});

jButton4.setText(“Spline”);
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(347, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton7)
.addContainerGap(74, Short.MAX_VALUE))
);

pack();
}//

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
derajat1(getGraphics());
// TODO add your handling code here:
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
derajat2(getGraphics());// TODO add your handling code here:
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
derajat3(getGraphics());// TODO add your handling code here:
}

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {
kurva();

}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
spline(getGraphics());
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Windows”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(tugas3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(tugas3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(tugas3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(tugas3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new tugas3().setVisible(true);

}
});
}
// Variables declaration – do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton7;
// End of variables declaration
}

[/spoiler]

Tugas 2 : Mewarnai Bentuk Primitive 2D Java

Mewarnai / Memberi warna pada objek primitive 2D bisa menggunakan ****.setColor(Color.blue);  Seperti dibawah ini adalah contohya :

titik

garis

segitiga

 

poligon

lingkaran

kotak

kotak

lihat source code :

[spoiler title=”Source code Kurva Bisa dilihat disini”]

<pre>

/*
* dasar_canvas.java
*
* Created on Feb 27, 2012, 7:54:17 PM
*/
package grafkom;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;

/**
*
* @author haris
*/
public class tugas2 extends javax.swing.JFrame {

public tugas2() {
initComponents();
}

@SuppressWarnings(“unchecked”)
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {

jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jButton5 = new javax.swing.JButton();
jButton6 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle(“Grafkom : Bentuk Primitive dan Mewarnai”);

jButton1.setText(“Titik”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jButton2.setText(“Garis”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText(“Segitiga”);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton4.setText(“Polygon”);
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton4ActionPerformed(evt);
}
});

jButton5.setText(“Kotak”);
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton5ActionPerformed(evt);
}
});

jButton6.setText(“Lingkaran”);
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton6ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(316, 316, 316)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jButton6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton6)
.addContainerGap(22, Short.MAX_VALUE))
);

pack();
}// </editor-fold>
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
poligon(getGraphics());
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
titik(getGraphics());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
garis(getGraphics());
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
segitiga(getGraphics());
}
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
kotak(getGraphics());
}
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {
linkaran(getGraphics());
}
public void segitiga(Graphics g) {
super.paintComponents(g);
Graphics2D se3 = (Graphics2D) g;
int x[] = {50, 150, 250};
int y[] = {250, 50, 250};
se3.setColor(Color.green);
se3.fill(new Polygon(x, y, x.length));
}

public void kotak(Graphics g) {
super.paintComponents(g);
Graphics2D kot = (Graphics2D) g;
int x = 100, y = 100;
int p = 100, l = 100;
kot.setColor(Color.yellow);
kot.fill(new Rectangle(x, y, p, l));
}

public void poligon(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
Polygon pol;
int x[] = {100, 125, 175, 200, 175, 125};
int y[] = {150, 100, 100, 150, 200, 200};
pol = new Polygon(x, y, x.length);
g2d.setPaint(Color.red);
g2d.fill(pol);

}

public void linkaran(Graphics g) {
super.paintComponents(g);
Graphics2D ling = (Graphics2D) g;
Ellipse2D lingkaran;
double x = 100, y = 100;
double diameter = 100;
lingkaran = new Ellipse2D.Double(x, y, diameter, diameter);
ling.setColor(Color.blue);
ling.fill(lingkaran);
}

public void garis(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine(100, 150, 250, 150);
}

public void titik(Graphics g) {
super.paintComponents(g);
Graphics2D t2k = (Graphics2D) g;
t2k.drawLine(150, 150, 150, 150);
}

public void paintComponent(Graphics g) {
clear(g);
segitiga(g);
kotak(g);
poligon(g);
garis(g);
linkaran(g);
titik(g);
}

protected void clear(Graphics g) {
super.paintComponents(g);
}

public static void main(String args[]) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if (“Windows”.equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(tugas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(tugas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(tugas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(tugas2.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
new tugas2().setVisible(true);

}
// Variables declaration – do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JButton jButton5;
private javax.swing.JButton jButton6;
// End of variables declaration
}

</pre>
[/spoiler]

Tugas 1 :Membuat gambar primitif menggunakan java

Gambar di atas adalah salah satu gambar primitive dari Graphic2d java,menggunakan Class Shape  ,untuk membuatnya cukup mudah,buat Class baru  java kemudian tulis seperti di bawah ini:

import java.awt.*;
 import java.awt.geom.Ellipse2D;
 import javax.swing.*;

 public class tugas1 extends JPanel {

public void segitiga(Graphics g) {
 Graphics2D se3 = (Graphics2D) g;
 int x[] = {50, 150, 250};
 int y[] = {250, 50, 250};
 se3.setColor(Color.green);
 se3.fill(new Polygon(x, y, x.length));
 }

public void kotak(Graphics g) {
 Graphics2D kot = (Graphics2D) g;
 int x = 500, y = 100;
 int p = 100, l = 100;
 kot.setColor(Color.yellow);
 kot.fill(new Rectangle(x, y, p, l));
 }

public void poligon(Graphics g) {
 Graphics2D g2d = (Graphics2D) g;
 Polygon pol;
 int x[] = {375, 400, 450, 475, 450, 400};
 int y[] = {150, 100, 100, 150, 200, 200};
 pol = new Polygon(x, y, x.length);
 g2d.setPaint(Color.red);
 g2d.fill(pol);
 }

Continue reading