我想制作一个小程序,允许您更改图像所选像素的Alpha通道。想想在photoshop上的橡皮擦工具。所以,我得到了在图像上绘制颜色的程序,并尝试使用setRGB(0x00000000,0x00000000,0x00000000)
将像素设置为alpha 0;没有骰子,只涂黑色。
我也试过画一个蓝色的椭圆形(效果很好)所以我知道我的鼠标听众正在工作。 (那是在代码中注释掉的)
现在我正在拍摄该图片的光栅并尝试使用setPixels()
或setRect()
修改alpha字符,但我似乎无法使其工作。我认为我的问题在于你必须传递的数组。我假设它是一个10 x 10像素阵列和4个通道,因此阵列长度必须为400.
这是我第一次尝试这个,所以我的代码有点丑陋,有很多东西被注释掉了,因为我已经尝试了大约一百万种方法;但在这里。
感谢您提前提供任何帮助。
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyFrame extends JFrame implements MouseListener,
MouseMotionListener {
BufferedImage myImage = new BufferedImage(300, 300,
BufferedImage.TYPE_INT_ARGB);
BufferedImage blankImage = new BufferedImage(10, 10,
BufferedImage.TYPE_INT_ARGB);
File myPNG = new File("output.png");
MyPanel mp = new MyPanel();
File tempPNG = new File("temp.png");
File initialFile = new File("test.jpg");
Color blankColor = new Color(0, 0, 0, .0f);
int diameter = 10;
int mouseX = 0;
int mouseY = 0;
boolean mouseClicked = false;
boolean fileCreated;
WritableRaster myRaster;
WritableRaster blankRaster;
int[] emptySquare = new int[400];
ColorModel colorModel = myImage.getColorModel();
public MyFrame() {
setLayout(new BorderLayout());
setSize(800, 800);
add(mp, BorderLayout.EAST);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setFocusable(true);
addMouseListener(this);
addMouseMotionListener(this);
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
for (int b = 0; b < 4; b++) {
emptySquare[x * y * b] = 0x00000000;
}
}
}
myRaster = myImage.getRaster();
blankRaster = blankImage.getRaster();
try {
myImage = ImageIO.read(initialFile);
fileCreated = ImageIO.write(myImage, "png", tempPNG);
myImage = ImageIO.read(tempPNG);
} catch (Exception e) {
}
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
// g2d.fillRect(0, 0, 300, 300);
g2d.drawImage(myImage, 0, 0, 300, 300, null);
// g2d.setColor(blankColor);
// g2d.setPaint(Color.blue);
// myImage.
}
private class MyPanel extends JPanel {
private MyPanel() {
}
}
@Override
public void mouseClicked(MouseEvent m) {
repaint();
}
@Override
public void mouseEntered(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent m) {
repaint();
}
@Override
public void mouseReleased(MouseEvent m) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent m) {
mouseX = m.getX();
mouseY = m.getY();
myRaster.setDataElements(mouseX, mouseY, blankRaster);
myImage = new BufferedImage(colorModel, myRaster, true, null);
myImage.setData(blankRaster);
myRaster.setPixel(mouseX, mouseY, emptySquare);
myImage.setData(blankRaster);
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .1f);
Graphics2D tempG2d = myImage.createGraphics();
tempG2d.setComposite(AlphaComposite.Clear);
// tempG2d.setColor(Color.blue);
tempG2d.fillOval(mouseX, mouseY, diameter, diameter);
// tempG2d.setColor(blankColor);
// tempG2d.setComposite(AlphaComposite.Src);
for (int i = mouseX; i < mouseX + diameter; i++) {
for (int j = mouseY; j < mouseY + diameter; j++) {
int white = 0xffffff;
Color c = new Color(0xccFF0000, true);
int color = myImage.getRGB(i, j) & 0x00ffffff; // use bitwise &
// to remove
// alpha
// component
myRaster.setPixel(mouseX, mouseY, emptySquare);
myImage.setData(myRaster);
// myImage.setRGB(i,j,color); // transparent white
}
}
// tempG2d.fillOval(mouseX, mouseY, diameter, diameter);
try {
// fileCreated=ImageIO.write(myImage, "png", tempPNG);
} catch (Exception e) {
}
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}