content bg

MEMBUAT GARIS DDA

Algoritma pembentukan garis DDA :

1. Tentukan dua titik yang akan dihubungkan dalam pembentukan garis.
2. Tentukan salah satu titik sebagai awal(x0,y0) dan titik akhir(x1,y1).
3. Hitung dx=x1¬x0, dan dy= y1¬y0.
4. Tentukan langkah, yaitu dengan cara jarak maksimum jumlah penambahan nilai x maupun nilai y, dengan caara :
Bila nilai absolut dari dx lebih besar dari absolut dy, maka langkah= absolut dari dx.
Bila tidak maka langkah= absolut dari dy
5. Hitung penambahan koordinat pixel yaitu x_increment=dx/langkah, dan y_increment=dy/langkah.
6. Koordinat selanjutnya (x+x_increment, y+y_increment).
7. Posisi pixel pada layar ditentukan dengan pembulatan nilai koordinat tersebut.
8. Ulangi nomor 6 dan 7 untuk menentukan posisi pixel selanjutnya,sampai x= x1 dan y= y1

Contoh Source Code nya :


package Garis;

import java.awt.Graphics;


public class Garis {
 
    public void Garisku (Graphics g, int x0, int y0, int x1, int y1){
        int dx,dy,steps;
        int x_tambah,y_tambah,x,y;
                           
  dx= x1-x0;
  dy= y1-y0;    
              
  if (Math.abs(dx) > Math.abs(dy)){
   steps = Math.abs(dx);
  }
  else{
   steps = Math.abs(dy);
  }
  x_tambah = dx / steps;
  y_tambah = dy / steps;
                x=x0;
                y=y0;
                      
                g.fillRect(x, y, 1, 1);
  for (int k=10; k< steps ;k++){
   x += x_tambah;
   y += y_tambah;
 
                        g.fillRect(x, y, 1, 1);
  }            
      
    }
  
}

Contoh Source Code untuk memanggil Class Garis yang bagian pertama yang sudah kita buat
Source Code nya :

package garis;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class tampilgaris extends JPanel{
    @Override
    public void paintComponent(Graphics g){
        Nur baru = new Nur();
        g.setColor(Color.RED);
        baru.Garisku(g, 200, 300, 400, 300);
        baru.Garisku(g, 200, 160, 400, 160);
            }
  
    public static void main(String[] args) {
        Nur baru = new Nur();
        JFrame frame = new JFrame("Nur Gambar ");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new tampilgaris());
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
             
    }
}
      

Membuat SEGITIGA.AWT


package penggunaanawt;


public class AWTDemo extends Frame implements ActionListener{
  
package penggunaanawt;
import java.awt.*;
import java.awt.event.*;

public class AwtDemo extends Frame implements ActionListener {

    int x = 100;
    int y = 100;
public static void main(String[] args) {
    Frame frame = new AwtDemo();
    frame.setSize(640, 480);
    frame.setVisible(true);
}
public AwtDemo() {
setTitle("AWT Demo");
// create menu
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    Menu menu = new Menu("File");
    mb.add(menu);
    MenuItem mi = new MenuItem("Exit");
    mi.addActionListener(this);
    menu.add(mi);
// end program when window is closed
    WindowListener l = new WindowAdapter()  {
    public void windowClosing(WindowEvent ev) {
    System.exit(0);
    }
    };
this.addWindowListener(l);
// mouse event handler
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
    x = ev.getX();
    y = ev.getY();
    repaint();
}
};
addMouseListener(mouseListener);
}

public void paint(Graphics g) {

g.drawLine(x, y, 400, 100);
g.setColor(Color.yellow);
g.drawLine(x, y,100 , 300);
g.setColor(Color.yellow);
g.drawLine(x, y+200,400 , 100);
g.setColor(Color.yellow);


}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}

MEMBUAT ORANG.AWT

package Nuraini;



import java.awt.*;

import java.awt.event.*;

public class Nuraini extends Frame implements ActionListener{

    int x = 100;

    int y = 100;

public static void main(String[] args) {

    Frame frame = new Nuraini();

    frame.setSize(640, 480);

    frame.setVisible(true);

}

public Nuraini() {

setTitle("Nuraini");

// create menu

    MenuBar mb = new MenuBar();

    setMenuBar(mb);

    Menu menu = new Menu("File");

    mb.add(menu);

    MenuItem mi = new MenuItem("Exit");

    mi.addActionListener(this);

    menu.add(mi);

// end program when window is closed

    WindowListener l = new WindowAdapter()  {

    public void windowClosing(WindowEvent ev) {

    System.exit(0);

    }

    };

this.addWindowListener(l);

// mouse event handler

MouseListener mouseListener = new MouseAdapter() {

public void mouseClicked(MouseEvent ev) {

    x = ev.getX();

    y = ev.getY();

    repaint();

}

};

addMouseListener(mouseListener);

}



public void paint(Graphics g) {

setBackground (Color.PINK);



        //kepala

g.setColor(Color.RED);

g.drawOval(320,240,  200, 200);

// mata

g.setColor(Color.black);

g.fillOval(360, 310, 50, 20);

g.fillOval(440, 310, 50, 20);



// mulut

g.setColor(Color.black);

g.drawArc(380, 360, 90, 50, 180, 180);

g.drawArc(380, 366, 90, 50, 180, 180);

//alis mata

g.setColor(Color.RED);

g.drawArc(360, 290, 50, 20, -180, -180);

g.drawArc(440, 290, 50, 20, -180, -180);



// bola mata

g.setColor(Color.white);

g.fillOval(379, 321, 10, 10);

g.fillOval(457, 321, 10, 10);

//hidung

g.setColor(Color.GREEN);

g.fillOval(410, 350, 30, 30);

//telinga

g.setColor(Color.YELLOW);

g.fillOval(300, 310, 20, 60);

g.fillOval(520, 310, 20, 60);



}

public void actionPerformed(ActionEvent ev) {

 

String command = ev.getActionCommand();

if ("Exit".equals(command)) {

System.exit(0);

}

}

}


MEMBUAT MOBIL.AWT

 package Nur_mobil;

import java.awt.*;
import java.awt.event.*;
public class Nur_mobil extends Frame implements ActionListener{
    int x = 100;
    int y = 100;
public static void main(String[] args) {
    Frame frame = new Nur_mobil();
    frame.setSize(640, 480);
    frame.setVisible(true);
}
public Nur_mobil() {
setTitle("Nur_mobil");
// create menu
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    Menu menu = new Menu("File");
    mb.add(menu);
    MenuItem mi = new MenuItem("Exit");
    mi.addActionListener(this);
    menu.add(mi);
// end program when window is closed
    WindowListener l = new WindowAdapter()  {
    public void windowClosing(WindowEvent ev) {
    System.exit(0);
    }
    };
this.addWindowListener(l);
// mouse event handler
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
    x = ev.getX();
    y = ev.getY();
    repaint();
}
};
addMouseListener(mouseListener);
}

public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillRect(550, 250, 550, 270); //g.drawRect(x, y, WIDTH, WIDTH)

g.setColor(Color.red);
g.drawRect(299, 320, 250, 200);
g.setColor(Color.red);
g.fillOval(349, 520, 100, 100);
g.setColor(Color.black);
g.fillOval(470, 520, 100, 100);
g.setColor(Color.black);
g.drawOval(349, 560, 200, WIDTH);

g.setColor(Color.black);
g.fillOval(930, 520, 100, 100);

g.setColor(Color.black);
g.fillOval(800, 520, 100, 100);

g.drawOval(800, 560, 200, WIDTH);

g.setColor(Color.white);
g.fillOval(520, 240, 30, 30);

g.setColor(Color.black);
g.fillRect(550, 244, 550, 120);

g.setColor(Color.pink);
g.fillRect(380, 370, 100, 100);

}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}




TUGAS GRAFIKA KOMPUTER

package penggunaanawt;

import java.awt.*;
import java.awt.event.*;
public class AwtDemo extends Frame implements ActionListener{
    int x = 100;
    int y = 100;
public static void main(String[] args) {
    Frame frame = new AwtDemo();
    frame.setSize(640, 480);
    frame.setVisible(true);
}
public AwtDemo() {
setTitle("AWT Demo");
// create menu
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    Menu menu = new Menu("File");
    mb.add(menu);
    MenuItem mi = new MenuItem("Exit");
    mi.addActionListener(this);
    menu.add(mi);
// end program when window is closed
    WindowListener l = new WindowAdapter()  {
    public void windowClosing(WindowEvent ev) {
    System.exit(0);
    }
    };
this.addWindowListener(l);
// mouse event handler
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent ev) {
    x = ev.getX();
    y = ev.getY();
    repaint();
}
};
addMouseListener(mouseListener);
}

public void paint(Graphics g) {

  
g.setColor(Color.pink);
g.fillOval(320,240,  200, 200);
g.setColor(Color.BLACK);
g.drawArc(360, 290, 50, 20, -180, -180);
g.drawArc(440, 290, 50, 20, -180, -180);
g.setColor(Color.black);
g.drawArc(380, 360, 90, 50, 180, 180);
g.fillOval(360, 310, 50, 20);
g.fillOval(440, 310, 50, 20);


}
public void actionPerformed(ActionEvent ev) {
String command = ev.getActionCommand();
if ("Exit".equals(command)) {
System.exit(0);
}
}
}