Bild ausgeben mit Swing

  • Themenstarter Themenstarter Gelöschtes Mitglied 2635
  • Beginndatum Beginndatum
Status
Nicht offen für weitere Antworten.
G

Gelöschtes Mitglied 2635

Gast
Hallo zusammen,

ich habe schon zu dem Thema gesucht und bin auch fündig geworden, allerdings komme ich trotzdem nicht zum Ziel. Ich will mittels Swing Bilder ausgeben, später auch zeichnen. Hier mein bisheriger Lösungsansatz:


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TSwing
	extends JFrame
	implements WindowListener {
	
	private Image img;

	public static void main(String args[]) {
		TSwing test = new TSwing();
	}
	
	public TSwing() {
		
		// Frame einrichten
		JFrame frame = new JFrame("Erstes Swing Programm");
		frame.addWindowListener(this);
		frame.setLocation(100,100);
		frame.setSize(800,600);	
		frame.setVisible(true);
		frame.show();	
		
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		PaintBoard pb = new PaintBoard();		
		contentPane.add(pb, BorderLayout.CENTER);		
	}
	
	public void windowDeactivated(WindowEvent e) { }
	public void windowOpened(WindowEvent e) { }
	public void windowDeiconified(WindowEvent e) { }
	public void windowClosed(WindowEvent e) { }
	public void windowActivated(WindowEvent e) { }
	public void windowIconified(WindowEvent e) { }
	
	public void windowClosing(WindowEvent e) {
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	class PaintBoard
		extends JLabel {
		
		public PaintBoard() {
			super();
		}
		
		public void paintComponent(Graphics g) {			
			img = getToolkit().getImage("images-tk/1.jpg");
			g.drawImage(img,0,0,this);		
		}
		
	}

}


Was habe ich vergessen, bzw. falsch gemacht?
 
Hallo,

mmh weiß jetzt nicht ob's daran liegt aber
Code:
frame.setVisible(true); 
      frame.show();
ruft man eigentlich erst am Ende auf, wenn alle Komponenten fertig zur Oberfläche hinzugefügt wurden...


Dann könnts noch Probleme mit dem "/" geben...
Code:
img = getToolkit().getImage("images-tk/1.jpg");
versuch mal "\\" oder noch besser (wegen Plattformunabhängigkeit) File.seperator...
 
Hab den Code dahingehend geändert, aber es geht leider immernoch nicht. :bahnhof:


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TSwing
	extends JFrame
	implements WindowListener {
	
	private Image img;

	public static void main(String args[]) {
		TSwing test = new TSwing();
	}
	
	public TSwing() {
		
		// Frame einrichten
		JFrame frame = new JFrame("Erstes Swing Programm");
		frame.addWindowListener(this);
		frame.setLocation(100,100);
		frame.setSize(800,600);	
			
		
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		PaintBoard pb = new PaintBoard();		
		contentPane.add(pb, BorderLayout.CENTER);
		
		frame.setVisible(true);
		frame.show();
	}
	
	public void windowDeactivated(WindowEvent e) { }
	public void windowOpened(WindowEvent e) { }
	public void windowDeiconified(WindowEvent e) { }
	public void windowClosed(WindowEvent e) { }
	public void windowActivated(WindowEvent e) { }
	public void windowIconified(WindowEvent e) { }
	
	public void windowClosing(WindowEvent e) {
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	class PaintBoard
		extends JLabel {
		
		public PaintBoard() {
			super();
		}
		
		public void paintComponent(Graphics g) {			
			String s = System.getProperty("file.separator");
			img = getToolkit().getImage("images-tk"+s+"1.jpg");
			g.drawImage(img,0,0,this);			
		}
		
	}

}
 
Lade das Bild lieber einmal im Konstruktor, anstatt jedesmal in der paintComponent. So vermute ich nämlich (abgesehen von der Geschwindigkeit, die das kostet), dass das Bild beim Zeichnen noch nicht fertig geladen ist, die getImage Methode blockt den Aufruf nämlich nicht, bis dies geschehen ist.
Auch wenn ud es im Konstruktor lädtst, solltest du also auf das Bild warten, soche deshalb mal im Forum nach MediaTracker.

Btw fände ich es vllt sinnvoller von JPanel und net von JLabel abzuleiten.
 
Jetzt bin ich doch langsam am verzweifeln. Lade das Bild jetzt im Konstruktor, Hab das Bild jetzt in den gleichen Ordner gepackt, um Probleme mit dem file.separator zu umgehen, leite aus JPanel ab, aber funktionieren tut's tatsächlich immer noch nicht. :autsch:


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TSwing
	extends JFrame
	implements WindowListener {
	
	protected Image img;

	public static void main(String args[]) {
		TSwing test = new TSwing();
	}
	
	public TSwing() {
		
		// Frame einrichten
		JFrame frame = new JFrame("Erstes Swing Programm");
		frame.addWindowListener(this);
		frame.setLocation(100,100);
		frame.setSize(800,600);	
		
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		PaintBoard pb = new PaintBoard();		
		contentPane.add(pb, BorderLayout.PAGE_START);
		
		frame.setVisible(true);
		frame.show();
	}
	
	public void windowDeactivated(WindowEvent e) { }
	public void windowOpened(WindowEvent e) { }
	public void windowDeiconified(WindowEvent e) { }
	public void windowClosed(WindowEvent e) { }
	public void windowActivated(WindowEvent e) { }
	public void windowIconified(WindowEvent e) { }
	
	public void windowClosing(WindowEvent e) {
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	class PaintBoard
		extends JPanel {
		
		public PaintBoard() {
			super();
			//String s = System.getProperty("file.separator");
			img = Toolkit.getDefaultToolkit().getImage("1.jpg");
		}
		
		public void paintComponent(Graphics g) {			
			g.drawImage(img,0,0,this);		
		}
		
	}

}
 
Du hast da zwei Fenster, einmal das TSwing, also deine Klasse, und einmal den JFrame, den du da im Konstruktor erzeugst. Du machst den anderen sichtbar, fügst das Bild aber auf this hinzu.

Mach also sowas:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TSwing
   extends JFrame
   implements WindowListener {


   public static void main(String args[]) {
      TSwing test = new TSwing();
   }

   public TSwing() {

      // Frame einrichten
      super("Erstes Swing Programm");
      addWindowListener(this);
      setLocation(100,100);
      setSize(800,600);

      Container contentPane = getContentPane();
      contentPane.setLayout(new BorderLayout());

      PaintBoard pb = new PaintBoard();
      contentPane.add("Center", pb);

      setVisible(true);
   }

   public void windowDeactivated(WindowEvent e) { }
   public void windowOpened(WindowEvent e) { }
   public void windowDeiconified(WindowEvent e) { }
   public void windowClosed(WindowEvent e) { }
   public void windowActivated(WindowEvent e) { }
   public void windowIconified(WindowEvent e) { }

   public void windowClosing(WindowEvent e) {
      setVisible(false);
      dispose();
      System.exit(0);
   }

   class PaintBoard
      extends JPanel {

      protected Image img;
      public PaintBoard() {
         super();
         //String s = System.getProperty("file.separator");
         img = Toolkit.getDefaultToolkit().getImage("1.jpg");
         MediaTracker mt = new MediaTracker (this);
         mt.addImage(img, 0);
         try{
           mt.waitForID(0);
         }catch (Exception e){ //this should never happen
           e.printStackTrace();
         }
      }

      public void paintComponent(Graphics g) {
         g.drawImage(img,0,0,this);
      }

   }

}
 
Das Problem für ein Bild wäre geklärt. Jetzt habe ich versucht eine Folge von Bildern auszugeben. Hab also 4 Bilder in einem Array zusammengefasst, checke mittels MediaTracker, ob Sie auch komplett geladen wurden, und dennoch läuft die Bildfolge nicht einwandfrei. Nicht nach jedem repaint() zeigt er mir das folgende Bild, und überhaupt zeigt er komischer Weise nur 2 der 4 Bilder an. Die Bilder sind alle gleich groß und haben ca. 50 kb. Komisch ist auch, dass er immer das erste und letzte Bild anzeigt, in unregelmäßiger Reihenfolge. ???:L


Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TSwing
	extends JFrame
	implements WindowListener {
	
	protected Image[] images;

	public static void main(String args[]) {
		TSwing test = new TSwing();
	}
	
	public TSwing() {
		super("Erstes Swing Programm");
		addWindowListener(this);
		setLocation(100,100);
		setSize(640,480);	
		
		Container contentPane = getContentPane();
		contentPane.setLayout(new BorderLayout());
		
		PaintBoard pb = new PaintBoard();		
		contentPane.add(pb, BorderLayout.CENTER);
		
		setVisible(true);
	}
	
	public void windowDeactivated(WindowEvent e) { }
	public void windowOpened(WindowEvent e) { }
	public void windowDeiconified(WindowEvent e) { }
	public void windowClosed(WindowEvent e) { }
	public void windowActivated(WindowEvent e) { }
	public void windowIconified(WindowEvent e) { }
	
	public void windowClosing(WindowEvent e) {
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	class PaintBoard
		extends JPanel {
		
		public PaintBoard() {
			super();
			String s = System.getProperty("file.separator");
			images = new Image[4];
			images[0] = getToolkit().getImage("images-tk"+s+"1.jpg");
			images[1] = getToolkit().getImage("images-tk"+s+"2.jpg");
			images[2] = getToolkit().getImage("images-tk"+s+"3.jpg");
			images[3] = getToolkit().getImage("images-tk"+s+"4.jpg");
			
			MediaTracker mt = new MediaTracker(this);
			for (int i = 0; i < 4; i++) {
				mt.addImage(images[i],i);
				try {
					mt.waitForID(i);
				} catch (InterruptedException ex) {
					
				}
			}
		}
		
		public void paintComponent(Graphics g) {			
			for (int i = 0; i < 4; i++) {
				g.drawImage(images[i],0,0,this);
				repaint();
				try {
					Thread.sleep(1000);
				} catch(Exception ex) 	{
					ex.printStackTrace();
				}
				
			}			
			
		}
		
	}

}
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben