gb.tampilan pertama, hanya terlihat segitiga dan permintaan masukan nilai kedalaman |
gb. ketika sudah diberikan nilai kedalaman |
gb. pemilihan warna setelah tombol ganti warna ditekan |
gb. segitiga Sierpinski berubah warna |
Kelas Pertama :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fractals;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
*
* @author bapake-aisyah
*/
public class fractals extends JPanel {
private int Level;
private Color color;
private final int WIDHT = 700;
private final int HEIGHT = 600;
public fractals(int currentLevel) {
color = Color.BLUE;
Level = currentLevel;
setBackground(Color.WHITE);
setPreferredSize(new Dimension(WIDHT, HEIGHT));
}
public void setColor(Color c) {
color = c;
}
public void setLevel(int currentLevel) {
Level = currentLevel;
}
public int getLevel() {
return Level;
}
public void gbFractal(Graphics g, int x1, int y1, int x2, int y2, int x3, int y3, int Level) {
if (Level == 0) {
g.drawLine(x1, y1, x2, y2);
g.drawLine(x2, y2, x3, y3);
g.drawLine(x3, y3, x1, y1);
return;
}
int xa, ya, xb, yb, xc, yc;
xa = (x1 + x2) / 2;
ya = (y1 + y2) / 2;
xb = (x1 + x3) / 2;
yb = (y1 + y3) / 2;
xc = (x2 + x3) / 2;
yc = (y2 + y3) / 2;
gbFractal(g, x1, y1, xa, ya, xb, yb, Level - 1);
gbFractal(g, xa, ya, x2, y2, xc, yc, Level - 1);
gbFractal(g, xb, yb, xc, yc, x3, y3, Level - 1);
}
public void paint(Graphics g) {
g.setColor(color);
gbFractal(g, 319, 0, 30, 479, 669, 479, Level);
}
}
Kelas Kedua sebagai pemanggil kelas pertama dengan penciptaan objek turunan kelas pertama :
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fractals;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/**
*
* @author bapake-aisyah
*/
public class Utama extends JFrame {
private final int WIDTH =700;
private final int HEIGHT = 650;
private final int MIN_level = 0;
private Color color = Color.BLUE;
private JButton gantiWarnaJButton, naiklevelJButton, turunLevelJButton;
private JLabel levelJLabel;
private fractals gambar;
private JPanel utamaJPanel, controlJPanel;
public Utama() {
super("Fractal");
controlJPanel = new JPanel();
controlJPanel.setLayout(new FlowLayout());
gantiWarnaJButton = new JButton();
gantiWarnaJButton.setText("ganti warna");
controlJPanel.add(gantiWarnaJButton);
gantiWarnaJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
color = JColorChooser.showDialog(Utama.this, "pilih warna", color);
if (color == null)
color = Color.BLUE;
gambar.setColor(color);
}
});
turunLevelJButton = new JButton();
turunLevelJButton.setText("kurangi level");
controlJPanel.add(turunLevelJButton);
turunLevelJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int level = gambar.getLevel();
level--;
if (level >= MIN_level) {
levelJLabel.setText("level " + level);
gambar.setLevel(level);
repaint();
}
}
});
naiklevelJButton = new JButton();
naiklevelJButton.setText("tambah level");
controlJPanel.add(naiklevelJButton);
naiklevelJButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
int level = gambar.getLevel();
level++;
if (level >= MIN_level) {
levelJLabel.setText("level " + level);
gambar.setLevel(level);
repaint();
}
}
});
levelJLabel = new JLabel("level : 0");
controlJPanel.add(levelJLabel);
gambar = new fractals(0);
utamaJPanel = new JPanel();
utamaJPanel.add(controlJPanel);
utamaJPanel.add(gambar);
add(utamaJPanel);
setSize(WIDTH, HEIGHT);
setVisible(true);
}
public static void main(String[] args) {
Utama utamaBaru = new Utama();
String input = JOptionPane.showInputDialog("level berapa :");
int level = Integer.parseInt(input);
utamaBaru.gambar.setLevel(level);
utamaBaru.gambar.repaint();
utamaBaru.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Nice Post ^_^
BalasHapustrims pak
BalasHapus