package metodedda;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
class garis implements ActionListener{
//frame utama
JFrame f= new JFrame("Garis DDA");
//panel
Kanvas k=new Kanvas();
//label
JLabel lx1=new JLabel("x1");
JLabel ly1=new JLabel("y1");
JLabel lx2=new JLabel("x2");
JLabel ly2=new JLabel("y2");
//form inputan
JTextField tx1=new JTextField("0");
JTextField ty1=new JTextField("0");
JTextField tx2=new JTextField("0");
JTextField ty2=new JTextField("0");
//buton
JButton dda=new JButton("DDA");
int x1,x2,y1,y2,tmp,dy=0,dx=0,p,r,x,y,tmp1,pusatx,pusaty;
float a,b,xinc,yinc,dy1,dx1,step;
public garis(){
f.setLocation(500,300);
f.setSize(500,455);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.add(k);
k.setBounds(0,0,500,360);
pusatx=k.getWidth()-(k.getWidth()/2);
pusaty=k.getHeight()-(k.getHeight()/2);
//Label
f.add(lx1); lx1.setBounds(30,365,30,20);
f.add(ly1); ly1.setBounds(80,365,30,20);
f.add(lx2); lx2.setBounds(130,365,30,20);
f.add(ly2); ly2.setBounds(180,365,30,20);
//Textfield
f.add(tx1); tx1.setBounds(20,390,40,20);
f.add(ty1); ty1.setBounds(70,390,40,20);
f.add(tx2); tx2.setBounds(120,390,40,20);
f.add(ty2); ty2.setBounds(170,390,40,20);
//Button
f.add(dda); dda.setBounds(220,370,150,40);
dda.addActionListener(this);
tx1.setHorizontalAlignment(tx1.CENTER);
ty1.setHorizontalAlignment(ty1.CENTER);
tx2.setHorizontalAlignment(tx2.CENTER);
ty2.setHorizontalAlignment(ty2.CENTER);
f.show();
}
public void actionPerformed(ActionEvent e){
Object objek=e.getSource();
if(objek==dda){
k.repaint();
}
}
private class Kanvas extends Canvas{
Kanvas(){
setBackground(Color.BLACK);
}
public void paint(Graphics g){
g.setColor(Color.YELLOW);
g.drawLine(0,pusaty,k.getWidth(),pusaty);
g.drawLine(pusatx,0,pusatx,k.getWidth());
garisDDA(g);
}
}
public void garisDDA(Graphics g)
{
//input
x1=pusatx+Integer.parseInt(tx1.getText());
y1=pusaty-Integer.parseInt(ty1.getText());
//default
//x1= 50;
//y1= 50;
x2=pusatx+Integer.parseInt(tx2.getText());
y2=pusaty-Integer.parseInt(ty2.getText());
if(x1>x2)
{
tmp=x1;
x1=x2;
x2=tmp;
tmp=y1;
y1=y2;
y2=tmp;
}
a=(float)(x1);
b=(float)(y1);
dx=(x2-x1);
dy=(y2-y1);
if(dx>dy)
step=dx;
else
step=dy;
xinc=dx/step;
yinc=dy/step;
do
{
x=Math.round(a);
y=Math.round(b);
g.fillRect(x, y, 1, 1);
a=a+xinc;
b=b+yinc;
}
while(x!=x2&&y!=y2);
}
}
public class garis_DDA {
public static void main (String[] args) {
new garis();
}
}
B. Brassenham
package metodedda;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import java.io.*;
import java.lang.*;
class garisB implements ActionListener {
JFrame f = new JFrame("Garis Bresenham");
Kanvas k = new Kanvas();
JLabel lx1 = new JLabel("x1");
JLabel ly1 = new JLabel("y1");
JLabel lx2 = new JLabel("x2");
JLabel ly2 = new JLabel("y2");
JTextField tx1 = new JTextField("0");
JTextField ty1 = new JTextField("0");
JTextField tx2 = new JTextField("0");
JTextField ty2 = new JTextField("0");
JButton bres = new JButton("BRESENHAM");
int x1, x2, y1, y2, p, pusatx, pusaty, tmp, dx, dy;
public garisB() {
f.setLocation(300, 100);
f.setSize(500, 455);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.add(k);
k.setBounds(0, 0, 500, 360);
pusatx = k.getWidth() - (k.getWidth() / 2);
pusaty = k.getHeight() - (k.getHeight() / 2);
//Label
f.add(lx1);
lx1.setBounds(30, 365, 30, 20);
f.add(ly1);
ly1.setBounds(80, 365, 30, 20);
f.add(lx2);
lx2.setBounds(130, 365, 30, 20);
f.add(ly2);
ly2.setBounds(180, 365, 30, 20);
//Textfield
f.add(tx1);
tx1.setBounds(20, 390, 40, 20);
f.add(ty1);
ty1.setBounds(70, 390, 40, 20);
f.add(tx2);
tx2.setBounds(120, 390, 40, 20);
f.add(ty2);
ty2.setBounds(170, 390, 40, 20);
//Button
f.add(bres);
bres.setBounds(220, 370, 150, 40);
bres.addActionListener(this);
tx1.setHorizontalAlignment(tx1.CENTER);
ty1.setHorizontalAlignment(ty1.CENTER);
tx2.setHorizontalAlignment(tx2.CENTER);
ty2.setHorizontalAlignment(ty2.CENTER);
f.show();
}
public void actionPerformed(ActionEvent e) {
Object objek = e.getSource();
if (objek == bres) {
k.repaint();
}
}
class Kanvas extends Canvas {
Kanvas() {
setBackground(Color.BLACK);
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.drawLine(0, pusaty, k.getWidth(), pusaty);
g.drawLine(pusatx, 0, pusatx, k.getWidth());
bresenham(g);
}
}
public void bresenham(Graphics g) {
//INPUT
x1 = pusatx + Integer.parseInt(tx1.getText());
y1 = pusaty - Integer.parseInt(ty1.getText());
//default
//x1 = 50;
//y1 = 50;
x2 = pusatx + Integer.parseInt(tx2.getText());
y2 = pusaty - Integer.parseInt(ty2.getText());
if (x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
dx = x2 - x1;
dy = y2 - y1;
p = (2 * dy) - dx;
if (p != 0) {
do {
if ((y2 < y1 && x2 < x1) || (x2 > x1 && y2 > y1)) {
if (p < 0) {
if (dx > dy) {
x1 = x1 + 1;
p = p + (2 * dy);
} else {
y1 = y1 + 1;
p = p + (2 * dx);
}
} else {
x1 = x1 + 1;
y1 = y1 + 1;
if (dx > dy) {
p = p + ((2 * dy) - (2 * dx));
} else {
p = p + (2 * dx - 2 * dy);
}
}
} else {
if (p > 0) {
if (dx < dy) {
x1 = x1 + 1;
p = p - (2 * dy);
} else {
y1 = y1 - 1;
p = p - (2 * dx);
}
} else {
x1 = x1 + 1;
y1 = y1 - 1;
if (dx < (dy * (-1))) {
p = p - ((2 * dy) + (2 * dx)); //bener
} else {
p = p - ((2 * dx) + (2 * dy * (-1)));
}
}
}
g.fillRect(x1, y1, 1, 1);
} while ((x1 != x2) && (y1 != y2));
}
}
}
public class Brassenham {
public static void main(String[] args) {
new garisB();
}
}
C. Lingkaran
a. method 1
package metodedda;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import static java.lang.Math.sqrt;
import javax.swing.JPanel;
public class Lingkaran extends JPanel {
BufferedImage gambar;
int x, y, r;
public Lingkaran(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
CirclePolar(x, y, r);
}
public void CirclePolar(int xc, int yc, int r){
gambar = new BufferedImage(300, 300, BufferedImage.TYPE_INT_ARGB);
double x, y;
for(double i = 1.0/r; i <= 2 * Math.PI; i += 1.0/r){
y = yc + r * Math.sin(i);
x = xc + r * Math.cos(i);
gambar.setRGB((int) x, (int) y, Color.yellow.getRGB());
}repaint();
}
public Dimension getPreferredSize() {
return new Dimension(gambar.getWidth(), gambar.getHeight());
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(gambar, null, null);
}
}
b. main method
package metodedda;
import java.awt.Color;
import javax.swing.JFrame;
public class mainLingkaran {
public static void main(String[] args) {
JFrame frame = new JFrame("Lingkaran");
Lingkaran panel = new Lingkaran(150,150,100);
panel.setBackground(Color.red);
frame.add(panel);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Tidak ada komentar:
Posting Komentar