import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.SystemColor; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JProgressBar; import javax.swing.SwingUtilities; import javax.swing.border.EmptyBorder; import javax.swing.plaf.basic.BasicProgressBarUI; public class FarbigeStatusAnzeige { private final static int MAX = 4000, MIN = 0; public FarbigeStatusAnzeige() { init(); } private void init() { FarbigeProgressBar bar = new FarbigeProgressBar(MIN, MAX); bar.setValue(MAX); bar.setStringPainted(true); final Runnable runnable = new Runnable() { int rot = 0, gruen = 255; double faktor = MAX / 255 / 2; public void run() { for (int i = MAX; i >= MIN; i--) { if (i < MAX / 5 * 2) { rot += faktor / 5; } if (i < MAX / 10) { gruen -= faktor / 25; rot += faktor / 15; } rot = rot > 255 ? 255 : rot; rot = rot < 0 ? 0 : rot; gruen = gruen > 255 ? 255 : gruen; gruen = gruen < 0 ? 0 : gruen; bar.setForeground(new Color(rot, gruen, 0)); try { Thread.sleep(3); } catch (InterruptedException ex) { } bar.setValue(i); } // Farbigkeit auf Ausgangswert zuruecksetzen rot = 0; gruen = 255; bar.updateUI(); } }; JButton button = new JButton("start"); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread thread = new Thread(runnable); thread.start(); } }); JFrame frame = new JFrame("Farbige Statusanzeige"); frame.setLayout(new BorderLayout()); frame.add(button, BorderLayout.SOUTH); frame.add(bar, BorderLayout.CENTER); frame.pack(); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new FarbigeStatusAnzeige()); } } class FarbigeProgressBar extends JProgressBar { public FarbigeProgressBar(int start, int end) { setMinimum(start); setMaximum(end); setForeground(SystemColor.window); setBackground(SystemColor.window); setBorder(new EmptyBorder(3, 5, 3, 5)); Dimension size = new Dimension(300, 20); setPreferredSize(size); setMaximumSize(size); setMinimumSize(size); BasicProgressBarUI ui = new BasicProgressBarUI() { protected Color getSelectionForeground() { return Color.BLACK; } protected Color getSelectionBackground() { return Color.BLACK; } }; setUI(ui); } @Override public void updateUI() { setUI(getUI()); } }