S niecim podobnym som sa nedavno hral:
public class Canv extends JPanel{
// Tieto JPanley vytvoris 2 v vo JForme napr... inak ten prvy nebide potrebovat
//premennu dest a zrejme ani VolatileImage kedze sa tam nic nebude menit, ale to
//uz pouvazuj sam.
public BufferedImage src;
public BufferedImage dest;
public VolatileImage vImg;
public Graphics2D g;
int w,h;
public boolean rend = false;
public Canv (){
w = 800;
h = 600;
src = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
dest = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
}
// Tato metoda je tam len preto ze sa mi to nechcelo robit inak
public void loadImg(File imgFile) {
try {
src = ImageIO.read(imgFile);
w = src.getWidth();
h = src.getHeight();
dest = createBufferedImage(w, h);
int[] rgb = new int[w*h];
src.getRGB(0, 0, w, h, rgb, 0, w);
dest.setRGB(0, 0, w, h, rgb, 0, w);
rend = false;
repaint();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public BufferedImage createBufferedImage(int w, int h) {
return new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
}
public void paint(Graphics g) {
vImg = createVolatileImage(w, h);
update(g);
}
public void update(Graphics g) {
do {
int returnCode = vImg.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
vImg.flush();
}
renderOffscreen();
g.drawImage(vImg, 0, 0, this);
} while (vImg.contentsLost());
}
private void renderOffscreen() {
do {
if (vImg.validate(getGraphicsConfiguration()) == VolatileImage.IMAGE_INCOMPATIBLE) {
vImg.flush();
vImg = createVolatileImage(w, h);
}
g = vImg.createGraphics();
g.drawImage(dest, 0, 0, dest.getWidth(), dest.getHeight(), this);
g.dispose();
} while (vImg.contentsLost());
}
public void changeImage(double d, double e, double f) {
// dufam ze potrebujes nejaku takuto zmenu intenzity farieb
rend = false;
int rgb[] = new int[w*h];
int rgbOut[] = new int[w*h];
int tmpR = 0, tmpB = 0, tmpG =0;
src.getRGB(0, 0, w, h, rgb, 0, w);
for(int i=0; i< w*h; i++) {
tmpR = (int) (d*((rgb[i]>>16)&0xff));
tmpG = (int) (e*((rgb[i]>>8)&0xff));
tmpB = (int) (f*(rgb[i]&0xff));
tmpR = (tmpR>0xff)?0xff:tmpR;
tmpG = (tmpG>0xff)?0xff:tmpG;
tmpB = (tmpB>0xff)?0xff:tmpB;
rgbOut[i] = (tmpR<<16) | (tmpG<<8) | tmpB;
}
dest.setRGB(0, 0, w, h, rgbOut, 0, w);
vImg.flush();
rend = true;
repaint();
}
}
v Maine som postupoval nejako takto:
public class Main extends JFrame implements ActionListener, ChangeListener {
/**
*
*/
Container container;
Canv canvas;
JMenuBar menub;
JMenu file;
JMenuItem open, hdropen;
GridBagConstraints gbc;
JSlider reSlider, blSlider, grSlider;
JButton process;
JLabel reLab, grLab, blLab;
JButton make;
JPanel lePanel;
private double bl=1.0,gr=1.0,re=1.0;
private boolean imgLoaded = false;
HDR hdr;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main();
}
public Main() {
super("Img");
setSize(1024,768);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container = getContentPane();
canvas = new Canv();
container.setLayout(new BorderLayout());
createMenu();
setJMenuBar(menub);
this.container.add(this.canvas, BorderLayout.CENTER);
createPanel();
setVisible(true);
}
public void createMenu() {
menub = new JMenuBar();
file = new JMenu("File");
open = new JMenuItem("Open");
open.addActionListener(this);
file.add(open);
menub.add(file);
}
public JPanel createPanel() {
Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>();
labelTable.put( new Integer( 100 ), new JLabel("1.0x") );
labelTable.put( new Integer( 300 ), new JLabel("3.0x") );
labelTable.put( new Integer( 0 ), new JLabel("0.0x") );
gbc = new GridBagConstraints();
gbc.insets = new Insets(10,5,0,0);
gbc.gridheight = 1;
gbc.gridwidth = 1;
lePanel = new JPanel();
lePanel.setLayout(new GridBagLayout());
lePanel.setFont(new Font("Serif", Font.ITALIC, 15));
gbc.gridy = 3;
make = new JButton("Change");
make.addActionListener(this);
make.setEnabled(false);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
lePanel.add(make,gbc);
gbc.gridy = 0;
reLab = new JLabel("red");
reSlider = new JSlider(0, 300);
reSlider.setValue(100);
reSlider.addChangeListener(this);
reSlider.setLabelTable(labelTable);
reSlider.setPaintLabels(true);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
lePanel.add(reLab, gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
lePanel.add(reSlider,gbc);
gbc.gridy = 1;
grLab = new JLabel("green");
grSlider = new JSlider(0, 300);
grSlider.addChangeListener(this);
grSlider.setValue(100);
grSlider.setLabelTable(labelTable);
grSlider.setPaintLabels(true);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
lePanel.add(grLab,gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
lePanel.add(grSlider,gbc);
gbc.gridy = 2;
blLab = new JLabel("blue");
blSlider = new JSlider(0, 300);
blSlider.setValue(100);
blSlider.addChangeListener(this);
blSlider.setLabelTable(labelTable);
blSlider.setPaintLabels(true);
gbc.gridx = 0;
gbc.fill = GridBagConstraints.NONE;
lePanel.add(blLab,gbc);
gbc.gridx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
lePanel.add(blSlider,gbc);
container.add(lePanel, BorderLayout.EAST);
return lePanel;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource()==this.open) mOpen();
if (e.getSource()==this.make) chImage();
if (e.getSource()==this.hdropen) hdrOpen();
}
private void chImage() {
canvas.changeImage(re,gr,bl);
make.setEnabled(false);
}
private void mOpen() {
make.setEnabled(false);
JFileChooser chooser = new JFileChooser();
ImageFileFilter filter = new ImageFileFilter();
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
canvas.loadImg(chooser.getSelectedFile());
make.setEnabled(true);
imgLoaded = true;
}
}
public class ImageFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String extension = Utils.getExtension(f);
if (extension != null) {
if (extension.equals(Utils.tiff) ||
extension.equals(Utils.tif) ||
extension.equals(Utils.gif) ||
extension.equals(Utils.jpeg) ||
extension.equals(Utils.jpg) ||
extension.equals(Utils.png)) {
return true;
} else {
return false;
}
}
return false;
}
@Override
public String getDescription() {
return "Image Files and Folders";
}
}
@Override
public void stateChanged(ChangeEvent e) {
JSlider src = (JSlider) e.getSource();
double val = src.getValue()/100.0;
if (src == this.reSlider) re = val;
if (src == this.blSlider) bl = val;
if (src == this.grSlider) gr = val;
if (imgLoaded) make.setEnabled(true);
}
}
Mozno tam este nieco chyba lebo tam toho bolo viac, ale tak to si uz napises. Ak ti to aspon trocha pozmoze.