/** * */ package image.create; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import java.text.*; import javax.swing.*; import javax.swing.event.*; import gui.elements.RGBControl; import image.create.CreateImage; /** * @author John * */ public class Hermann extends CreateImage { static final long serialVersionUID = 0; // name of illusion private final static String NAME = "Hermann Grid"; // the basic sinewave grating parameters private double rSqBal = 1.0; private double gSqBal = 1.0; private double bSqBal = 1.0; private double rLnBal = 1.0; private double gLnBal = 1.0; private double bLnBal = 1.0; DecimalFormat balFmt = new DecimalFormat("0.00"); private double squareLum = 0.0; // luminance of the squares private double lineLum = 1.0; // luminance of the lines private double squareSize = 0.10; // proportion of width private double lineWidth = 0.02;// proporition of width // the image to be returned private Image hermann; // image manipulation objects // square private JPanel squarePanel = new JPanel(); private JLabel squareLabel = new JLabel("Square Size"); private JSlider squareSlider = new JSlider(0,100,(int)(100.0*squareSize)); private JLabel squareValLabel = new JLabel(" = "+ balFmt.format(squareSize)); // Size of lines cycle private JPanel linePanel = new JPanel(); private JLabel lineLabel = new JLabel("Line Width"); private JSlider lineSlider = new JSlider(1,20, (int)(lineWidth*100.0)); private JLabel lineValLabel = new JLabel(" = "+ balFmt.format(lineWidth)); // foreground colors private RGBControl fore = new RGBControl(); private JLabel foreLabel = new JLabel(" Square"); // background colors private RGBControl back = new RGBControl(); private JLabel backLabel = new JLabel(" Line"); // Rel Luminance of Square private JPanel sqLumPanel = new JPanel(); private JLabel sqLumLabel = new JLabel(" Rel. Lum."); private JSlider sqLumSlider = new JSlider(0,255,0); private JLabel sqLumValLabel = new JLabel(" = 0.00"); // Rel Luminance of Line private JPanel lnLumPanel = new JPanel(); private JLabel lnLumLabel = new JLabel(" Rel. Lum."); private JSlider lnLumSlider = new JSlider(0,255,255); private JLabel lnLumValLabel = new JLabel(" = 1.00"); // the control panel to be returned private JPanel control = new JPanel(); // empty constructor public Hermann(){ setSize(width,height); setupControl(); } private void setupControl(){ control.setLayout(new GridLayout(2,1)); JPanel top = new JPanel(new BorderLayout()); JPanel bot = new JPanel(new BorderLayout()); // the square size control squarePanel.add(squareLabel); squarePanel.add(squareSlider); squarePanel.add(squareValLabel); top.add(squarePanel,BorderLayout.NORTH); // add the listener squareSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ squareSize = (double)squareSlider.getValue() /100.0; squareValLabel.setText(" = "+ balFmt.format(squareSize)); repaint(); } }); // the Square Luminance control sqLumPanel.add(sqLumLabel); sqLumPanel.add(sqLumSlider); sqLumPanel.add(sqLumValLabel); top.add(sqLumPanel,BorderLayout.CENTER); // add the listener sqLumSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ squareLum = (double)sqLumSlider.getValue() /255.0; sqLumValLabel.setText(" = "+ balFmt.format(squareLum)); repaint(); } }); fore.setShowLabels(true); JPanel forePanel = new JPanel(); forePanel.add(foreLabel); forePanel.add(fore); top.add(forePanel,BorderLayout.SOUTH); // add the listeners fore.red.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ rSqBal = fore.getRedDbl(); repaint(); } }); fore.green.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ gSqBal = fore.getGreenDbl(); repaint(); } }); fore.blue.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ bSqBal = fore.getBlueDbl(); repaint(); } }); // line width control linePanel.setAlignmentX(LEFT_ALIGNMENT); linePanel.add(lineLabel); linePanel.add(lineSlider); linePanel.add(lineValLabel); bot.add(linePanel,BorderLayout.NORTH); // add the listener lineSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ lineWidth = (double)lineSlider.getValue() /100.0; lineValLabel.setText(" = "+ balFmt.format(lineWidth)); repaint(); } }); // the Line Luminance control lnLumPanel.add(lnLumLabel); lnLumPanel.add(lnLumSlider); lnLumPanel.add(lnLumValLabel); bot.add(lnLumPanel,BorderLayout.CENTER); // add the listener lnLumSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ lineLum = (double)lnLumSlider.getValue() /255.0; lnLumValLabel.setText(" = "+ balFmt.format(lineLum)); repaint(); } }); back.setShowLabels(true); JPanel backPanel = new JPanel(); backPanel.add(backLabel); backPanel.add(back); bot.add(backPanel,BorderLayout.SOUTH); // add the listeners back.red.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ rLnBal = back.getRedDbl(); repaint(); } }); back.green.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ gLnBal = back.getGreenDbl(); repaint(); } }); back.blue.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ bLnBal = back.getBlueDbl(); repaint(); } }); control.add(top); control.add(bot); } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; //get the images hermann = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); // get the images graphics handler Graphics2D gP = (Graphics2D)hermann.getGraphics(); gP.setPaint(new Color((float)(lineLum*rLnBal), (float)(lineLum*gLnBal), (float)(lineLum*bLnBal))); gP.fill(new Rectangle(0,0,width,height)); double squarePxl = (double)width*squareSize; double linePxl = (double)width*lineWidth; gP.setPaint(new Color((float)(squareLum*rSqBal), (float)(squareLum*gSqBal), (float)(squareLum*bSqBal))); for (int i = 0; i < width; i += squarePxl+linePxl){ for (int j = 0; j < height; j += squarePxl+linePxl){ gP.fill(new Rectangle2D.Double(i,j,squarePxl, squarePxl)); } } // put the image onto the panel g2d.drawImage(hermann, 0,0,this); } /* (non-Javadoc) * @see image.create.CreateImage#getImage() */ @Override public Image getImage() { return hermann; } public String getName(){ return NAME; } public JPanel getControlPanel(){ return control; } public int getHeight(){ return height; } public int getWidht() { return width; } }