/** * */ package image.create; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import java.awt.event.*; import java.text.*; import javax.swing.*; import javax.swing.event.*; // FrontEnd Imports import image.create.CreateImage; import gui.elements.RGBControl; /** * @author John * */ public class BasicShape extends CreateImage { static final long serialVersionUID = 0; // name of illusion public final static String NAME = "Circle or Bar"; // type private final static int CIRCLE = 0; private final static int BAR = 1; private int type = CIRCLE; // private int type = BAR; // the basic image parameters private Color foreground = new Color(0.6f,0.6f,0.6f); private Color background = new Color(0.4f,0.4f,0.4f); //luminances private double lumF = 0.6; private double lumB = 0.4; // color balance private double rBalF = 1.0; private double gBalF = 1.0; private double bBalF = 1.0; private double rBalB = 1.0; private double gBalB = 1.0; private double bBalB = 1.0; DecimalFormat balFmt = new DecimalFormat("0.00"); // circle parameters private double cW = 0.5; // proportion of whole // square parameters private double bW = 0.3; // proportion of whole private double bH = 1.0; // proportion of whole private double angle = 0; // angle in radians // the image to be returned private Image object; // image manipulation objects // type private JPanel typePanel = new JPanel(); private JLabel typeLabel = new JLabel("Type: "); private final static String [] TYPES = { "Circle", "Bar"}; private JComboBox typeCBX = new JComboBox(TYPES); // 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 widthPanel = new JPanel(); private JLabel widthLabel = new JLabel("Circle Diam."); private JSlider widthSlider = new JSlider(1,90, (int)(cW*100.0)); private JLabel widthValLabel = new JLabel(" = "+ balFmt.format(cW)); // Size of sine wave cycle private JPanel heightPanel = new JPanel(); private JLabel heightLabel = new JLabel("Bar Height"); private JSlider heightSlider = new JSlider(1,100, (int)(bH*100.0)); private JLabel heightValLabel = new JLabel(" = "+ balFmt.format(bH)); // contrast private JPanel contrastPanel = new JPanel(); private JLabel contrastLabel = new JLabel("Contrast"); private JSlider contrastSlider = new JSlider(-100,100, 20); private JLabel contrastValLabel = new JLabel(" = "+ balFmt.format(0.2)); // foreground colors private RGBControl fore = new RGBControl(); private JLabel foreLabel = new JLabel("Foreground"); // background colors private RGBControl back = new RGBControl(); private JLabel backLabel = new JLabel("Background"); // the control panel to be returned private JPanel control = new JPanel(); // empty constructor public BasicShape(){ setSize(width,height); setupControl(); } private void setupControl(){ control.setLayout(new BorderLayout()); JPanel top = new JPanel(new GridLayout(5,1)); // type width control typePanel.add(typeLabel); typePanel.add(typeCBX); top.add(typePanel); // add the listener typeCBX.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ type = typeCBX.getSelectedIndex(); setVisibility(); repaint(); } }); // object width control widthPanel.add(widthLabel); widthPanel.add(widthSlider); widthPanel.add(widthValLabel); top.add(widthPanel); // add the listener widthSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ if (type == CIRCLE){ cW = (double)widthSlider.getValue()/100.0; widthValLabel.setText(" = "+ balFmt.format(cW)); } else { bW = (double)widthSlider.getValue()/100.0; widthValLabel.setText(" = "+ balFmt.format(bW)); } repaint(); } }); // bar height control heightPanel.add(heightLabel); heightPanel.add(heightSlider); heightPanel.add(heightValLabel); top.add(heightPanel); // add the listener heightSlider.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ bH = (double)heightSlider.getValue()/100.0; heightValLabel.setText(" = "+ balFmt.format(bH)); 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){ double d = (double)contrastSlider.getValue()/ (double)contrastSlider.getMaximum(); lumF = 0.5+0.5*d; lumB = 0.5-0.5*d; contrastValLabel.setText(" = "+ balFmt.format(d)); repaint(); } }); fore.setShowLabels(true); JPanel forePanel = new JPanel(); forePanel.add(foreLabel); forePanel.add(fore); control.add(forePanel,BorderLayout.CENTER); // add the listeners fore.red.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ rBalF = fore.getRedDbl(); repaint(); } }); fore.green.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ gBalF = fore.getGreenDbl(); repaint(); } }); fore.blue.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ bBalF = fore.getBlueDbl(); repaint(); } }); back.setShowLabels(true); JPanel backPanel = new JPanel(); backPanel.add(backLabel); backPanel.add(back); control.add(backPanel,BorderLayout.SOUTH); // add the listeners back.red.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ rBalB = back.getRedDbl(); repaint(); } }); back.green.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ gBalB = back.getGreenDbl(); repaint(); } }); back.blue.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e){ bBalB = back.getBlueDbl(); 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(); } }); control.add(top,BorderLayout.NORTH); setVisibility(); } private void setVisibility(){ if (type == CIRCLE){ widthLabel.setText("Circle Diam."); anglePanel.setVisible(false); heightPanel.setVisible(false); widthSlider.setValue((int)(cW*100)); widthValLabel.setText(" = "+ balFmt.format(cW)); } else { widthLabel.setText("Bar Diam."); anglePanel.setVisible(true); heightPanel.setVisible(true); widthSlider.setValue((int)(bW*100)); widthValLabel.setText(" = "+ balFmt.format(bW)); } } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; //get the images object = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB); // get the images graphics handler Graphics2D gP = (Graphics2D)object.getGraphics(); // background color background = new Color((float)(rBalB*lumB), (float)(gBalB*lumB),(float)(bBalB*lumB)); gP.setPaint(background); gP.fill(new Rectangle(0,0,width,height)); // forgeround luminance foreground = new Color((float)(rBalF*lumF), (float)(gBalF*lumF),(float)(bBalF*lumF)); gP.setPaint(foreground); double startX,startY; switch (type){ case (CIRCLE) : startX = width/2.0; startX -= cW*(double)width/2.0; startY = height/2.0; startY -= cW*width/2.0; double size = cW*(double)width; gP.fill(new Ellipse2D.Double(startX,startY, size,size)); break; case (BAR) : // rotate the image gP.rotate(angle,width/2,height/2); startX = width/2.0; startX -= bW*(double)width/2.0; startY = height/2.0; startY -= bH*(double)height/2.0; double w = (double)width*bW; double h = (double)height*bH; gP.fill(new Rectangle2D.Double(startX,startY, w,h)); break; } // put the image onto the panel g2d.drawImage(object, 0,0,this); } /* (non-Javadoc) * @see image.create.CreateImage#getImage() */ @Override public Image getImage() { return object; } public String getName(){ return TYPES[type]; } public JPanel getControlPanel(){ return control; } public int getHeight(){ return height; } public int getWidht() { return width; } }