import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Tes extends JPanel {
BufferedImage pic;
public Tes() {
try {
pic = ImageIO.read(getClass().getClassLoader().getResourceAsStream("pic.jpg"));
} catch (IOException e) {}
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setLocation(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
}
public static void main(String[] args) {
new Tes();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setClip(new RoundRectangle2D.Double(10,10,pic.getWidth(),pic.getHeight(),10,10));
g.drawImage(pic,10,10,this);
g.setClip(new RoundRectangle2D.Double(10,70,pic.getWidth(),pic.getHeight(),10,10));
g.drawImage(pic,10,70,this);
}
}
public class ImageManager {
public ImageManager(){
...
}
public void createImage(String imageURL) {
// Bild von URL einlesen
// Quadratischen Ausschnitt rausschneiden
// abgerundeten Rahmen drum machen
// Bild abspeichern.
}
}
imageManager.createImage(String Url) aufrufen.
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
public class Tes extends JPanel {
BufferedImage pic;
public Tes() {
pic = createImage(getClass().getClassLoader().getResource("pic.jpg"));
JFrame frame = new JFrame();
frame.setSize(200,200);
frame.setLocation(100,100);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
}
public BufferedImage createImage(URL imageURL) {
BufferedImage buf = null;
BufferedImage temp = null;
try {
temp = ImageIO.read(imageURL);
} catch (IOException e) {
return null;
}
buf = new BufferedImage(temp.getWidth(),temp.getHeight(),BufferedImage.TRANSLUCENT);
Graphics drawBuf = buf.createGraphics();
drawBuf.setClip(new RoundRectangle2D.Double(0,0,temp.getWidth(),temp.getHeight(),10,10));
drawBuf.drawImage(temp, 0, 0, this);
drawBuf.dispose();
return buf;
}
public static void main(String[] args) {
new Tes();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(pic, 10,10,this);
}
}
private void resizeImage(BufferedImage bufImage, int width, int height){
// Hier sollte einfach nur das Bild skaliert werden.
}
private void cropToSquare(BufferedImage bufImage){
// Hier sollte ausgehend von der Bildmitte ein maximal mögliches Quadrat ausgeschnitten werden.
}
public void doMagic(String imageURL) throws IOException{
URL imgUrl = new URL(imageURL);
BufferedImage image = ImageIO.read(imageURL);
BufferedImage scaledImage = this.getScaledImage(image, 150, 150);
BufferedImage framedImage = this.getFramedImage(resizedImage);
File imgFile = fileManager.getFile("myfile.png");
ImageIO.write(framedImage, "png", imgFile);
}
private BufferedImage getScaledImage(BufferedImage originalImage, int width, int height){
Image tempImg = originalImage.getScaledInstance(width, height,Image.SCALE_SMOOTH);
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
resizedImage.createGraphics().drawImage(tempImg, 0, 0, null);
return resizedImage;
}
private BufferedImage getFramedImage(BufferedImage originalImage){
BufferedImage buf = new BufferedImage(originalImage.getWidth(),originalImage.getHeight(),BufferedImage.TRANSLUCENT);
Graphics graphics = buf.createGraphics();
graphics.setClip(new RoundRectangle2D.Double(0,0,originalImage.getWidth(),originalImage.getHeight(),10,10));
graphics.drawImage(originalImage, 0, 0, null);
graphics.dispose();
return buf;
}