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