Grafik laden

jorgeHX

Erfahrenes Mitglied
hallo zusammen,
ich hab mal wieder ein keines Problemchen...
Folgendes...

Ich lade eine Grafik, die wesentlich größer ist als mein Frame. Dennoch möchte ich diese Grafik komprimiert in meinem BorderLayout zentriet und vollständig anzeigen. Ist das irgendwie möglich?

Es soll so funktionieren, wie wenn man bei Windows den Hintergrund auf zentriert einstellt und das gesamte Bild genau auf den Hintergrund passt.
Ich hoffe es weiß jemand Rat...

Merci
 
Hallo!

Versuchs doch mal mit ...

Code:
BufferedImage bla = new BufferedImage(.....);
//Odetr sonst irgendwoher ein Image Objekt bekommen...

//Hints aus der Image Klasse:
//SCALE_DEFAULT, SCALE_FAST, SCALE_SMOOTH, SCALE_REPLICATE, SCALE_AREA_AVERAGING


int width = 320;
int height = 240;
int hints = BufferedImage.SCALE_SMOOTH

Image scaledImage = bla.getScaledInstance(width, height, hints);

//hier hast du nun dein skaliertes Bildschen mit dem du machen kannst was du willst.

Gruß Tom
 
Danke Thomas,
ich habe es schon mit der Methode setRenderingHint() hinbekommen. Gott sei Dank :)

Vielleicht kannste mir aber verraten, warum ich nicht richtig zeichne kann?
ich lade das Image und will dann mit einer Farbe zeichnen.
Color drawingColor = Color.WHITE;

Leider malt er aber nicht weiß, wenn der Hintergrund nicht schwarz ist. Komisch aber wahr.
Haste darauf vielleicht ne Antwort?

Danke
 
Mach mal langsam, mache von uns haben auch noch was anderes zu tun ;-)

Poste mal zu deinem Problem relevanten Quellcode und dann sehen wir mal weiter...

Gruß Tom
 
Moin, also ich poste hier mal zwei Methoden, in der ich das Hintergrundbild lade.
Die Methoden befinden sich in der Klasse MeineGraphik2D.java

In der Klasse Start.java erzeuge ich ein Objekt der Klasse MeineGraphik2D und füge es anschließend der ContentPane im Layout.CENTER hinzu.
Das klappt auch alles wunderbar. Zeichnen etc. läuft auch, nur mixt sich die Farbe immer.... Ich hoffe du weißt was. Und danke für die Zeit....

class MeineGraphik{

.....

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);
}
}
 
Hallo!

Irgendwie komm ich mit deiner Beschreibung nicht ganz klar,
du willst in einem JPanel ein Bild anzeigen lassen.
Dieses Bild willst du nun vorher aber noch Skalieren (erledigt)
und was willst du dann tun, darauf zeichnen?
Mach doch mal ein minimales Lauffähiges Beispiel, damit ich das ganze schnell nachvollziehen kann.
Dein letztes Beispiel reicht nicht ...

Aber versuch mal folgendes: Caste mal das scrGraphic auf Graphics2D bevor du die setColor Methoden aufrufst...

Gruß Tom
 
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);
}
}
 

Neue Beiträge

Zurück