package T3dawt; import java.awt.*; import T3dobject.T3DObject; import java.awt.event.*; import java.util.*; public class T3dCanvas extends Canvas implements MouseMotionListener, MouseListener, Runnable { Image off; Graphics off_g; int mouse_x, mouse_y; Vector ob; T3ddraw draw; byte lock[] = new byte[0]; volatile MouseEvent mouse_e; volatile Thread th; public T3dCanvas(Vector ob) { setBackground(Color.white); addMouseMotionListener(this); addMouseListener(this); this.ob = ob; draw = new T3ddraw(ob); th = new Thread(this); th.start(); } public void createOff() { off = createImage(getSize().width, getSize().height); off_g = off.getGraphics(); } public void createOff(Image img) { off = img; off_g = off.getGraphics(); } private void rotation(int x, int y) { double ra; double step_ra = Math.PI / 18; if (y == 0) { ra = Math.PI / 2; if (x <= 0) step_ra = -step_ra; } else { ra = -Math.atan((double)x / y); if (y >= 0) step_ra = -step_ra; } //System.out.println(ra*180/Math.PI); for (int i = 0; i < ob.size(); i++) ((T3DObject)ob.elementAt(i)).rotation(ra, step_ra); } public void paint_off() { if (off_g != null) { draw.sort(); off_g.setColor(Color.white); off_g.fillRect(0, 0, getSize().width, getSize().height); //for (int i = 0; i < ob.size(); i++) //((T3DObject)ob.elementAt(i)).paint(off_g); draw.paint(off_g, getSize().width / 2, getSize().height / 2); } } public void paint(Graphics g) { if (off_g != null) { g.drawImage(off, 0, 0, this); } } public void update(Graphics g) { paint(g); } /****** mouse ******/ public void mouseClicked (MouseEvent e) { mouse_x = e.getX(); mouse_y = e.getY(); } public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void mouseDragged(MouseEvent e) { synchronized (lock) { mouse_e = e; lock.notify(); } } public void mouseMoved (MouseEvent e) {} public void run() { Thread thisth = Thread.currentThread(); MouseEvent e; while (th == thisth) { synchronized (lock) { while (mouse_e == null) { try { lock.wait(); } catch (InterruptedException ie) {} } e = mouse_e; mouse_e = null; } rotation(e.getX() - mouse_x, e.getY() - mouse_y); mouse_x = e.getX(); mouse_y = e.getY(); paint_off(); repaint(); } } }