#2 sleepy
radsej pridavam cely Applet, zrejme bude chyba niekde inde.
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class KU3Applet extends Applet {
public static final int WIDTH = 600;
public static final int HEIGHT = 300;
public static final int XMIN = WIDTH /3;
public static final int XMAX = WIDTH/3 *2;
public static final int YMIN = HEIGHT /3;
public static final int YMAX = HEIGHT/3*2;
int pocetbodov = 0;
int Ax,Ay,Bx,By;
//vykresli mriezky
public void paint(Graphics g){
g.setColor(Color.black);
g.drawLine(XMIN, 0, XMIN, HEIGHT);
g.drawLine(XMAX, 0, XMAX, HEIGHT);
g.drawLine(0, YMIN, WIDTH, YMIN);
g.drawLine(0, YMAX, WIDTH, YMAX);
addMouseListener(new MouseAdapter()
{public void mouseClicked(MouseEvent e) {
//TODO Auto-generated method stub
pocetbodov++;
if(pocetbodov==1){
Ax = e.getX();
Ay = e.getY();
System.out.print(Ax+" ");
}
if(pocetbodov==2){
Bx = e.getX();
By = e.getY();
pocetbodov=0;
//line(Ax,Ay,Bx,By);
cohen(Ax,Ay,Bx,By);
}
}
});
}
//vykrsli line
public void line(int Ax,int Ay,int Bx,int By){
Graphics g = getGraphics();
g.drawLine(Ax, Ay, Bx, By);
}
//priradi kod
public int code(int x,int y){
int kod =0;
if (x<XMIN) kod++;
else if(x>XMAX) kod |=2;
if (y < YMIN) kod |=4;
else if(y > YMAX) kod |=8;
return kod;
}
public void cohen(int Ax,int Ay,int Bx,int By){
boolean visible = true;
int Akod = code(Ax,Ay);
int Bkod = code(Bx,By);
int codePom;
int x =0,y=0;
while(true){
//trivialne riesenia
if ((Akod | Bkod) == 0){ visible = true;
System.out.println("je v nutri"); //preskocit orezavanie - cela vnutry
break;
}
else if((Akod & Bkod) != 0) visible = false; //preskocit orezavanie - cela mimo
if(Akod == 0) codePom = Bkod;
else codePom = Akod;
if((codePom & 8 ) == 8){
x = Ax + (Ay - Ax) * (YMIN - Bx) / (By - Bx);
y = YMIN;
}
if((codePom & 4 ) == 4){
x = Ax + (Ay - Ax) * (YMAX - Bx) / (By - Bx);
y = YMAX;
}
if((codePom & 1 ) == 1){
y = Bx + (By - Bx) * (XMIN - Ax) / (Ay - Ax);
x = XMIN;
}
if((codePom & 2 ) == 2){
y = Bx + (By - Bx) * (XMAX - Ax) / (Ay - Ax);
x = XMAX;
}
Ay = x;
By = y;
Bkod = code(Ay, By);
if ((Bkod & Akod ) ==0){
line(Ay, By, this.Ay, this.By);
}
}
if(visible)line(Ax,Ay,Bx,By);
}
@SuppressWarnings("deprecation")
public void main (String[]args){
Applet applet = new KU3Applet();
Frame frame = new Frame();
frame.addWindowFocusListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
frame.add(applet);
frame.setSize(WIDTH, HEIGHT);
frame.show();
}
Ked stlacim mys, tak to reaguje ako by som ho stlacil 10x na jednom mieste. Dikes za pomoc:)