E
eichkamp
Gast
hallo, also ich sollte 2 methoden schreiben, eine soll ein bild um 90grad na rechts schieben, andere um 180. Die Aufgabe scheint fertig zu sein, aber wenn ich die tetsen moechte, krieg ich eine fehlermeldung, dass BufferedImage cannot be resolved.
Code:
import javax.imageio.*;
import java.io.IOException;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.*;
import java.io.FileInputStream;
import java.io.InputStream;
public class Picture {
private RGBColor picture[][];
private int width;
private int height;
/**
* initialize a picture by opening given url
* @param picUrl url to the path of *.bmp picture
*/
public Picture(String picUrl){
openAndSetPicture(picUrl);
}
/**
* rotates this picture 90 degrees to the right
*
*/
public void rot90DegRight(BufferedImage img){
int width = img.getWidth();
int height = img.getHeight();
BufferedImage imgFlip = new BufferedImage(height, width, img.getType());
for(int i=0; i<width; i++)
for(int j=0; j<height; j++)
imgFlip.setRGB(j, i, img.getRGB(i, j));
}
/**
* rotates this picture 180 degrees
*
*/
public void rot180Deg(BufferedImage img, int angle){
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = new BufferedImage(w, h, img.getType());
Graphics2D g = dimg.createGraphics();
g.rotate(Math.toRadians(angle), w/2, h/2);
g.drawImage(img, null, 0, 0);
}
/**
* determines white pixels, aproximates its new color by using the average of neighbour colors
*
*/
public void repairPicture(){
//TODO
// !!caution!! at the borders and do not use white neighbours by aproximation
}
/**
* reads an 24-bit(8,8,8) Bitmap and store it into picture-array
* @param picUrl The url to the pic
* @return true, if successful else false
*/
private boolean openAndSetPicture(String picUrl){
BufferedImage pic;
try {
InputStream iS= new FileInputStream(picUrl);
// get buffer of the picture
pic = ImageIO.read(iS);
// get additional picture informations
this.height = pic.getHeight();
this.width = pic.getWidth();
// store rgb colors in picture
this.picture = new RGBColor[this.width][this.height];
ColorModel cm= ColorModel.getRGBdefault();
for (int w=0; w< this.width; w++){
for(int h=0; h< this.height; h++){
// read out every RGBcolor
int pixel = pic.getRGB(w, h);
int rVal= cm.getRed(pixel);
int gVal= cm.getGreen(pixel);
int bVal= cm.getBlue(pixel);
//set this colors in picture
this.picture[w][h] = new RGBColor(rVal, gVal, bVal);
}
}
return true;
}catch (IOException e) {
e.printStackTrace();
}
return false;
}
public BufferedImage getImage(){
BufferedImage pic = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
for(int w=0; w<this.width; w++){
for(int h=0; h<this.height; h++){
int red= this.picture[w][h].getRed();
int blue= this.picture[w][h].getBlue();
int green= this.picture[w][h].getGreen();
int rgbVal= new Color(red, green, blue).getRGB();
pic.setRGB(w, h, rgbVal);
}
}
return pic;
}
// Getters
/**
*
* @return the width of the picture
*/
public int getWidth(){
return this.width;
}
/**
*
* @return the height of the picture
*/
public int getHeight(){
return this.height;
}
}
Code:
/**
* this class is able to show a picture on the monitor
* @author MPGI 2
* @version 1.0
*
*/
public class ShowPicture extends Frame
{
private Image pic;
static final long serialVersionUID= 2L;
public static void main( String[] args )
{
// url to the Picture
String picUrl= "brokenPic.bmp"; // sunset at Kuehlungsborn-Yachhafen (Bildausschnitt)
//Creates an Picture
Picture pic= new Picture(picUrl);
// *******************************************
// here you can test all new implemented methods
// *******************************************
//BufferedImage inputImage= rot180Deg(brokenPic.bmp,180);
pic.rot90DegRight(BufferedImage img);
pic.rot180Deg(BufferedImage ,180);
//pic.repairPicture();
// *******************************************
// end of testarea
// *******************************************
// shows the Picture on the monitor
ShowPicture frm = new ShowPicture(pic.getWidth(), pic.getHeight(),picUrl);
frm.pic= pic.getImage();
frm.repaint();
}
/**
* Contructor to set show-properties
* @param width image-width
* @param height image-height
* @param title title for the picture-window
*/
public ShowPicture(int width, int height, String title)
{
setTitle( "Picture: "+title );
setSize( width, height );
setVisible( true );
this.
addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent ev ) {
dispose();
System.exit( 0 ); } } );
}
/**
* paints the picture
* will be called automatically
*/
public void paint( Graphics g )
{
g.drawImage(pic,0,0, null);
}
}