Bildgröße anpassen

Status
Nicht offen für weitere Antworten.

mathon

Bekanntes Mitglied
Hi,

Ich habe eine Anwendung, dass ich Bilder in ein ImageLabel lade, die nach der Reihe angezeigt werden. Das Problem ist jetzt, dass ich verschiedene Modi von Bildübergänge verwende, jedoch wenn ich den Modi für einen Bildübergang wähle, dann wird wenn ich während der Slideshow die Fenstergröße verändere, das Bild nicht mehr entsprechend angepasst. Wenn ich keinen Übergang wähle und die Bilder anzeigen lasse, funktioniert das verändern der Fenstergröße.

Code:
public class ImageLabel extends JLabel implements Runnable
{
	private ImageIcon icon;
	private Image theOldImage;
	private Image theNewImage;
	private int x = 0;
	private int y = 0;
	private int w = 0;
	private int h = 0;
	private int width = 0;
	private int height = 0;
	
	public ImageLabel()
	{
		super();
	}
	
	public void setIcon(ImageIcon icon) {
		this.icon = icon;
	}
	
	public void paint( Graphics g )
	{
		super.paintComponent( g ); // should always call this method
		Dimension d = getSize();
		//the icon is directly used when the TRANSITION_MODE is set to "WithoutTransition"
		if(icon != null)
		{

			double windowWidth = this.getWidth();
			double windowHeight = this.getHeight();
			double pictureWidth = icon.getIconWidth();
			double pictureHeight = icon.getIconHeight();
			double windowRatio = windowHeight/windowWidth;
			double pictureRatio = pictureHeight/pictureWidth;
			int drawWidth;
			int drawHeight;
			if(pictureRatio > windowRatio){
				drawHeight = (int)windowHeight;
				drawWidth = (int)(drawHeight/pictureRatio);
			}else{
				drawWidth = (int)windowWidth;
				drawHeight = (int)(drawWidth*pictureRatio);
			}
			
			//center
			int x = (int)(windowWidth-drawWidth)/2;
			int y = (int)(windowHeight-drawHeight)/2;
			
			d.setSize(drawWidth,drawHeight);
			g.drawImage( icon.getImage(), x, y, d.width, d.height, null );
		}
		
		if(TransitionDialog.TRANSITION_MODE == "VanishToCorner")
		{
			if( theNewImage != null )
			{
				g.drawImage( theNewImage, 0, 0, width, height, null );
			}
			if( theOldImage != null )
			{
				g.drawImage( theOldImage, x, y, w, h, null );
			}
			
		}
		else
		{
			if( theOldImage != null )
			{
				g.drawImage( theOldImage, 0, 0, width, height, null );
			}
			if( theNewImage != null )
			{
				g.drawImage( theNewImage, x, y, w, h, null );
			}
		}
	}
	
	
	// Need this method in order to load an image for display
	public void setImage( Image i )
	{
		theOldImage = theNewImage; 
		theNewImage = i;
	}

	public void changeImage()
	{
		
		Thread t = new Thread( this );
		t.start();
	}
	
	public void run()
	{
		Dimension d = this.getSize();
		width = d.width;
		height = d.height;
		
		if(TransitionDialog.TRANSITION_MODE == "SlideFromRight")
		{
			x = width;
			y = 0;
			w = width;
			h = height;
				
			do
			{
				x-=2;
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(InterruptedException ex)
				{}
			
			} while( x > 0 );
		}
		else if(TransitionDialog.TRANSITION_MODE == "ExpandFromRight")
		{
			x = width;
			y = 0;
			w = 0;
			h = height;
					
			do
			{
				x-=2;
				w+=2;
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(InterruptedException ex)
				{}
			} while( x > 0 );
		}
		else if(TransitionDialog.TRANSITION_MODE == "VanishToCorner")
		{
			x = 0;
			y = 0;
			w = width;
			h = height;
			
			double wInc = width/200;
			double hInc = height/200;
			
			do
			{
				x = (int)(x + wInc);
				y = (int)(y + hInc);
				w = (int)(w - wInc);
				h = (int)(h - hInc);		
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(Exception ex){}
			} while( x < width );
		}
		else if(TransitionDialog.TRANSITION_MODE == "ExpandOutwards")
		{
			x = width/2;
			y = height/2;
			w = 0;
			h = 0;
			
			double wInc = width/200;
			double hInc = height/200;
			
			do
			{
				x = (int)(x - wInc);
				y = (int)(y - hInc);
				w = (int)(w + 2*wInc);
				h = (int)(h + 2*hInc);		
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(InterruptedException ex){}
			} while( x > 0 );
		}
	}
	
	public void update(Graphics g)
	{	
		paint(g);
	}
}

Weiß vielleicht jemand, wie ich das definieren muss, damit die Bilder wenn ich einen Bildübergang verwende, bei Veränderung der Fenstergröße korrekt an die Größe angepasst werden?? :bahnhof:

danke im voraus

lg matti
 

André Uhres

Top Contributor
Warum zum Teufel macht fast niemand ein KSKB?
Das raubt mir noch den letzten Nerv!
<--------------------
Code:
//package paint;
/*
 * ImageLabel_KSKB.java
 */
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class ImageLabel_KSKB extends JFrame {
    public ImageLabel_KSKB() {
        super("ImageLabel KSKB");
        setSize(400,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        imgBtn = new JButton("Next");
        imgBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                imgBtnActionPerformed(evt);
            }
        });
        getContentPane().add(imgBtn, BorderLayout.NORTH);
        imageLabel = new ImageLabel();
        System.out.println("Please wait while loading pictures...");
        String img = "http://today.java.net/jag/bio/JagHeadshot-small.jpg";
        System.out.println("...loading " + img);
        try{ url = new URL(img);
        }catch(Exception ex){ex.printStackTrace();}
        ico1 = new ImageIcon(url);
        img = "http://today.java.net/jag/bio/JAG2001small.jpg";
        System.out.println("...loading " + img);
        try{url = new URL(img);
        }catch(Exception ex){ex.printStackTrace();}
        ico2 = new ImageIcon(url);
        System.out.println("...OK");
        imageLabel.setIcon(ico1);
        getContentPane().add(imageLabel, BorderLayout.CENTER);
    }
    private void imgBtnActionPerformed(ActionEvent evt) {
        ImageIcon ico = ico1;
        sw = !sw;
        if(sw) ico = ico2;
        imageLabel.setIcon(ico);
    }
    public static void main(String args[]) {new ImageLabel_KSKB().setVisible(true);}
    private ImageIcon ico1, ico2;
    private ImageLabel imageLabel;
    private URL url;
    private boolean sw;
    private JButton imgBtn;
}
class ImageLabel extends JLabel implements Runnable {
    private ImageIcon icon;
    private Image theOldImage;
    private int x, y, w, h, xNew, yNew, wNew, hNew;
    public ImageLabel() {
        super();
    }
    public void setIcon(ImageIcon icon) {
        if(this.icon != null){
            theOldImage = this.icon.getImage();
            x = xNew;
            y = yNew;
            w = wNew;
            h = hNew;
        }
        this.icon = icon;
        changeImage();
    }
    public void paintComponent( Graphics g ) {
        super.paintComponent( g ); // should always call this method
        Dimension d = getSize();
        //the icon is directly used when the TRANSITION_MODE is set to "WithoutTransition"
        if(icon != null) {
            double windowWidth, windowHeight, pictureWidth, pictureHeight, windowRatio, pictureRatio;
            int drawWidth, drawHeight;
            windowWidth = this.getWidth();
            windowHeight = this.getHeight();
            pictureWidth = icon.getIconWidth();
            pictureHeight = icon.getIconHeight();
            windowRatio = windowHeight/windowWidth;
            pictureRatio = pictureHeight/pictureWidth;
            if(pictureRatio > windowRatio){
                drawHeight = (int)windowHeight;
                drawWidth = (int)(drawHeight/pictureRatio);
            }else{
                drawWidth = (int)windowWidth;
                drawHeight = (int)(drawWidth*pictureRatio);
            }
            //center
            xNew = (int)(windowWidth-drawWidth)/2;
            yNew = (int)(windowHeight-drawHeight)/2;
            d.setSize(drawWidth,drawHeight);
            wNew = d.width;
            hNew = d.height;
            g.drawImage( icon.getImage(), xNew, yNew, wNew, hNew, null );
        }
        if( theOldImage != null ) {
            g.drawImage( theOldImage, x, y, w, h, null );
        }
    }
    private void changeImage() {
        Thread t = new Thread( this );
        t.start();
    }
    public void run() {
        double wInc = w/200;
        double hInc = h/200;
        if(wInc==0)wInc=1;
        if(hInc==0)hInc=1;
        do{
            x = (int)(x + wInc);
            w = (int)(w - wInc);
            y = (int)(y + hInc);
            h = (int)(h - hInc);
            if(w < 0) w = 0;
            repaint();
            try {Thread.sleep(1);} catch(Exception ex){}
        } while( h >= 0);
        theOldImage = null;
        repaint();
    }
}
 

mathon

Bekanntes Mitglied
Hi,

vielen Dank erstmals für das Codebeispiel. Ich habe jetzt mein ImageLabel mal angepasst:

Code:
public class ImageLabel extends JLabel implements Runnable
{
	private ImageIcon icon;
	private Image theOldImage;
	private Image theNewImage;
	private int x = 0;
	private int y = 0;
	private int w = 0;
	private int h = 0;
	private int width = 0;
	private int height = 0;
	private int xNew, yNew, wNew, hNew; 
	
	public ImageLabel()
	{
		super();
	}
	
	public void setIcon(ImageIcon icon) {
		 if(this.icon != null){
            theOldImage = this.icon.getImage();
            x = xNew;
            y = yNew;
            w = wNew;
            h = hNew;
        }
        this.icon = icon;
        changeImage(); 
	}
	
	public void paintComponent( Graphics g )
	{
		super.paintComponent( g ); // should always call this method
		Dimension d = getSize();
		//the icon is directly used when the TRANSITION_MODE is set to "WithoutTransition"
		if(icon != null)
		{

			double windowWidth = this.getWidth();
			double windowHeight = this.getHeight();
			double pictureWidth = icon.getIconWidth();
			double pictureHeight = icon.getIconHeight();
			double windowRatio = windowHeight/windowWidth;
			double pictureRatio = pictureHeight/pictureWidth;
			int drawWidth;
			int drawHeight;
			if(pictureRatio > windowRatio){
				drawHeight = (int)windowHeight;
				drawWidth = (int)(drawHeight/pictureRatio);
			}else{
				drawWidth = (int)windowWidth;
				drawHeight = (int)(drawWidth*pictureRatio);
			}
			
			//center
			xNew = (int)(windowWidth-drawWidth)/2;
			yNew = (int)(windowHeight-drawHeight)/2;
			wNew = d.width;
            hNew = d.height; 
			
			d.setSize(drawWidth,drawHeight);
			g.drawImage( icon.getImage(), xNew, yNew, wNew, hNew, null );
		}
		if( theOldImage != null ) {
            g.drawImage( theOldImage, x, y, w, h, null );
        } 
		
		if(TransitionDialog.TRANSITION_MODE == "VanishToCorner")
		{
			if( theNewImage != null )
			{
				g.drawImage( theNewImage, 0, 0, width, height, null );
			}
			if( theOldImage != null )
			{
				g.drawImage( theOldImage, x, y, w, h, null );
			}
			
		}
		else
		{
			if( theOldImage != null )
			{
				g.drawImage( theOldImage, 0, 0, width, height, null );
			}
			if( theNewImage != null )
			{
				g.drawImage( theNewImage, x, y, w, h, null );
			}
		}
	}
	
	
	// Need this method in order to load an image for display
	public void setImage( Image i )
	{
		theOldImage = theNewImage; 
		theNewImage = i;
	}

	public void changeImage()
	{
		
		Thread t = new Thread( this );
		t.start();
	}
	
	public void run()
	{
		Dimension d = this.getSize();
		width = d.width;
		height = d.height;
		
		if(TransitionDialog.TRANSITION_MODE == "SlideFromRight")
		{
			x = width;
			y = 0;
			w = width;
			h = height;
				
			do
			{
				x-=2;
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(InterruptedException ex)
				{}
			
			} while( x > 0 );
		}
		else if(TransitionDialog.TRANSITION_MODE == "ExpandFromRight")
		{
			x = width;
			y = 0;
			w = 0;
			h = height;
					
			do
			{
				x-=2;
				w+=2;
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(InterruptedException ex)
				{}
			} while( x > 0 );
		}
		else if(TransitionDialog.TRANSITION_MODE == "VanishToCorner")
		{
			x = 0;
			y = 0;
			w = width;
			h = height;
			
			double wInc = w/200;
	        double hInc = h/200;
	        if(wInc==0)wInc=1;
	        if(hInc==0)hInc=1;
	        do{
	            x = (int)(x + wInc);
	            w = (int)(w - wInc);
	            y = (int)(y + hInc);
	            h = (int)(h - hInc);
	            if(w < 0) w = 0;
	            repaint();
	            try {Thread.sleep(1);} catch(Exception ex){}
	        } while( h >= 0);
	        theOldImage = null;
	        repaint(); 
		}
		else if(TransitionDialog.TRANSITION_MODE == "ExpandOutwards")
		{
			x = width/2;
			y = height/2;
			w = 0;
			h = 0;
			
			double wInc = width/200;
			double hInc = height/200;
			
			do
			{
				x = (int)(x - wInc);
				y = (int)(y - hInc);
				w = (int)(w + 2*wInc);
				h = (int)(h + 2*hInc);		
				this.repaint();
				try
				{
					Thread.sleep(1);
				}
				catch(InterruptedException ex){}
			} while( x > 0 );
		}
	}
	
}

In meinem Thread wo die einzelnen Bilder durchgegangen und angezeigt werden wird halt immer beim Wechsel die changeImage Methode des ImageLabels aufgerufen. Leider ist es jetzt aber noch so, dass das Bild immer nur angepasst wird, wenn ein neues Image angezeigt wird und nicht währenddessen zb. ein Bild nach rechts unten minimiert wird. War das von dir so gedacht oder habe ich da noch einen Fehler im Code? :bahnhof:

lg matti
 

André Uhres

Top Contributor
mathon hat gesagt.:
...Leider ist es jetzt aber noch so, dass das Bild immer nur angepasst wird, wenn ein neues Image angezeigt wird und nicht währenddessen zb. ein Bild nach rechts unten minimiert wird...
Erstmal ist das Problem ja wohl ein wenig mit den Haaren herbeigezogen:
niemand tut sowas absichtlich, und wenn es mal passiert ist es ja sowieso nur für einen kurzen Augenblick.
Ausserdem gilt dein Code zur Grössenanpassung in paintComponent() ja auch nur für das neue Bild icon und
nicht für theOldImage. Das bedeutet: während die Animation läuft wird jedenfalls das animierte Bild theOldImage
nicht angepasst, sondern nur das neue Bild icon .
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
D Methoden Methoden anpassen und fehlende Funktionen hinzufügen Allgemeine Java-Themen 475
D Swing MaskFormatter Maske Variabel anpassen Allgemeine Java-Themen 2
B Altes Applet anpassen Allgemeine Java-Themen 8
T jfreechart Linien/Punkttyp anpassen Allgemeine Java-Themen 1
B Umgebungsvariable Anpassen der Umgebungsvariablen nach Java-Update ? Allgemeine Java-Themen 14
I JPanel soll sich dem JFrame anpassen Allgemeine Java-Themen 1
M Array "Größe" anpassen Allgemeine Java-Themen 2
L Methoden methoden an generischen klassentyp anpassen Allgemeine Java-Themen 5
X Windows-Environment-Variable per jRegistryKey anpassen Allgemeine Java-Themen 6
S JFrame an Fenstergröße anpassen Allgemeine Java-Themen 16
S Arraygröße anpassen Allgemeine Java-Themen 6
F JFreeChart Größe anpassen Allgemeine Java-Themen 8
L Datentypen Datenmodell anpassen? Allgemeine Java-Themen 7
K JFreeChart - Einträge in DomainAxis anpassen Allgemeine Java-Themen 2
N Jlabel automatisch anpassen Allgemeine Java-Themen 3
H JDateChooser anpassen Allgemeine Java-Themen 4
F KeyEvents anpassen? Allgemeine Java-Themen 4
J Itext , Spaltenbreite automatisch anpassen ? Allgemeine Java-Themen 4
M JFrame anpassen Allgemeine Java-Themen 4
S spielgeschwindigkeit an rechenzeit anpassen Allgemeine Java-Themen 31
N JTable & JScrollPane - Wie den Header anpassen? Allgemeine Java-Themen 4
R Größe der JRE anpassen Allgemeine Java-Themen 18
M JLabel an Textlänge anpassen / Textlänge in pixel Allgemeine Java-Themen 3

Ähnliche Java Themen

Neue Themen


Oben