Änderndes Bild im JPanel

mashine-obf

Mitglied
ich möchte in im jpanel (im mittleren Rechteck)ein bild ausgeben (_ImgBild), das sich im sekunden takt änder mit der farbe ändert
bekomme es aber nicht ausgegeben bitte um hilfe danke
Code:
/*

package gui;
import java.awt.*;
import javax.swing.*;

public class ColorFrameMitImage extends JFrame {
	JPanel _panel = null;
	Color  _colRed = null;
	Color  _colGreen = null;
	Color  _colBlue = null;
	ImageIcon _ImgBild = null;
	public class ColorThread extends Thread {
	
		public void run() {
			for ( int i = 1; i < 256; i += 1 ) {
				try {
				 _ImgBild = new ImageIcon("c:\\"+i+".jpg");
					_colRed = new Color(i,0,0);
					_colGreen = new Color(0,i,0);
					_colBlue = new Color(0,0,i);
					_panel.repaint();
					sleep(1);
				} catch (InterruptedException e) {}
			}
			System.out.println("Fertig");
		}
	}
	
	public class ColorPanel extends JPanel {
		
		public ColorPanel() {
			//setDoubleBuffered(false);
		}
		
		public void paintComponent(Graphics graphics) {
			super.paintComponent(graphics);
			
			int width   = getWidth();
			int height  = getHeight();
			Graphics g = getGraphics();
			g.setColor(_colRed);
			g.fillRect(0,0,width,height);
			if ( width > 100 && height > 100 )
			{
				g.setColor(_colGreen);
				g.fillRect(50,50,width-100,height-100);
			}
			if ( width > 200 && height > 200 )
			{
				g.setColor(_colBlue);
				g.fillRect(100,100,width-200,height-200);
			}
//  		  graphics.drawImage(_ImgBild,0,0,this);
		}
	}
	
	public ColorFrameMitImage() {
		_panel = new ColorPanel();
		getContentPane().add(_panel,BorderLayout.CENTER);
	   
		setTitle("Frame mit wechselnder Farbe");
		setSize(600, 600);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		new ColorThread().start();
	}
	
	public static void main(String[] args) {
		new ColorFrameMitImage();
	}
}
 
vielen dank schonmal !in 10 jahren werd ich dir auch mal helfen können ;-] hier ist mein zweiter versuch bekomme aber nur das 1.bild ausgegeben der thread läuft durch aber aktualisiert das label nicht
Code:
package gui;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
/**
Autor mashine
 * Ein Frame mit einem Panel, das die Farbe ändert
 */
public class ColorFrameMitImage extends JFrame {
	ImageIcon _ImgBild = null;
	private int i =1;
	private Container _contentPane = null;
	private JLabel _lblZahl1 = null;
	
	public ColorFrameMitImage() {
	 new ColorThread().start();
	 _contentPane = getContentPane();
		_contentPane.setLayout(new FlowLayout());
		
		updateLabel(i);
		_contentPane.add(_lblZahl1);
		setTitle("Frame mit wechselnder Farbe");
		pack();
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	  
	}
	
   
	private void updateLabel(int i)
	{
		_lblZahl1= new JLabel(new ImageIcon("c:\\"+ i +".jpg"));
		_contentPane.add(_lblZahl1);
	}
	
	
	public class ColorThread extends Thread {
	
		public void run() {
			for ( int i = 1; i < 21; i += 1 ) {
				try {
//				 _ImgBild = new ImageIcon("c:\\"+i+".jpg");
					sleep(300);
					updateLabel(i);
				} catch (InterruptedException e) {}
			}
			System.out.println("Fertig");
		}
	}
	
  
	public static void main(String[] args) {
		new ColorFrameMitImage();
	}
}
 
mashine-obf hat gesagt.:
vielen dank schonmal !in 10 jahren werd ich dir auch mal helfen können ;-] hier ist mein zweiter versuch bekomme aber nur das 1.bild ausgegeben der thread läuft durch aber aktualisiert das label nicht

Moin,
Du musst auch entsprechend das Icon setzen. Hier meine kleinen Änderungen, mit denen es bei mir einwandfrei funktioniert.

Code:
public class ColorFrameMitImage extends JFrame
{
  ImageIcon         _ImgBild     = null;

  private int       i            = 1;

  private Container _contentPane = null;

  private JLabel    _lblZahl1    = new JLabel();

  public ColorFrameMitImage()
  {
    new ColorThread().start();
    _contentPane = getContentPane();
    _contentPane.setLayout( new FlowLayout() );

    updateLabel( i );
    _contentPane.add( _lblZahl1 );
    setTitle( "Frame mit wechselnder Farbe" );
    setSize(800, 600);
    setVisible( true );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

  }

  private void updateLabel(int i)
  {
    _lblZahl1.setIcon( new ImageIcon( "c:\\" + i + ".jpg" ) );
    repaint();
  }

  public class ColorThread extends Thread
  {

    public void run()
    {
      for (int i = 1; i < 21; i += 1)
      {
        try
        {
          //				 _ImgBild = new ImageIcon("c:\\"+i+".jpg");
          sleep( 300 );
          updateLabel( i );
        }
        catch (InterruptedException e)
        {
        }
      }
      System.out.println( "Fertig" );
    }
  }

  public static void main(String[] args)
  {
    new ColorFrameMitImage();
  }
}
 
viiieeelllen dank hatte immer set ImageIcon probiert aber nie geklapp jetzt weiß ich warum :)
aber kannst du mir erklären warum ich meinem label nicht den wert null zuweisen darf

ps: hast du noch kleine und leichte programme die du mir mal senden kannst damit ich etwas weiter komme?bitte
 
mashine-obf hat gesagt.:
viiieeelllen dank hatte immer set ImageIcon probiert aber nie geklapp jetzt weiß ich warum :)
aber kannst du mir erklären warum ich meinem label nicht den wert null zuweisen darf

Darfst Du schon, aber Du hast es etwas zweckentfremdet.
In der actionPerformed hast Du immer wieder mit new JLabel(...) neue Label erzeugt und damit Speicher verpulvert. Das ist nicht notwendig. Ebenso ist das ständige contentPane.add(...) ist eine Fehlkonstruktion.

ps: hast du noch kleine und leichte programme die du mir mal senden kannst damit ich etwas weiter komme?bitte

Nö. Aber kannst Dir ja selbst was überlegen oder in den Java-Foren die Fragen durchschauen und dann probieren, ob Du das selbst gelöst bekommst - vorausgesetzt, die Fragen sind noch recht einfach.
 
Zurück