Java by API/java.awt.image/BufferedImage
Содержание
BufferedImage: getSource()
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.RGBImageFilter;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
NegativeFilter nf = new NegativeFilter();
Image i = createImage(new FilteredImageSource(createImage().getSource(), nf));
g.drawImage(i, 20, 20, this);
}
private BufferedImage createImage() {
BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawString("www.jexp.ru", 20, 20);
return bufferedImage;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class NegativeFilter extends RGBImageFilter {
public NegativeFilter() {
canFilterIndexColorModel = true;
}
public int filterRGB(int x, int y, int rgb) {
return rgb ^ 0x00ffffff;
}
}
BufferedImage: setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class Main extends JComponent {
public void paint(Graphics g) {
int width = getSize().width;
int height = getSize().height;
int[] data = new int[width * height];
int i = 0;
for (int y = 0; y < height; y++) {
int red = (y * 255) / (height - 1);
for (int x = 0; x < width; x++) {
int green = (x * 255) / (width - 1);
int blue = 128;
data[i++] = (red << 16) | (green << 8) | blue;
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
g.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("ColorPan");
frame.getContentPane().add(new Main());
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
BufferedImage.TYPE_INT_RGB
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class ColorPan extends JComponent {
public void paint(Graphics g) {
int width = getSize().width;
int height = getSize().height;
int[] data = new int[width * height];
int i = 0;
for (int y = 0; y < height; y++) {
int red = (y * 255) / (height - 1);
for (int x = 0; x < width; x++) {
int green = (x * 255) / (width - 1);
int blue = 128;
data[i++] = (red << 16) | (green << 8) | blue;
}
}
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, width, height, data, 0, width);
g.drawImage(image, 0, 0, this);
}
public static void main(String[] args) {
JFrame frame = new JFrame("ColorPan");
frame.getContentPane().add(new ColorPan());
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
BufferImage emboss
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
BufferedImage clone = emboss(createImage());
g.drawImage(clone, 20,20,this);
}
public BufferedImage emboss(BufferedImage src) {
int width = src.getWidth();
int height = src.getHeight();
BufferedImage dst;
dst = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++) {
int upperLeft = 0;
int lowerRight = 0;
if (i > 0 && j > 0)
upperLeft = src.getRGB(j - 1, i - 1);
if (i < height - 1 && j < width - 1)
lowerRight = src.getRGB(j + 1, i + 1);
int redDiff = ((lowerRight >> 16) & 255) - ((upperLeft >> 16) & 255);
int greenDiff = ((lowerRight >> 8) & 255) - ((upperLeft >> 8) & 255);
int blueDiff = (lowerRight & 255) - (upperLeft & 255);
int diff = redDiff;
if (Math.abs(greenDiff) > Math.abs(diff))
diff = greenDiff;
if (Math.abs(blueDiff) > Math.abs(diff))
diff = blueDiff;
int grayColor = 128 + diff;
if (grayColor > 255)
grayColor = 255;
else if (grayColor < 0)
grayColor = 0;
int newColor = (grayColor << 16) + (grayColor << 8) + grayColor;
dst.setRGB(j, i, newColor);
}
return dst;
}
private BufferedImage createImage(){
BufferedImage bufferedImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawString("www.jexp.ru", 20,20);
return bufferedImage;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
new BufferedImage(int width, int height, int imageType)
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public void paint(Graphics g) {
Image img = createImage();
g.drawImage(img, 20,20,this);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
private Image createImage(){
BufferedImage bufferedImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.drawString("www.jexp.ru", 20,20);
return bufferedImage;
}
}