/** * */ package image.create; import java.awt.*; import java.awt.geom.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; import image.create.CreateImage; /** * @author John * */ public class Poggendorf extends CreateImage { static final long serialVersionUID = 0; // name of illusion private final static String NAME = "Poggendorf Illusion"; // the basic poggendorf color parameters private Color back = Color.LIGHT_GRAY; private Color bar = Color.DARK_GRAY; private Color line = Color.black; private double angle = 65; private double posRight = 0; // off set from intersection private double barWidth = 0.15; // proportion of whole // the image to be returned private Image poggendorf; // image manipulation objects // angle private JPanel anglePanel = new JPanel(); private JLabel angleLabel = new JLabel("Angle"); private JSlider angleSlider = new JSlider(0,85,(int)angle); private JLabel angleValLabel = new JLabel(" = "+angle); // position of right line private JPanel posPanel = new JPanel(); private JLabel posLabel = new JLabel("Move Right Line"); private JButton plus10 = new JButton("+10"); private JButton plus1 = new JButton("+1"); private JButton minus1 = new JButton("-1"); private JButton minus10 = new JButton("-10"); private JPanel resetRightPanel = new JPanel(); private JButton resetRight = new JButton("Reset"); // width of bar private JPanel barWPanel = new JPanel(); private JLabel barWLabel = new JLabel("Bar Width"); private JSlider barWSlider = new JSlider(0,100, (int)(barWidth*100.0)); private JLabel barWValLabel = new JLabel(" = "+ barWSlider.getValue()+"%"); // the control panel to be returned private JPanel control = new JPanel(); // empty constructor public Poggendorf(){ setSize(width,height); setupControl(); } private void setupControl(){ control.setLayout(new GridLayout(4,1)); // the position of the right line controls posPanel.add(posLabel); posPanel.add(plus10); posPanel.add(plus1); posPanel.add(minus1); posPanel.add(minus10); resetRightPanel.add(resetRight); control.add(posPanel); control.add(resetRightPanel); // add the listeners plus10.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ posRight -=10; repaint(); } }); plus1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ posRight -=1; repaint(); } }); minus1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ posRight +=1; repaint(); } }); minus10.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ posRight +=10; repaint(); } }); resetRight.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ posRight =0; repaint(); } }); plus10.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ posRight -=10; repaint(); } }); // the angle control anglePanel.add(angleLabel); anglePanel.add(angleSlider); anglePanel.add(angleValLabel); control.add(anglePanel); // add the listener angleSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ angle = angleSlider.getValue(); angleValLabel.setText(" = "+angle); repaint(); } }); // bar width control barWPanel.add(barWLabel); barWPanel.add(barWSlider); barWPanel.add(barWValLabel); control.add(barWPanel); // add the listener barWSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ barWidth = (double)barWSlider.getValue()/ (double)barWSlider.getMaximum(); barWValLabel.setText(" = "+ barWSlider.getValue()+"%"); repaint(); } }); } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; // find the center double cX = width/2.0; double cY = height/2.0; //get the images poggendorf = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); // get the images graphics handler Graphics2D gP = (Graphics2D)poggendorf.getGraphics(); gP.setPaint(back); gP.fill(new Rectangle(0,0,width,height)); // default stroke width gP.setStroke(new BasicStroke(3.0f)); // convert angle to rations double aRad = angle*Math.PI/180.0; gP.setPaint(line); // first the standard line on the left double xS = 0; double yS = findY(cY, xS-cX,aRad); gP.draw(new Line2D.Double(xS,yS,cX,cY)); // now the right hand line that can be moved xS = width; yS = findY(cY,xS-cX,aRad); gP.draw(new Line2D.Double(xS,yS+posRight, cX,cY+posRight)); // now draw the bar gP.setPaint(bar); gP.fill(new Rectangle2D.Double(cX-(double)width*barWidth/2.0, 0.0,(double)width*barWidth,height)); // put the image onto the panel g2d.drawImage(poggendorf, 0,0,this); } // find the y position of a line private double findY(double cY, double dX, double aRad){ return dX*Math.tan(aRad) + cY; } /* (non-Javadoc) * @see image.create.CreateImage#getImage() */ @Override public Image getImage() { return poggendorf; } public String getName(){ return NAME; } public JPanel getControlPanel(){ return control; } public int getHeight(){ return height; } public int getWidht() { return width; } }