|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jun 2003
Città: Monopoli
Messaggi: 2789
|
[JAVA] Creare filtro ad un immagine (modificare il colore)
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. Codice:
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();
}
}
__________________
CASE: Antec P182 - ALI: Corsair CX650M - CPU: AMD Ryzen 5 PRO 4650G + Cooler Master Hyper 212X - MB: ASUS PRIME A520M-K -RAM: G.SKILL Aegis 3200MHz 16GB - SSD: SanDisk Plus SSD 240GB - SA: Asus Xonar Essence STX - MOUSE: Logitech G9 - CUFFIE: Sennheiser HD 595 - *STEAM* |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Jun 2003
Città: Monopoli
Messaggi: 2789
|
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?
__________________
CASE: Antec P182 - ALI: Corsair CX650M - CPU: AMD Ryzen 5 PRO 4650G + Cooler Master Hyper 212X - MB: ASUS PRIME A520M-K -RAM: G.SKILL Aegis 3200MHz 16GB - SSD: SanDisk Plus SSD 240GB - SA: Asus Xonar Essence STX - MOUSE: Logitech G9 - CUFFIE: Sennheiser HD 595 - *STEAM* Ultima modifica di supersalam : 10-11-2011 alle 19:25. |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Jun 2003
Città: Monopoli
Messaggi: 2789
|
Codice:
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);
}
}
Ho fatto tutto ma non riesco a sovrapporre i pannelli. Dopo il click del pulsante ingrandite la finestra per veder comparire il pannello.
__________________
CASE: Antec P182 - ALI: Corsair CX650M - CPU: AMD Ryzen 5 PRO 4650G + Cooler Master Hyper 212X - MB: ASUS PRIME A520M-K -RAM: G.SKILL Aegis 3200MHz 16GB - SSD: SanDisk Plus SSD 240GB - SA: Asus Xonar Essence STX - MOUSE: Logitech G9 - CUFFIE: Sennheiser HD 595 - *STEAM* |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:13.



















