/** * */ 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 SquareGrat extends CreateImage { static final long serialVersionUID = 0; // name of illusion private final static String NAME = "Square Wave Grating"; // the basic sinewave grating parameters private double rBal = 1.0; private double gBal = 1.0; private double bBal = 1.0; DecimalFormat balFmt = new DecimalFormat("0.00"); private double angle = 0; // angle in radians private double cycleWidth = 25; // proportion of whole private double contrast = 1.0; // the image to be returned private Image grating; // image manipulation objects // angle private JPanel anglePanel = new JPanel(); private JLabel angleLabel = new JLabel("Angle"); private JSlider angleSlider = new JSlider(0,180,(int)angle); private JLabel angleValLabel = new JLabel(" = "+angle); // Size of sine wave cycle private JPanel cycleWPanel = new JPanel(); private JLabel cycleWLabel = new JLabel("Cycle Width"); private JSlider cycleWSlider = new JSlider(3,2*width, (int)(cycleWidth)); DecimalFormat cFMT = new DecimalFormat("000"); private JLabel cycleWValLabel = new JLabel(" = "+ cFMT.format(cycleWSlider.getValue())+"pixels"+ " ("+(balFmt.format( (double)cycleWidth/(double)width))+ "% of image)"); // contrast of sinewave private JPanel contrastPanel = new JPanel(); private JLabel contrastLabel = new JLabel("Contrast"); private JSlider contrastSlider = new JSlider(0,200,200); private JLabel contrastValLabel = new JLabel(" = 1.00"); // Color balance private RGBControl bal = new RGBControl(); private JLabel balLabel = new JLabel("Color"); // the control panel to be returned private JPanel control = new JPanel(); // empty constructor public SquareGrat(){ setSize(width,height); setupControl(); } private void setupControl(){ control.setLayout(new BorderLayout()); JPanel top = new JPanel(new GridLayout(4,1)); // cycle width control cycleWPanel.add(cycleWLabel); cycleWPanel.add(cycleWSlider); top.add(cycleWPanel); top.add(cycleWValLabel); // add the listener cycleWSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ cycleWidth = (double)cycleWSlider.getValue(); cycleWValLabel.setText(" = "+ cycleWSlider.getValue()+"pixels"+ " ("+(balFmt.format( (double)cycleWidth/(double)width))+ "% of image)"); repaint(); } }); // the angle control anglePanel.add(angleLabel); anglePanel.add(angleSlider); anglePanel.add(angleValLabel); top.add(anglePanel); // add the listener angleSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ angle = (double)angleSlider.getValue() *Math.PI/180.0; angleValLabel.setText(" = "+angleSlider.getValue()); repaint(); } }); // the contrast control contrastPanel.add(contrastLabel); contrastPanel.add(contrastSlider); contrastPanel.add(contrastValLabel); top.add(contrastPanel); // add the listener contrastSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ contrast = (double)contrastSlider.getValue()/ (double)contrastSlider.getMaximum(); contrastValLabel.setText(" = "+ balFmt.format(contrast)); repaint(); } }); control.add(top,BorderLayout.NORTH); bal.setShowLabels(true); JPanel balPanel = new JPanel(); balPanel.add(balLabel); balPanel.add(bal); control.add(balPanel,BorderLayout.CENTER); // add the listeners bal.red.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ rBal = bal.getRedDbl(); repaint(); } }); bal.green.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ gBal = bal.getGreenDbl(); repaint(); } }); bal.blue.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ bBal = bal.getBlueDbl(); repaint(); } }); } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; //get the images grating = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); // get the images graphics handler Graphics2D gP = (Graphics2D)grating.getGraphics(); gP.setPaint(Color.BLACK); gP.fill(new Rectangle(0,0,width,height)); // the bright bar luminance float br = 0.5f+0.5f*(float)contrast; Color brColor = new Color((float)(rBal*br), (float)(gBal*br),(float)(bBal*br)); // the dark bar luminance float dk = 0.5f-0.5f*(float)contrast; Color dkColor = new Color((float)(rBal*dk), (float)(gBal*dk),(float)(bBal*dk)); // rotate the image gP.rotate(angle,width/2,height/2); for (int i = -width/2; i <= width*3/2; i+= cycleWidth){ // bright bar gP.setColor(brColor); gP.fill(new Rectangle2D.Double(i,-height/2, cycleWidth/2.0, 2.0*height)); // dark bar gP.setColor(dkColor); gP.fill(new Rectangle2D.Double(i+cycleWidth/2.0, -height/2, cycleWidth/2.0, 2.0*height)); } // end the for block for drawing the sine wave grating // put the image onto the panel g2d.drawImage(grating, 0,0,this); } /* (non-Javadoc) * @see image.create.CreateImage#getImage() */ @Override public Image getImage() { return grating; } public String getName(){ return NAME; } public JPanel getControlPanel(){ return control; } public int getHeight(){ return height; } public int getWidht() { return width; } }