public class NewJFrame3 extends javax.swing.JFrame {
DisplayPanel displayPanel;
JButton brightenButton, darkenButton,
contIncButton, contDecButton;
final private JLabel jLabel1=null;
public JDesktopPane jDesktopPane2;
public JScrollPane jScrollPane1;
public JButton jButton1;
public JButton jj;
/** Creates new form NewJFrame3 */
public NewJFrame3() {
// initComponents();
jDesktopPane2.setSize(1000, 1000);
jDesktopPane2.add(displayPanel);
setVisible(true);
// jScrollPane1.add(displayPanel);
displayPanel = new DisplayPanel();
// jScrollPane1.add(container);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 2));
panel.setBorder(new TitledBorder(
"Click a Button to Perform the Associated Operation..."));
brightenButton = new JButton("Brightness >>");
brightenButton.addActionListener(new ButtonListener());
darkenButton = new JButton("Brightness <<");
darkenButton.addActionListener(new ButtonListener());
contIncButton = new JButton("Contrast >>");
contIncButton.addActionListener(new ButtonListener());
contDecButton = new JButton("Contrast <<");
contDecButton.addActionListener(new ButtonListener());
panel.add(brightenButton);
panel.add(darkenButton);
panel.add(contIncButton);
panel.add(contDecButton);
// container.add(BorderLayout.SOUTH, panel);
// addWindowListener(new WindowEventHandler());
// setSize(displayPanel.getWidth(), displayPanel.getHeight() + 10);
show(); // Display the frame
// jScrollPane1.add(displayPanel);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 628, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 482, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame3();
}
});
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton temp = (JButton) e.getSource();
if (temp.equals(brightenButton)) {
displayPanel.brighten = true;
displayPanel.changeOffSet();
System.out.println(displayPanel.offset + "=offset");
displayPanel.rescale();
displayPanel.repaint();
}
else if (temp.equals(darkenButton)) {
displayPanel.brighten = false;
displayPanel.changeOffSet();
System.out.println(displayPanel.offset + "=offset");
displayPanel.rescale();
displayPanel.repaint();
}
else if (temp.equals(contIncButton)) {
displayPanel.contrastInc = true;
displayPanel.changeScaleFactor();
System.out.println(displayPanel.scaleFactor + "=scaleF");
displayPanel.rescale();
displayPanel.repaint();
}
else if (temp.equals(contDecButton)) {
displayPanel.contrastInc = false;
displayPanel.changeScaleFactor();
System.out.println(displayPanel.scaleFactor + "=scaleF");
displayPanel.rescale();
displayPanel.repaint();
}
}
}
class DisplayPanel extends JPanel {
Image displayImage;
BufferedImage biSrc, biDest, bi;
Graphics2D big;
RescaleOp rescale;
float scaleFactor = 1.0f;
float offset = 10;
boolean brighten, contrastInc;
DisplayPanel() {
setBackground(Color.black);
loadImage();
setSize(displayImage.getWidth(this),
displayImage.getWidth(this));
createBufferedImages();
}
public void loadImage() {
displayImage = Toolkit.getDefaultToolkit().getImage("C:/TempEI4/dd.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(displayImage, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Exception while loading.");
}
// if (displayImage.getWidth(this) == -1) {
// System.out.println("No jpg file");
// System.exit(0);
// }
}
public void createBufferedImages() {
biSrc = new BufferedImage(displayImage.getWidth(this),
displayImage.getHeight(this),
BufferedImage.TYPE_INT_RGB);
big = biSrc.createGraphics();
big.drawImage(displayImage, 0, 0, this);
biDest = new BufferedImage(displayImage.getWidth(this),
displayImage.getHeight(this),
BufferedImage.TYPE_INT_RGB);
bi = biSrc;
}
public void changeOffSet() {
if (brighten) {
if (offset < 255)
offset = offset+5.0f;
}
else {
if (offset > 0)
offset = offset-5.0f;
}
}
public void changeScaleFactor() {
if (contrastInc) {
if (scaleFactor < 2)
scaleFactor = scaleFactor+0.1f;
}
else {
if (scaleFactor > 0)
scaleFactor = scaleFactor-0.1f;
}
}
public void rescale() {
rescale = new RescaleOp(scaleFactor, offset, null);
rescale.filter(biSrc, biDest);
bi = biDest;
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
paintComponent(g);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(bi, 0, 0, this);
}