import java.awt.Stroke; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.awt.Point; import javax.swing.event.*; import javax.swing.border.*; import java.awt.image.*; import java.awt.geom.*; import static java.lang.Math.*; /** *CURVE DI LISSAJOUS 3D e stereo * * @author Sergio Savoldelli wwww.savoldelli.net * @version numero 1 *******3/2004 * @version numero 2 *******9/2011 */ public class pendoloi7 extends java.applet.Applet implements MouseListener, MouseMotionListener { public double z = 0.015; public double yz = 0.015; public double pi2=PI*2; public int mov1,mov2,mov3,mov4,k=2,n1=6,n2=4,qq,q,modulo,temp=20,puntox=319,puntoy=300,pux3,puy3,puntox2=314,puntoy2=295,pux4,puy4; public double x1,y1, z1,x0,z0,y0,xe,ye,ze,xeb,yeb,zeb,i,a2; public double aa,k0,k02,xe2,ye2,xe2c,ye2c,x,y,xx,yy,r0,r02,w1,k1; Font font; Panel controlPanel, optionPanelColor, optionPanel0; Scrollbar sliderR, sliderr, sliderO; private boolean movingCircleOut = false; String sliderTextR = " A (1 - 12): "; String sliderTextr = " B (1 - 12): "; String sliderTextO = " C (1=rall. - 2=normale): "; String sliderTextC = " - Stereo - "; Label sliderLabelR, sliderLabelr, sliderLabelO, sliderLabelC, titleLabel,titolotra; Checkbox insideCheckbox, outsideCheckbox; public void init() { setSize(640, 480); setBackground(Color.black); setLayout(new BorderLayout()); q=0; controlPanel = new Panel(); controlPanel.setLayout(new GridLayout(13,1)); add("East", controlPanel); addMouseListener(this); addMouseMotionListener(this); titleLabel = new Label("CURVE DI LISSAJOUS. 3D ", 1); font = new Font("Helvetica", Font.BOLD, 12); titleLabel.setFont(font); titleLabel.setBackground(Color.black); titleLabel.setForeground(Color.red); //////////// titolotra = new Label("CLICK AND DRAG ", 2); font = new Font("Helvetica", Font.BOLD, 12); titolotra.setFont(font); titolotra.setBackground(Color.black); titolotra.setForeground(Color.green); controlPanel.add(titleLabel); controlPanel.add(titolotra); sliderLabelR = new Label(sliderTextR + Integer.toString(n2)); font = new Font("Helvetica", Font.PLAIN, 12); sliderLabelR.setFont(font); sliderLabelR.setBackground(Color.black); sliderLabelR.setForeground(Color.white); controlPanel.add(sliderLabelR); sliderR = new Scrollbar(Scrollbar.HORIZONTAL, n2, 0, 1,13); controlPanel.add(sliderR); sliderLabelr = new Label(sliderTextr + Integer.toString(n1)); font = new Font("Helvetica", Font.PLAIN, 12); sliderLabelr.setFont(font); sliderLabelr.setBackground(Color.black); sliderLabelr.setForeground(Color.white); controlPanel.add(sliderLabelr); sliderr = new Scrollbar(Scrollbar.HORIZONTAL, n1, 0, 1,13); controlPanel.add(sliderr); sliderLabelO = new Label(sliderTextO + Integer.toString(k)); font = new Font("Helvetica", Font.PLAIN, 12); sliderLabelO.setFont(font); sliderLabelO.setBackground(Color.black); sliderLabelO.setForeground(Color.white); controlPanel.add(sliderLabelO); sliderO = new Scrollbar(Scrollbar.HORIZONTAL, k, 0, 1, 3); controlPanel.add(sliderO); controlPanel.add(new Label(" ")); sliderLabelC = new Label(sliderTextC); CheckboxGroup CircleInOutGroup = new CheckboxGroup (); insideCheckbox = new Checkbox("si", CircleInOutGroup, !movingCircleOut); outsideCheckbox = new Checkbox("no", CircleInOutGroup, movingCircleOut); insideCheckbox.setForeground(Color.green); outsideCheckbox.setForeground(Color.cyan); font = new Font("Helvetica", Font.BOLD, 12); insideCheckbox.setFont(font); outsideCheckbox.setFont(font); optionPanel0 = new Panel(); optionPanel0.setLayout(new GridLayout(1, 3)); font = new Font("Helvetica", Font.BOLD, 12); sliderLabelC.setFont(font); sliderLabelC.setBackground(Color.red); sliderLabelC.setForeground(Color.white); optionPanel0.add(insideCheckbox); optionPanel0.add(outsideCheckbox); controlPanel.add(new Label(" ")); controlPanel.add(sliderLabelC,10); controlPanel.add("East", optionPanel0); } public boolean handleEvent(Event evt) { if (evt.target instanceof Scrollbar) { int value = ((Scrollbar)evt.target).getValue(); if (evt.target == sliderR) { sliderLabelR.setText(sliderTextR + Integer.toString(value)); n2 = value; if (n2 < 1)n2=1; } else if (evt.target == sliderr) { sliderLabelr.setText(sliderTextr + Integer.toString(value)); n1 = value; if (n1 < 1)n1=1; } else if (evt.target == sliderO) { sliderLabelO.setText(sliderTextO + Integer.toString(value)); k = value; } repaint(); } if ( evt.target == insideCheckbox || evt.target == outsideCheckbox) { repaint(); } return true; } public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; BufferedImage bi,co; Graphics2D big,cog; bi = (BufferedImage)createImage(420,420); big = bi.createGraphics(); big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); big.setColor(new Color(255,0,0));//linea rossa big.setStroke(new BasicStroke(1.5f)); co = new BufferedImage(420, 420, BufferedImage.TYPE_4BYTE_ABGR); cog = co.createGraphics(); cog.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); cog.setColor(new Color(0,255,0));//linea verde cog.setStroke(new BasicStroke(1.5f)); k1=0; if(k >= 2)k1=0; puntox=(int)((Math.cos(z))*(200-k1))+219; puntoy=(int)( Math.sin(yz)*(200-k1)+cos(yz))+220; puntox2=(int)((Math.cos(z+0.1))*(200-k1))+214; puntoy2=(int)( Math.sin(yz)*(200-k1)+cos(yz+0.01))+215; for (i=0;i<=1;i=i+(2)) { for(a2=0;a2<=pi2+0.0002;a2=a2+PI/160){ k1=k1+0.6283; if(k >= 2)k1=0; xeb= Math.cos(a2*n2+z+0.1)*(200-k1); xe= Math.cos(a2*n2+z)*(200-k1); yeb= Math.sin(a2*n1+yz+0.01)*(100-k1/2); ye= Math.sin(a2*n1+yz)*(100-k1/2); zeb= Math.sin(a2*n1+yz+0.01)*(100-k1/2); ze= Math.sin(a2*n1+yz)*(100-k1/2); k0=((xe)+219); r0= (ye)+220+ze; k02=(xeb+214); r02= (yeb)+215+zeb; if (outsideCheckbox.getState()){ k02= k02-1000; r02= r02-1000; puntox2= puntox2-1000; puntoy2= puntoy2-1000; } big.drawLine((int)(k0),(int)(r0),(int)puntox,(int)puntoy); cog.drawLine((int)(k02),(int)(r02),(int)puntox2,(int)puntoy2); puntox=(int)k0;puntoy=(int)r0; puntox2=(int)k02;puntoy2=(int)r02; } Point aPoint = new Point((int)k0,(int)r0); aPoint.move((int)(puntox),(int)(puntoy/sin(pi2))); pux3= aPoint.x ; puy3= aPoint.y; puntox=(int)pux3;puntoy=(int)puy3; Point aPoint2 = new Point((int)k02,(int)r02); aPoint2.move((int)(puntox2),(int)(puntoy2/sin(pi2))); pux4= aPoint2.x ; puy4= aPoint2.y; puntox2=(int)pux4;puntoy2=(int)puy4; } g2.drawImage(bi,0,0, this); g2.drawImage(co,0,0, this); try{ Thread.sleep(temp); } catch(InterruptedException e){ } } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { mov2 = e.getX(); int mov3 = e.getY(); if (e.isMetaDown()) { temp= 20; repaint(); } if (e.isAltDown()) { temp =1; repaint();} } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} public void mouseDragged(MouseEvent e) { mov1 = e.getX(); mov4 = e.getY(); z=z+0.015*((mov1-mov2));mov2=mov1; yz=yz +0.015* ((mov4-mov3));mov3=mov4; repaint(); e.consume(); } }