Du hast das schon alles richtig erkannt.... Das Bild wird geladen und ich will darauf malen...
Die Klasse ShapesDraw hab ich weggelassen. Dort werden Mausevents etc abgefragt und Zeichenobjekte wie Box, Line etc. erstellt.
Es muss aber eh irgendwie an der Farbe liegen. Es scheint als vermischen sich die DrawingColor und der Hintergrund miteinander....
Das mit dem Casten ist nicht nötig, da es bereits vom Typ Graphic2D ist
public class Editor extends JComponent {
protected GrafikBild2D grBild;
protected ShapesDraw shapesDraw;
public Editor(){
init();
}
protected void init() {
this.setLayout(new BorderLayout());
grBild = new GrafikBild2D(uebungDAO);
shapesDraw = new ShapesDraw(grBild);
this.add(new MenuEditor().getJMenuBar(), BorderLayout.NORTH);
this..add(grBild, BorderLayout.CENTER);
this.show();
this.repaint();
}
class MeineGraphik{
protected Graphics2D scrGraphic;
public MeineGraphik(){}
// creats a new BufferedImage with RGB color
public void createMyNewImage(int wid, int ht){
int width = wid;
int height = ht;
// determine thumbnail size from WIDTH and HEIGHT
thumbWidth = width;
thumbHeight = height;
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
Image image = Toolkit.getDefaultToolkit().getImage("Universität.jpg");
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
thumbHeight = (int) (thumbWidth / imageRatio);
}
else {
thumbWidth = (int) (thumbHeight * imageRatio);
}
scrImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
scrGraphic = scrImage.createGraphics();
scrGraphic.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
scrGraphic.setColor(Color.WHITE);
//scrGraphic.setBackground(Color.BLACK);
scrGraphic.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D 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)){
createMyNewImage(bounds.width, bounds.height);
}
// draws the shape depending on the new position of the mouse
g2d.drawImage(scrImage, 0,0,thumbWidth, thumbHeight, this);
}
}