ok..
sekarang akan mempelajari bagaimana cara membuat segitiga kapersky..
haa??
kapersky??
memang segitiga antivirus ya??
hahaha..
ya bukanlah..
maksud saya segitiga sibiernsky..
lohh..
sibeirnsky kan pemain sepak bola..
ya weslah..
ya penting bukan segitiga pascal..
ok..
untuk membuatnya melalui java..
saya akan berikan codenya..
pertama buat class dlu yang berisikan tentang ini..
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.awt.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author humbeda
*/
public class Sierpinski extends JPanel{
public static final int LEBAR_TAMPILAN = 750;
static Polygon[] polyList;
static int count = 0;
public static void drawTriangles(int splits, int ax, int ay, int bx, int by, int cx, int cy)
{
int[] p1 = {ax,ay};
int[] p2 = {bx,by};
int[] p3 = {cx,cy};
if(splits == 1){
int[]xs={ax,bx,cx};
int[]ys={ay,by,cy};
Polygon p = new Polygon(xs,ys,xs.length);
polyList[count] = p;
count++;
}
else
{
int[] p4 = getMidpoint(ax, ay, bx, by);
int[] p5 = getMidpoint(bx, by, cx, cy);
int[] p6 = getMidpoint(ax, ay, cx, cy);
drawTriangles(splits - 1,p1[0],p1[1],p4[0],p4[1],p6[0],p6[1]);
drawTriangles(splits - 1,p4[0],p4[1],p2[0],p2[1],p5[0],p5[1]);
drawTriangles(splits - 1,p6[0],p6[1],p5[0],p5[1],p3[0],p3[1]);
}
}
public static int[] getMidpoint(int ax, int ay, int bx, int by)
{
int[] mid = new int[2];
mid[0] = (ax + bx)/2;
mid[1] = (ay + by)/2;
return mid;
}
public void paint(Graphics g)
{
for(int i = 0; i < polyList.length; i++)
{
g.fillPolygon(polyList[i]);
}
}
}
trus bkin lagi kelas yang memanggil code tersebut./*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.awt.Color;
import java.awt.Polygon;
import java.util.Scanner;
import javax.swing.JFrame;
/**
*
* @author humbeda
*/
public class Main extends Sierpinski {
public static void main (String[] args)
{
// make the base triangle based on the window size
//point 1 -- top
int p1x = LEBAR_TAMPILAN/2;
int p1y = LEBAR_TAMPILAN/10;
//point 2 -- bottom right
int p2x = LEBAR_TAMPILAN-p1y;
int p2y = LEBAR_TAMPILAN-p1y;
//point 3 -- bottom left
int p3x = LEBAR_TAMPILAN/10;
int p3y = LEBAR_TAMPILAN-p1y;
// ask user how many splits
Scanner keyboard = new Scanner(System.in);
System.out.println("******************************");
System.out.println("Input Kedalaman Sierpinski");
System.out.println("******************************");
int splits = keyboard.nextInt();
polyList = new Polygon[(int)Math.pow(3,splits-1)];
// new window
JFrame f = new JFrame("Output Segitiga Sierpinski");
f.setSize(LEBAR_TAMPILAN,LEBAR_TAMPILAN);
f.setBackground(Color.white);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setVisible(true);
drawTriangles(splits,p1x,p1y,p2x,p2y,p3x,p3y);
Sierpinski s = new Sierpinski();
f.add(s);
System.out.println("******************************");
}
}
itu dia yang bisa saya berikan..
tapi kalau mau yang 1 kelas juga bisa...
saya kasih codenya deeeh..
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author humbeda
*/
import java.applet.*;
import java.awt.*;
import javax.swing.JOptionPane;
public class sierpinski extends Applet {
int awal = Integer.parseInt(JOptionPane.showInputDialog(null,"Masukkan tingkat segitiga yang diinginkan"));
int tingkat = awal + 1;
Graphics g;
Point a1,b1,c1;
Point a2,b2,c2;
Point a3,b3,c3;
@Override
public void init()
{
setBackground(new Color(255,255,255));
setBackground( null );
}
@Override
public void paint(Graphics segitiga)
{
segitiga.setColor( Color.BLACK );
int titikX[] = {10, 390, 200};
int titikY[] = {390, 390, 10};
segitiga.drawPolygon(titikX, titikY, 3);
gambar(segitiga, new Point(10,390),new Point(390,390),new Point(200,10), getTingkat());
}
public void gambar(Graphics g, Point a, Point b, Point c, int tingkat)
{
if (tingkat==0) return;
tingkat -= 1;
int TitikX[] = {c.x, (c.x+b.x)/2, (a.x+c.x)/2};
int TitikY[] = {b.y, (c.y+a.y)/2, (c.y+a.y)/2};
g.drawPolygon(TitikX, TitikY, 3);
a1 = a;
b1 = new Point(c.x, b.y);
c1 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
gambar(g, a1, b1, c1, tingkat);
a2 = new Point(c.x, b.y);
b2 = b;
c2 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
gambar(g, a2, b2, c2, tingkat);
a3 = new Point((a.x+c.x)/2, (c.y+a.y)/2);
b3 = new Point((c.x+b.x)/2, (c.y+a.y)/2);
c3 = c;
gambar(g, a3, b3, c3, tingkat);
}
/**
* @return the awal
*/
public int getAwal() {
return awal;
}
/**
* @param awal the awal to set
*/
public void setAwal(int awal) {
this.awal = awal;
}
/**
* @return the tingkat
*/
public int getTingkat() {
return tingkat;
}
/**
* @param tingkat the tingkat to set
*/
public void setTingkat(int tingkat) {
this.tingkat = tingkat;
}
}
selamat mencobaaaaaa
No comments:
Post a Comment