Image Color Effect: Brightness, Contrast, Negative : Color Model « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » Color ModelScreenshots 
Image Color Effect: Brightness, Contrast, Negative
Image Color Effect: Brightness, Contrast, Negative
   

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ByteLookupTable;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.ShortLookupTable;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;

public class ColorApp extends JFrame {
  DisplayPanel displayPanel;

  JButton brightenButton, darkenButton, contrastIncButton, contrastDecButton,
      reverseButton, resetButton;

  public ColorApp() {
    super();
    Container container = getContentPane();

    displayPanel = new DisplayPanel();
    container.add(displayPanel);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(32));
    panel
        .setBorder(new TitledBorder(
            "Click a Button to Perform the Associated Operation and Reset..."));

    brightenButton = new JButton("Brightness >>");
    brightenButton.addActionListener(new ButtonListener());
    darkenButton = new JButton("Darkness >>");
    darkenButton.addActionListener(new ButtonListener());
    contrastIncButton = new JButton("Contrast >>");
    contrastIncButton.addActionListener(new ButtonListener());
    contrastDecButton = new JButton("Contrast <<");
    contrastDecButton.addActionListener(new ButtonListener());
    reverseButton = new JButton("Negative");
    reverseButton.addActionListener(new ButtonListener());
    resetButton = new JButton("Reset");
    resetButton.addActionListener(new ButtonListener());

    panel.add(brightenButton);
    panel.add(darkenButton);
    panel.add(contrastIncButton);
    panel.add(contrastDecButton);
    panel.add(reverseButton);
    panel.add(resetButton);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowEventHandler());
    setSize(displayPanel.getWidth(), displayPanel.getHeight() 25);
    show();
  }

  class WindowEventHandler extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }

  public static void main(String arg[]) {
    new ColorApp();
  }

  class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      JButton button = (JButtone.getSource();

      if (button.equals(brightenButton)) {
        displayPanel.brightenLUT();
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (button.equals(darkenButton)) {
        displayPanel.darkenLUT();
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (button.equals(contrastIncButton)) {
        displayPanel.contrastIncLUT();
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (button.equals(contrastDecButton)) {
        displayPanel.contrastDecLUT();
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (button.equals(reverseButton)) {
        displayPanel.reverseLUT();
        displayPanel.applyFilter();
        displayPanel.repaint();
      else if (button.equals(resetButton)) {
        displayPanel.reset();
        displayPanel.repaint();
      }
    }
  }
}

class DisplayPanel extends JPanel {
  Image displayImage;

  BufferedImage bi;

  Graphics2D big;

  LookupTable lookupTable;

  DisplayPanel() {
    setBackground(Color.black)// panel background color
    loadImage();
    setSize(displayImage.getWidth(this), displayImage.getWidth(this))// panel
    createBufferedImage();
  }

  public void loadImage() {
    displayImage = Toolkit.getDefaultToolkit().getImage(
        "largeJava2sLogo.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 createBufferedImage() {
    bi = new BufferedImage(displayImage.getWidth(this), displayImage
        .getHeight(this), BufferedImage.TYPE_INT_ARGB);

    big = bi.createGraphics();
    big.drawImage(displayImage, 00this);
  }

  public void brightenLUT() {
    short brighten[] new short[256];
    for (int i = 0; i < 256; i++) {
      short pixelValue = (short) (i + 10);
      if (pixelValue > 255)
        pixelValue = 255;
      else if (pixelValue < 0)
        pixelValue = 0;
      brighten[i= pixelValue;
    }
    lookupTable = new ShortLookupTable(0, brighten);
  }

  public void darkenLUT() {
    short brighten[] new short[256];
    for (int i = 0; i < 256; i++) {
      short pixelValue = (short) (i - 10);
      if (pixelValue > 255)
        pixelValue = 255;
      else if (pixelValue < 0)
        pixelValue = 0;
      brighten[i= pixelValue;
    }
    lookupTable = new ShortLookupTable(0, brighten);
  }

  public void contrastIncLUT() {
    short brighten[] new short[256];
    for (int i = 0; i < 256; i++) {
      short pixelValue = (short) (i * 1.2);
      if (pixelValue > 255)
        pixelValue = 255;
      else if (pixelValue < 0)
        pixelValue = 0;
      brighten[i= pixelValue;
    }
    lookupTable = new ShortLookupTable(0, brighten);
  }

  public void contrastDecLUT() {
    short brighten[] new short[256];
    for (int i = 0; i < 256; i++) {
      short pixelValue = (short) (i / 1.2);
      if (pixelValue > 255)
        pixelValue = 255;
      else if (pixelValue < 0)
        pixelValue = 0;
      brighten[i= pixelValue;
    }
    lookupTable = new ShortLookupTable(0, brighten);
  }

  public void reverseLUT() {
    byte reverse[] new byte[256];
    for (int i = 0; i < 256; i++) {
      reverse[i(byte) (255 - i);
    }
    lookupTable = new ByteLookupTable(0, reverse);
  }

  public void reset() {
    big.setColor(Color.black);
    big.clearRect(00, bi.getWidth(this), bi.getHeight(this));
    big.drawImage(displayImage, 00this);
  }

    public void applyFilter() {
    LookupOp lop = new LookupOp(lookupTable, null);
    lop.filter(bi, bi);
  }

  public void update(Graphics g) {
    g.clearRect(00, getWidth(), getHeight());
    paintComponent(g);
  }

  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2D = (Graphics2Dg;
    g2D.drawImage(bi, 00this);
  }
}

           
         
    
    
  
Related examples in the same category
1.Convert HSB to RGB value
2.Convert RGB to HSB
3.IndexColorModelIndexColorModel
4.Color model demoColor model demo
5.Color CompositeColor Composite
6.Image Color Gray EffectImage Color Gray Effect
7.Checking for Color ModelsChecking for Color Models
8.Layers - Try to use alpha values (transparency) to draw in layersLayers - Try to use alpha values (transparency) to draw in layers
9.RGB Gray Filter
10.Color Schema Generator
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.