Rabu, 27 November 2013

MID Grafika komputer

MID GRAFIKA KOMPUTER

PERGESERAN

A  (50,150)
B (150,100)
C (150,200)
Delta x = 150-150-50
                = -50
Delta y = 200-100-50
                = 50
Inc_x = delta x/step
                = -50/50=-1
Inc_y = delta y/step
                = 50/50 = 1
Digeser sumbu x = -50 = delta X
Digeser sumbu y = -75 = delta y
                                -50 = x’-150
                                -75 = y’-50
X’= 100
Y’ = 25
Refleksika :

Int x = -50                                
                                                  Data pergeseran
Int y = -75


Listning;

package rotasi;
import java.awt.*;
import java.awt.event.*;
public class Rotasi extends Frame implements ActionListener{
    int x = 200;
    int y = 200;
public static void main(String[] args) {
    Frame frame = new Rotasi();
    frame.setSize(240, 360);
    frame.setVisible(true);
}
public Rotasi() {
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 = 200;
    y = 200;
    repaint();
}
};
addMouseListener(mouseListener);
}

public void paint(Graphics g) {
int xasalA = 50; int yasalA = 150;
int xasalB = 150; int yasalB = 100;
int xasalC = 150; int yasalC = 200;

int sudut = -40;

g.setColor(Color.blue);
g.drawLine(xasalA,yasalA, xasalB,yasalB);
g.drawLine(xasalB,yasalB,xasalC,yasalC);

long xA = Math.round(x+(xasalA-x)*Math.cos(sudut-40)-(yasalA-y)*Math.sin(sudut-40));
long yA = Math.round(x+(xasalA-x)*Math.sin(sudut-40)-(yasalA-y)*Math.cos(sudut-40));
long xB = Math.round(x+(xasalB-x)*Math.cos(sudut-40)-(yasalB-y)*Math.sin(sudut-40));
long yB = Math.round(x+(xasalB-x)*Math.sin(sudut-40)-(yasalB-y)*Math.cos(sudut-40));
long xC = Math.round(x+(xasalC-x)*Math.cos(sudut-40)-(yasalC-y)*Math.sin(sudut-40));
long yC = Math.round(x+(xasalC-x)*Math.sin(sudut-40)-(yasalC-y)*Math.cos(sudut-40));

int xA1 = (int)xA; int yA1 = (int)yA;
int xB1 = (int)xB; int yB1 = (int)yB;
int xC1 = (int)xC; int yC1 = (int)yC;

g.drawLine(xA1,yA1, xB1,yB1);
g.drawLine(xB1,yB1, xC1,yC1);

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



Output ;