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]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s