ok agan n sista pada blog kali ini saya akan menjelaskan bagaimana membuat bangun primitif2D dengan menggunakan gui bulder yaitu kita akan membuat garis,persegi, segitiga, segiempat, lingkaran dan poligon.
di bawah ini akan ditampilkan gambar beserta sourcecodenya.
adapun Source Codenya adalah sebagai berikut.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JPanel;
public class grafkom2 extends javax.swing.JFrame {
/** Creates new form grafkom2 */
public grafkom2() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTitik = new javax.swing.JButton();
jPersegi = new javax.swing.JButton();
jSegitiga = new javax.swing.JButton();
jGaris = new javax.swing.JButton();
jLingkaran = new javax.swing.JButton();
jPolygon = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTitik.setText("Titik");
jTitik.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTitikActionPerformed(evt);
}
});
jPersegi.setText("Persegi");
jPersegi.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPersegiActionPerformed(evt);
}
});
jSegitiga.setText("Segitiga");
jSegitiga.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jSegitigaActionPerformed(evt);
}
});
jGaris.setText("Garis");
jGaris.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jGarisActionPerformed(evt);
}
});
jLingkaran.setText("Lingkaran");
jLingkaran.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jLingkaranActionPerformed(evt);
}
});
jPolygon.setText("Polygon");
jPolygon.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jPolygonActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 386, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPolygon, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jGaris, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jTitik, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPersegi, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLingkaran, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jSegitiga, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(38, 38, 38))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(20, 20, 20)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jTitik)
.addGap(18, 18, 18)
.addComponent(jPersegi)
.addGap(36, 36, 36)
.addComponent(jSegitiga)
.addGap(39, 39, 39)
.addComponent(jLingkaran)
.addGap(30, 30, 30)
.addComponent(jGaris)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPolygon)))
.addContainerGap(15, Short.MAX_VALUE))
);
getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);
pack();
}// </editor-fold>
// untuk memberikan aksi
private void jLingkaranActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1 = new lingkaran();
jPanel1.repaint();
jScrollPane1.setViewportView(jPanel1);
jPanel1.setAutoscrolls(true);
}
private void jTitikActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1 = new titik();
jPanel1.repaint();
jScrollPane1.setViewportView(jPanel1);
jPanel1.setAutoscrolls(true);
}
private void jPersegiActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1 = new persegi();
jPanel1.repaint();
jScrollPane1.setViewportView(jPanel1);
jPanel1.setAutoscrolls(true);
}
private void jSegitigaActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1 = new segitiga();
jPanel1.repaint();
jScrollPane1.setViewportView(jPanel1);
jPanel1.setAutoscrolls(true);
}
private void jGarisActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1 = new garis();
jPanel1.repaint();
jScrollPane1.setViewportView(jPanel1);
jPanel1.setAutoscrolls(true);
}
private void jPolygonActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jPanel1 = new polygon();
jPanel1.repaint();
jScrollPane1.setViewportView(jPanel1);
jPanel1.setAutoscrolls(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new grafkom2().setVisible(true);
}
});
}
private javax.swing.JButton jGaris;
private javax.swing.JButton jLingkaran;
private javax.swing.JPanel jPanel1;
private javax.swing.JButton jPersegi;
private javax.swing.JButton jPolygon;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton jSegitiga;
private javax.swing.JButton jTitik;
// End of variables declaration
class titik extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
Shape titik2 = new Ellipse2D.Double(100, 120, 2, 2);
g2.setColor(Color.RED);
g2.draw(titik2);
}
}
class persegi extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
Shape kotak = new Rectangle2D.Double(50, 60, 150, 150);
g2.setColor(Color.darkGray);
g2.fill(kotak);
g2.draw(kotak);
}
}
class segitiga extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
GeneralPath segi3 = new GeneralPath();
segi3.moveTo(50, 80);
segi3.lineTo(50, 160);
segi3.lineTo(160, 160);
segi3.lineTo(50, 80);
segi3.closePath();
g2.setColor(Color.WHITE);
g2.draw(segi3);
g2.fill(segi3);
}
}
class lingkaran extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
//koordinat sebelumnya Shape lingkaran2 = new Ellipse2D.Double(5, 50, 80, 80);
//koornat tugas
Shape lingkaran2 = new Ellipse2D.Double(300, 207, 80, 80);
g2.setColor(Color.LIGHT_GRAY);
g2.draw(lingkaran2);
g2.fill(lingkaran2);
}
}
class garis extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
Shape garis2 = new Line2D.Double(30, 80, 210, 210);
g2.setColor(Color.green);
g2.draw(garis2);
}
}
class polygon extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
GeneralPath segi6 = new GeneralPath();
segi6.moveTo(30, 130);
segi6.lineTo(100, 30);
segi6.lineTo(300, 30);
segi6.lineTo(370, 130);
segi6.lineTo(300, 230);
segi6.lineTo(100, 230);
segi6.lineTo(30, 130);
segi6.closePath();
g2.setColor(Color.GRAY);
g2.draw(segi6);
g2.fill(segi6);
}
}
}
Tidak ada komentar:
Posting Komentar