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]

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