public class GrafikBild2D extends JComponent implements MyGraphicDisplay {
protected BufferedImage scrImage = null;
protected BufferedImage scrWhiteImage = null;
protected Graphics2D scrGraphic = null;
protected Graphics2D scrWhiteGraphics = null;
protected Graphics2D scrBackgroundGraphic = null;
protected Graphics2D scrBackgroundWhiteGraphics = null;
protected Image image = null;
protected Image whiteImage = null;
protected BufferedImage backgroundImage = null;
protected BufferedImage backgroundWhiteImage = null;
public GrafikBild2D(UebungDAO uebungDAO) {
System.out.println("GrafikBild2D");
this.uebungDAO = uebungDAO;
}
// overwrites the paint-Method
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics g2d = (Graphics2D)g;
Rectangle bounds = getBounds();
// the offScreenImage has to be created to begin
if((width <=0) || (height <=0) ||
(width != bounds.width) ||
(height != bounds.height)){
createScreenImage(bounds.width, bounds.height);
width = bounds.width; // unbedingt nötig, sonst wird nicht gezeichnet
height = bounds.height;
g2d.drawImage(scrImage, 0, 0, imageWidth, imageHeight, this);
}
else{
// draws the shape depending on the new position of the mouse
g2d.drawImage(scrImage, 0, 0, imageWidth, imageHeight, this);
}
}
}
catch(Exception e){ }
}
// creats a new BufferedImage with RGB color
public void createScreenImage(int wid, int ht){
try{
if (uebungDAO.getGrafik() != null ) {
// determine thumbnail size from WIDTH and HEIGHT
//thumbWidth = width;
//thumbHeight = height;
//double thumbRatio = (double) thumbWidth / (double) thumbHeight;
image = Toolkit.getDefaultToolkit().getImage(uebungDAO.getDefaultGrafik());
whiteImage = Toolkit.getDefaultToolkit().getImage(uebungDAO.getDefaultWeisseGrafik());
// MediaTracker damit das Bild erst geladen wird
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
mt.addImage(whiteImage, 1);
try {
// Warte bis das Image geladen ist
mt.waitForAll();
}
catch (InterruptedException ee) {
// tue nichts
}
mt = null;
imageWidth = image.getWidth(null);
imageHeight = image.getHeight(null);
scrImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_BGR);
scrGraphic = scrImage.createGraphics();
scrGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
scrGraphic.setColor(drawingColor);
scrGraphic.drawImage(image, 0, 0, imageWidth, imageHeight, null);
scrWhiteImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
scrWhiteGraphics = scrWhiteImage.createGraphics();
scrWhiteGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.
VALUE_INTERPOLATION_BILINEAR);
scrWhiteGraphics.setColor(drawingColor);
scrWhiteGraphics.drawImage(whiteImage, 0, 0, imageWidth, imageHeight, null);
//*****same for the BackroundGraphic*****
backgroundImage = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_BGR);
scrBackgroundGraphic = backgroundImage.createGraphics();
scrBackgroundGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
scrBackgroundGraphic.setColor(drawingColor);
scrBackgroundGraphic.drawImage(image, 0, 0, imageWidth, imageHeight, null);
backgroundWhiteImage = new BufferedImage(imageWidth, imageHeight,
BufferedImage.TYPE_INT_RGB);
scrBackgroundWhiteGraphics = backgroundWhiteImage.createGraphics();
scrBackgroundWhiteGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.
VALUE_INTERPOLATION_BILINEAR);
scrBackgroundWhiteGraphics.setColor(drawingColor);
scrBackgroundWhiteGraphics.drawImage(whiteImage, 0, 0, imageWidth, imageHeight, null);
}