PDA

View Full Version : [JAVA] Creare filtro ad un immagine (modificare il colore)


supersalam
09-11-2011, 13:02
Ragazzi sto cercando di capire come cambiare colore ad una immagine che ho in una JLabel.

E' un esercizio che sto facendo solo per diletto.

import javax.swing.*;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.awt.image.RescaleOp;

//Inserire un immagine in una label, tramite 2 bottoni cambiare la tonalità dell'immagine (blu e rossa) senza aprire un nuovo frame.

public class Bho7 implements ActionListener{

JButton rosso;
JButton blu;
JLabel label;
ImageIcon img = new ImageIcon("gray.jpg");


public Bho7(){

JFrame frame = new JFrame("Cambia Colore");
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 600);
frame.setLayout(new FlowLayout());
label = new JLabel(img);
rosso = new JButton("Rosso");
blu = new JButton("Blu");

rosso.addActionListener(this);
blu.addActionListener(this);


frame.add(label);
frame.add(rosso);
frame.add(blu);



}

public void actionPerformed (ActionEvent e){
if (e.getSource()==rosso){
//colora di rosso
}

if (e.getSource()==blu){
//colora di blu
}


}



public static void main (String[] args){
new Bho7();
}


}

supersalam
10-11-2011, 19:21
Ragazzi mi è venuta in idea.

Potrei creare un pannello opaco e sovrapporlo all'immagine.

Ma come si fa?

Edit: oppure, è possibile ridefinire questa funzione ( BufferedImage.TYPE_BYTE_GRAY)? Al posto della scala di grigi ci metto un altro colore, che dite, si può fare?

supersalam
13-11-2011, 19:43
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
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 javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Bho8 extends JFrame implements ActionListener {

JButton rosso;
JButton blu;
immagine img;
JPanel ab =new JPanel();
JPanel uno = new JPanel();
JPanel due = new JPanel();

Bho8() {

setSize(670, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(new ContentPanel());

rosso = new JButton("Rosso");
blu = new JButton("Blu");

rosso.addActionListener(this);
blu.addActionListener(this);

due.add(rosso);
due.add(blu);
add(due, BorderLayout.SOUTH);

setVisible(true);
}

public void actionPerformed (ActionEvent e){
if (e.getSource()==rosso){
//colora di rosso
ab.setSize(640, 480);
ab.setBackground(new Color(255, 0, 0, 56));
add(ab);

}

if (e.getSource()==blu){
//colora di blu
ab.setSize(640, 480);
ab.setBackground(new Color(0, 0, 255, 56));
add(ab);
}


}

public static void main(String[] args) {
Bho8 jrframe = new Bho8();
jrframe.setVisible(true);
}

}

class ContentPanel extends JPanel {
private String immag = "C:/Users/Alessandro/Documents/Programmi Ecplise/Esercizi/src/gray.jpg";
private int imageWidth, imageHeight;

Image bgimage = null;

ContentPanel() {
MediaTracker mt = new MediaTracker(this);
bgimage = Toolkit.getDefaultToolkit().getImage(immag);
mt.addImage(bgimage, 0);
try {
mt.waitForAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);
int imageWidth = bgimage.getWidth(null);
int imageHeight = bgimage.getHeight(null);
g.drawImage(bgimage, 1, 1, null);
}


}

Aiutino?? :help:

Ho fatto tutto ma non riesco a sovrapporre i pannelli. Dopo il click del pulsante ingrandite la finestra per veder comparire il pannello.