Graphics Objekt in Zeitschleife zeichnen

CarlCosinus

Mitglied
Hallo liebe Leute!

Ich habe vor kurzem angefangen mich mal wieder mit Java zu beschäftigen aufgrund meines Fachpraktikums was ich gerade mache.

Kurz was zu meiner Aufgabe. Ich hab ein Programm geschrieben, was die Zeiten misst und Stückzahlen aufzählt. Dazu will ich eine Art Zeitbalken abbilden, der , wie der Name schon sagt, mit der Zeit läuft. Er soll von grün nach rot gehen und somit als "Hinweis" für die Mitarbeiter dienen, dass sie nicht mehr in der Zeit liegen. Das erstmal zur Aufgabe.

Ich habe den Balken bereits erfolgreich in einen Test-Projekt darstellen können, genauso wie ich es will, jedoch hapert es jetzt bei der Implementierung im richtigen Projekt.

Hier erstmal der Code für meine Test-Klasse, um nochmal zu verdeutlichen, wie es denn aussehen soll:

Java:
import java.awt.*;

import javax.swing.*;

import java.util.TimerTask;
import java.util.Timer;


public class FarbBalkenTest extends JPanel {
	
	static Timer timer = new Timer ();
	static TimerTask task ;
	static int time;
	static Graphics G ;
	
	public static void main(String[] args) {
		FarbBalkenTest fbt = new FarbBalkenTest();
		fbt.los();

	}
	
	
	public void los (){
		
		final JFrame frame = new JFrame();
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		final ZeichenTest zeichentest = new ZeichenTest();
		
		frame.getContentPane().add(BorderLayout.CENTER, zeichentest);
		frame.setSize(300,300);
		frame.setVisible(true);
		
		task = new TimerTask(){
			public void run(){
				frame.repaint();
				time+= 20;
				System.out.println(time);
			}
		};
		timer.scheduleAtFixedRate(task, 0, 1000);
		
	}
	public class ZeichenTest extends JComponent  {
		
		public void paintComponent (Graphics g){
				
				Graphics2D g2d = (Graphics2D) g;
				
				Color startgreen = new Color(50,205,50);
				
				Color endred = new Color (255, 97, 3);
				
				GradientPaint startend = new GradientPaint(0, 150, startgreen, 400, 150 , endred );
				
				g2d.setPaint (startend);
				
				g2d.fillRect(100, 100 , time, 50);
			}


		}

}


Nun hab ich versucht, das obige Graphics-Objekt in meinen Code zu implementieren. Das Problem, was ich nun habe, ist, dass einfach nicht gezeichnet wird. Das Panel wird ordnungsgemäß angezeigt, aber es wird eben nichts gemalt. Ich hab auch schon lange bei google gesucht, aber irgendwie nichts passendes gefunden. Hier nun der Code, in dem es implementiert werden soll.

Java:
public class Zeit  {
	private double time;
	public double zähler;
	static int einaus=1; 
	// einaus=1 => aus ; einaus=0 => ein
	
	public Zeit (){
		time=0;
	}
	
	public Zeit(double Zeitgröße){
		time=Zeitgröße;
	}
	
	public void setTime(double Time){
		time= Time;
	}
	
	public double getTime(){
		return time;
	}
	
	public void setEin(){
		einaus=0;
	}
	
	public void setAus(){
		einaus=1;
	}
	
	public void timer (){
		zähler+=0.1;
		time= Math.round(100.0*zähler)/100.0;
	}
}

Java:
import java.util.TimerTask;
import java.util.Timer;
import java.text.SimpleDateFormat;

import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.awt.*;


public class Montagezeit extends JFrame {
	
	private static final long serialVersionUID = 43216789L;
	
	// frame: width = 548 , height = 466;
	// RGB für Hintergrund 230,230,230
	
	static int test;
	static Zeit aktuell= new Zeit(0);
	static Timer timer = new Timer();
	static Timer datetimer = new Timer();
	static TimerTask Uhr;
	static TimerTask Datum;
	static SimpleDateFormat heute= new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
	static Date now;
	String zeitausgabe;
	static Montagezeit mz = new Montagezeit ();
	static int erledigt;
	final Color background = new Color (230,230,230);
	
	public static void main(String[] args) {
		mz.init();
		//System.out.println("Montagezeit ist "+ fest.getTime());
	}
	
	public class Balkengraphics extends JComponent {
		
		private static final long serialVersionUID = 5L;
		
		Balkengraphics (){
			setPreferredSize(new Dimension(500,50));
		}
		
		public void paintComponent(Graphics g){
			
			Graphics2D g2d = (Graphics2D) g;
			
			Color startgreen = new Color(50,205,50);
			
			Color endred = new Color (255, 97, 3);
			
			GradientPaint startend = new GradientPaint(0, 150, startgreen, 400, 150 , endred );
			
			g2d.setPaint (startend);
			
			g2d.fillRect(100, 350 , ((int) aktuell.getTime())*10, 50) ;
		}
		
	}
	
	static void addComponent( Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty, int fill, int anchor){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		gbc.fill = fill;
		gbc.anchor = anchor;
		gbc.insets = new Insets(0,0,5,5);
		gbl.setConstraints(c, gbc);
		cont.add(c);
		
	}
	
	
	public void init(){
		

                          // Zeitbalken wird erstellt
		JPanel panel = new JPanel();
		panel.add( new Balkengraphics());




		//String [] störungen = {"01 Rüsten", "02 Werkzeugstörung" , "03 Maschinenstörung" , "04 Handlingstörung" , "05 Perihperiestörung" , "06 Werkzeugwartung", "07 Maschinenwartung",
								//"08 Handlingwartung" , "09 Peripheriewartung" , "10 Variantenwechsel" , "11 geplanter Stillstand" , "12 kein Personal" , "13 Kein Material" , "14 Ausprobe"};
		final JLabel Date = new JLabel ("Datum: " + zeitausgabe);
		JLabel mzLabel = new JLabel("Montagezeit [in s] : ");
		final JTextField mz_time = new JTextField("0");
		mz_time.setColumns(3);
		JLabel lblaktzeit = new JLabel("Aktuelle Zeit [in s] : ");
		final JTextField aktzeit_txt = new JTextField("0.0");
		aktzeit_txt.setColumns(3);
		aktzeit_txt.setEditable(false);
		JLabel stckzahl = new JLabel ("Stückzahl : ");
		final JTextField stckzahl_txt = new JTextField("0");
		stckzahl_txt.setColumns(3);
		stckzahl_txt.setEditable(false);
		JButton stckzahlreset = new JButton("Stückzahl Reset");
		final JButton start_stopp = new JButton("           Start          ");
		JButton timereset = new JButton("Reset");
		JButton fertig = new JButton("        Fertig         ");
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(EXIT_ON_CLOSE);
		Container c = f.getContentPane();
		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[] {30, 30, 30, 30, 0, 0, 0, 30, 30, 0, 0, 0, 0, 0, 0, 30, 30, 30};
		gbl.rowHeights = new int[] {30, 0, 0, 0, 30, 0, 0, 30, 0, 30, 0, 0, 30, 30, 30, 30, 30, 30, 30, 0, 0};
		gbl.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0};
		gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE, 0.0, 0.0};
		c.setLayout(gbl);
		//
		addComponent(c, gbl, Date, 8 , 3, 5 , 1, 0, 0, GridBagConstraints.NONE ,GridBagConstraints.WEST  );
		addComponent(c, gbl, mzLabel, 5 , 6, 1, 1, 0,0, GridBagConstraints.NONE , GridBagConstraints.EAST );
		addComponent(c, gbl, mz_time, 6 , 6, 4, 1, 0,0, GridBagConstraints.HORIZONTAL , GridBagConstraints.CENTER);
		addComponent(c, gbl, lblaktzeit, 5, 8, 1, 1, 0, 0 , GridBagConstraints.NONE ,GridBagConstraints.EAST);
		addComponent(c, gbl, aktzeit_txt, 6 , 8 , 4 , 1, 0,0, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
		addComponent(c, gbl, stckzahl, 5, 10, 1, 1, 0,0, GridBagConstraints.NONE ,GridBagConstraints.EAST);
		addComponent(c, gbl, stckzahl_txt , 6, 10 , 4, 1, 0,0, GridBagConstraints.HORIZONTAL , GridBagConstraints.CENTER);

                          //Zeitbalken wird hinzugefügt
		addComponent(c, gbl, panel, 5,12 , 10 , 1 , 0 , 0, GridBagConstraints.NONE, GridBagConstraints.CENTER );
		addComponent(c, gbl, stckzahlreset, 5, 14, 1, 1, 0, 0,GridBagConstraints.VERTICAL, GridBagConstraints.CENTER );
		addComponent(c, gbl, start_stopp, 10, 14, 1,1,0,0,GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		addComponent(c, gbl, timereset, 5, 16, 1,1,0,0,GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		addComponent(c, gbl, fertig, 10, 16, 1 ,1 , 0,0, GridBagConstraints.BOTH, GridBagConstraints.CENTER );
		
		
		Datum = new TimerTask(){
			public void run(){
				now = new Date();
				String zeitausgabe = heute.format(now);
				Date.setText("Datum: " + zeitausgabe);
				//test = Integer.parseInt(mz_time.getText());
				//System.out.println(String.valueOf(test));
			}
		};
		datetimer.scheduleAtFixedRate(Datum, 0, 1000);
		
		f.getContentPane().setBackground(background);
		f.pack();
		f.setVisible(true);
		System.out.println(f.getSize());
			
		
		start_stopp.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				
				try {
					test = Integer.parseInt(mz_time.getText());
				} catch (NumberFormatException ex){
					mz_time.setText("0");
					Zeit.einaus=0;
				}
				
				
				if (Zeit.einaus==1){
				
					try {
						
						if ((test<=0) || mz_time.getText().equals("")) {
							throw new ZeitException();
						}
					aktuell.setEin();
					start_stopp.setText("Stopp");
					 Uhr = new TimerTask(){
						public void run(){
							aktzeit_txt.setText(String.valueOf(aktuell.getTime()));
							stckzahl_txt.setText(String.valueOf(erledigt));

                                                                                          //Zeitbalken soll neugezeichnet werden
							f.repaint();
							//System.out.println("Aktuelle Zeit ist " + aktuell.getTime());
							if (Zeit.einaus == 1){
								Uhr.cancel();
							}
							if ((aktuell.getTime()/test) > 1 ){
								System.out.println("Du bist zu langsam");
							}
							else
								if((aktuell.getTime()/test) > 0.8){
									System.out.println("Du solltest dich beeilen");
								}
							
							aktuell.timer();
						}
					};
					
					timer.scheduleAtFixedRate(Uhr, 0, 100);
					//System.out.println("Zeit ist  " + Time);
					
					}
					
					catch (ZeitException f){
						System.out.println(f.getMessage());
					}
				} else {
					aktuell.setAus();
					start_stopp.setText("Start");
				}
			}
		});
		
		timereset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				start_stopp.setText("Start");
				aktuell.setTime(0);
				aktuell.setAus();
				aktuell.zähler= aktuell.getTime();
				aktzeit_txt.setText(String.valueOf(aktuell.getTime()));
			}
		});
		fertig.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if (aktuell.getTime()== 0){
					
				}
				else {
				start_stopp.setText("Start");
				aktuell.setTime(0);
				aktuell.setAus();
				aktuell.zähler= aktuell.getTime();
				erledigt++;
				stckzahl_txt.setText(String.valueOf(erledigt));
				aktzeit_txt.setText(String.valueOf(aktuell.getTime()));
				}
			}
		});
		stckzahlreset.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				erledigt=0;
				stckzahl_txt.setText(String.valueOf(erledigt));
			}
		});
	}
	
}


Noch als kleine Bermerkung:

Meine Methode repaint() soll im TimerTask angewendet werden und jede Zehntelsekunde neu gemalt werden, genau wie die Zeit auch jede Zehntelsekunde hochgezählt werden soll.
Ich hoffe, hierbei kann mir jemand helfen. 🙂

Mit freundlichen Grüßen

CarlCosinus
 
Hat niemand eine Idee? Weiß mir da im Moment selber nicht zu helfen, außer stundenlang im Internet zu suchen.
Ich hab ziemlich viel unwichtigen Kram in den ersten Post reingepackt. Werd hier nochmal die Code-Zeilen posten um die es mir geht.

Hier ist erstmal meine innere Klasse, die den Balken zeichnet:

Java:
public class Balkengraphics extends JPanel {
		
		private static final long serialVersionUID = 5L;
		
		Balkengraphics (){
			setPreferredSize(new Dimension(500,50));
		}
		
		public void paintComponent(Graphics g){
			
			Graphics2D g2d = (Graphics2D) g;
			
			Color startgreen = new Color(50,205,50);
			
			Color endred = new Color (255, 97, 3);
			
			GradientPaint startend = new GradientPaint(0, 150, startgreen, 400, 150 , endred );
			
			g2d.setPaint (startend);
			
			g2d.fillRect(100, 400 , balkenvar, 25) ;
			               	}
		
	}

Dann hab ich mir eine Hilfsmethode geschrieben, die den einzelnen Constraints-Objekten die Werte zuweist und sie in den Container packt.

Java:
static void addComponent( Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty, int fill, int anchor){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		gbc.fill = fill;
		gbc.anchor = anchor;
		gbc.insets = new Insets(0,0,5,5);
		gbl.setConstraints(c, gbc);
		cont.add(c);
		
	}

Und letzten Endes wird in der Initialisierungs-Methode ein Balkengraphics-Objekt erzeugt und mit addComponent hinzugefügt

Java:
Balkengraphics balken2 = new Balkengraphics();

Container c = f.getContentPane();
GridBagLayout gbl = new GridBagLayout();
gbl.columnWidths = new int[] {30, 120, 30, 60, 107, 41, 17, 5, 5, 3, 30, 30, 30, 30, 30, 30, 0};
gbl.rowHeights = new int[]{30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 32, 0, 32, 30, 30};
gbl.columnWeights = new double[]{0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
c.setLayout(gbl);

addComponent(c, gbl, Date, 12 , 1, 3 , 1, 0, 0, GridBagConstraints.NONE ,GridBagConstraints.WEST  );
addComponent(c, gbl, maschinenliste, 1, 2, 2,1,0,0,GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(c, gbl, entfernen, 4, 2, 4,1, 0,0, GridBagConstraints.BOTH, GridBagConstraints.CENTER );
addComponent(c, gbl, störliste, 9, 2, 2,1, 0,0, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(c, gbl, maschine_txt ,1 ,4 ,2 ,1 ,0 ,0 , GridBagConstraints.BOTH, GridBagConstraints.CENTER);
addComponent(c, gbl, hinzufügen, 4 ,4, 4, 1, 0, 0, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
addComponent(c, gbl, meldestörung, 9, 4, 2,1,0,0, GridBagConstraints.BOTH, GridBagConstraints.CENTER);
addComponent(c, gbl, mzLabel, 4 , 7, 4, 1, 0,0, GridBagConstraints.NONE , GridBagConstraints.EAST );
addComponent(c, gbl, mz_time, 8 , 7, 1, 1, 0,0, GridBagConstraints.HORIZONTAL , GridBagConstraints.CENTER);
addComponent(c, gbl, lblaktzeit, 4, 9, 4, 1, 0, 0 , GridBagConstraints.NONE ,GridBagConstraints.EAST);
addComponent(c, gbl, aktzeit_txt, 8 , 9 , 1 , 1, 0,0, GridBagConstraints.HORIZONTAL, GridBagConstraints.CENTER);
addComponent(c, gbl, stckzahl, 4, 11, 4, 1, 0,0, GridBagConstraints.NONE ,GridBagConstraints.EAST);
addComponent(c, gbl, stckzahl_txt , 8, 11 , 1, 1, 0,0, GridBagConstraints.HORIZONTAL , GridBagConstraints.CENTER);
addComponent(c, gbl, balken2, 2,13 , 10 , 1 , 0 , 0, GridBagConstraints.NONE, GridBagConstraints.CENTER );
addComponent(c, gbl, timereset, 4, 15, 4,2,0,0,GridBagConstraints.BOTH, GridBagConstraints.CENTER );
addComponent(c, gbl, start_stopp, 9, 15, 1,2,0,0,GridBagConstraints.BOTH, GridBagConstraints.CENTER );
addComponent(c, gbl, stckzahlreset, 4, 18, 4, 2, 0, 0,GridBagConstraints.BOTH, GridBagConstraints.CENTER );
addComponent(c, gbl, fertig, 9, 18, 1 ,2 , 0,0, GridBagConstraints.BOTH, GridBagConstraints.CENTER );
addComponent(c, gbl, Logo, 13, 19,2,2,0,0, GridBagConstraints.NONE, GridBagConstraints.WEST);

Erzeug ich ein weiteres Balkengraphics-Objekt in einem neuen JFrame(Standard-Layoutmanager), funktioniert es. Nur wird es irgendwie nicht in meinem JFrame mit GridbagLayout angezeigt. Das ganze wird dann in einem Timer erneuert alle 0,1 sekunden,aber der funktioniert, da es auf dem TestFrame einbahnfrei läuft.

Bin sehr dankbar um Hilfe!

Grüße

Carl
 
Zuletzt bearbeitet:
Ich verstehe dein Problem nicht.???:L

Läuft bei mir. 😉

Hab lediglich die Animation schneller gemacht und eingebaut, das der Balken nicht endlos wächst.

Java:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Balkengraphics extends JPanel {

	private static final long serialVersionUID = 5L;
	private Timer timer;
	private int balkenvar;

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Balkengraphics balkengraphics = new Balkengraphics();
		frame.add(balkengraphics);
		frame.pack();
		frame.setVisible(true);
		balkengraphics.init(frame);
	}

	private void init(JFrame frame) {
		Balkengraphics balken2 = new Balkengraphics();

		GridBagLayout gbl = new GridBagLayout();
		gbl.columnWidths = new int[] { 30, 120, 30, 60, 107, 41, 17, 5, 5, 3, 30, 30, 30, 30, 30, 30, 0 };
		gbl.rowHeights = new int[] { 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 32, 0, 32, 30,
				30 };
		gbl.columnWeights = new double[] { 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, Double.MIN_VALUE };
		gbl.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
		setLayout(gbl);

		addComponent(this, gbl, balken2, 2, 13, 10, 1, 0, 0, GridBagConstraints.NONE, GridBagConstraints.CENTER);
		TimerTask task = new TimerTask() {

			public void run() {
				balkenvar += 10;
				if (balkenvar >= 400) {
					timer.cancel();
				}
				frame.repaint();
			}
		};
		timer.scheduleAtFixedRate(task, 0, 100);

	}

	Balkengraphics() {
		timer = new Timer();
		setPreferredSize(new Dimension(700, 200));
	}

	public void paintComponent(Graphics g) {

		Graphics2D g2d = (Graphics2D) g;

		Color startgreen = Color.GREEN;

		Color endred = Color.RED;

		GradientPaint startend = new GradientPaint(0, 50, startgreen, 400, 50, endred);

		g2d.setPaint(startend);
		if (balkenvar <= 400) {
			g2d.fillRect(100, 50, balkenvar, 25);
		}
		g2d.fillRect(100, 80, 400, 25);
	}

	static void addComponent(Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height,
			double weightx, double weighty, int fill, int anchor) {
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = width;
		gbc.gridheight = height;
		gbc.weightx = weightx;
		gbc.weighty = weighty;
		gbc.fill = fill;
		gbc.anchor = anchor;
		gbc.insets = new Insets(0, 0, 5, 5);
		gbl.setConstraints(c, gbc);
		cont.add(c);

	}

}
 
Mal eine Frage nebenbei: Mein Eclipse meckert bei der init-Methode rum, dass er in einer inneren Klasse nicht auf eine non-final Variable referieren kann, in dem Fall bei frame.repaint() in der TimerTask. Konnte das erst zum Laufen bringen, als ich den Parameter auf init ( final JFrame frame) gesetzt hab. Sollte das so sein, oder hat sich da Eclipse einfach nur komisch?


Dein Code klappt jetzt wiederum auch bei mir, aber ich habe sowieso nur Probleme,wenn ich neben dem Balken noch andere Komponenten im Frame anordne.

Ich hab dafür mal noch ein weiteres Test-Projekt aufgemacht, in dem ich 4 Buttons habe in den oberen und unteren Ecken und in der Mitte den Balken haben möchte, mit den einfachsten GridBagConstraints, die es dafür gibt. Aber auch hier erscheint der Balken nicht. Man sieht eindeutig, dass der LayoutManager auf jeden Fall Platz für den Balken schafft, aber es passiert leider nichts. 🙁

Java:
import java.awt.*;
import javax.swing.*;
import java.util.TimerTask;
import java.util.Timer;


public class FarbBalkenTest extends JPanel {
	
	static Timer timer = new Timer ();
	static TimerTask task ;
	static int time;
	
	public static void main(String[] args) {
		
		FarbBalkenTest fbt = new FarbBalkenTest();
		fbt.init();
		

	}
	
	public static void addComponent(Container cont, GridBagLayout gbl, Component c, int x, int y, int width, int height, double weightx, double weighty ){
		
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx=x;
		gbc.gridy=y;
		gbc.gridwidth=width;gbc.gridheight=height;
		gbc.weightx=weightx;gbc.weighty=weighty;
		gbl.setConstraints(c,gbc);
		cont.add(c);
		
	}
	
	
	public void init (){
		
		final JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setBackground(new Color(230,230,230));
		Container c = frame.getContentPane();
		GridBagLayout gbl = new GridBagLayout();
		c.setLayout(gbl);
		ZeichenTest z_test = new ZeichenTest();
		
		addComponent(c,gbl, new Button("top left"), 0,0,1,1,1.0,1.0);
		addComponent(c,gbl, new Button("top right"), 2,0, 1,1,1.0,1.0);
		addComponent(c, gbl,z_test, 0,1,1,1,1.0,1.0);
		addComponent(c,gbl, new Button("bottom left"),0,2,1,1,1.0,1.0);
		addComponent(c,gbl, new Button("bottom right"),2,2,1,1,1.0,1.0);
		frame.setSize(500,500);
		frame.setVisible(true);
		
		
		task = new TimerTask(){
			public void run(){
				frame.repaint();
				time+= 20;
				System.out.println(time);
			}
		};
		timer.scheduleAtFixedRate(task, 0, 1000);
		
	
		
	}
	public class ZeichenTest extends JPanel  {
		
		public void paintComponent (Graphics g){
				
				Graphics2D g2d = (Graphics2D) g;
				
				Color startgreen = new Color(50,205,50);
				
				Color endred = new Color (255, 97, 3);
				
				GradientPaint startend = new GradientPaint(0,25 , startgreen, 400, 25 , endred );
				
				g2d.setPaint (startend);
				
				g2d.fillRect(50, 200 , time, 50);
			}


		}

}
 
Okay ich hab das Problem nun selbst gelöst und es war, wie immer, ziemlich simpel und auch logisch. Dadurch dass meine Balkengraphics ein JPanel erzeugen im JFrame, muss ich natürlich auch meinen Balken an der Stelle (0,0) beginnen lassen. Ansonsten wird der Balken, irgendwo weiter unten erzeugt und von Buttons, Labels etc. verdeckt. und ist logischerweise gar nicht erst sichtbar.

Hab nun einfach anstatt

Java:
g2d.fillRect(50, 200 , time, 50);

das hier geschrieben

Java:
g2d.fillRect(0, 0 , time, 50);

und schon seh ich den Balken und alles ist prima. Manchmal sieht man den Wald vor lauter Bäumen nicht, wie es so schön heißt. Trotzdem vielen Dank an Turtle, dass du dich meinem Problem gewidmet hast. 🙂
 

Zurück
Oben