OVH Cloud OVH Cloud

Pixels transparants ?!

1 réponse
Avatar
Julien
Bonjour à tous,
je cherche à rendre transparant les pixels qui sont en pur bleue sur mon
image, comment puis-je faire ?
J'arrive à charger une image (png ou gif) dans mon panel, et je voudrais
rendre les pixels bleu transparants...

Voici mon code, d'avance merci pour vos conseils.

//-----------------------------------------------
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;

public class ju2 extends Frame {
/*-----------------------------------------------------------
* DECLARATIONS
-----------------------------------------------------------*/
private Panel mControlPanel;
private BufferedImage mBufferedImage;

/*-----------------------------------------------------------
* MAIN
-----------------------------------------------------------*/
public static void main(String[] args) {
String fileName = "default";
if (args.length > 0) fileName = args[0];
new ju2(fileName);
}

/*-----------------------------------------------------------
* CONSTRUCTEUR
-----------------------------------------------------------*/
public ju2(String fileName) {
super("ju2");
createUI();
loadImage(fileName);
setVisible(true);
}

/*-----------------------------------------------------------
* CREE L'UI
-----------------------------------------------------------*/
private void createUI() {
setLayout(new BorderLayout());

Button loadButton = new Button("Load...");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
FileDialog fd = new FileDialog(ju2.this);
fd.show();
if (fd.getFile() == null) return;
String path = fd.getDirectory() + fd.getFile();
loadImage(path);
}
});

// Add the user controls at the bottom of the window.
mControlPanel = new Panel();
mControlPanel.add(loadButton);
add(mControlPanel, BorderLayout.SOUTH);

// Terminate the application if the user closes the window.
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
}

/*-----------------------------------------------------------
* METHODE
-----------------------------------------------------------*/
private void adjustToImageSize() {
if (!isDisplayable()) addNotify(); // Do this to get valid Insets.
Insets insets = getInsets();
int w = mBufferedImage.getWidth() + insets.left + insets.right;
int h = mBufferedImage.getHeight() + insets.top + insets.bottom;
h += mControlPanel.getPreferredSize().height;
setSize(w, h);
}

/*-----------------------------------------------------------
* METHODE
-----------------------------------------------------------*/
private void center() {
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension d = getSize();
int x = (screen.width - d.width) / 2;
int y = (screen.height - d.height) / 2;
setLocation(x, y);
}

/*-----------------------------------------------------------
* METHODE
-----------------------------------------------------------*/
private void loadImage(String fileName) {
Image image = Toolkit.getDefaultToolkit().getImage(fileName);
image.transparentPixel(Color.blue);
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try { mt.waitForID(0); }
catch (InterruptedException ie) { return; }
if (mt.isErrorID(0)) return;
mBufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = mBufferedImage.createGraphics();
g2.drawImage(image, null, null);

adjustToImageSize();
center();
validate();
repaint();
setTitle("ju2");
}

/*-----------------------------------------------------------
* METHODE
-----------------------------------------------------------*/
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
if (mBufferedImage == null) return;
Insets insets = getInsets();
g2d.drawImage(mBufferedImage, insets.left, insets.top, null);
}
}

1 réponse

Avatar
Vincent Cantin
mBufferedImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);


Si tu veux avoir de la transparence, alors il te faut un alpha channel dans
ton image.
TYPE_INT_RGBA .. et utilise un logiciel d'imagerie pour editer l'alpha
channel dans ton image avant de la charger dans ton programme.