import java.awt.*; import javax.swing.*; public class Danger3 extends JPanel implements Runnable { String text = "No text has been specified"; float hue = (float) 0.5; float saturation = (float) 0.8; float brightness = (float) 0.0; Font textFont = new Font("Dialog", Font.BOLD, 20); int textX; Thread runner; Color bgColor; public Danger3(String warning, int red, int green, int blue) { text = warning; FontMetrics fm = getFontMetrics(textFont); textX = 200 - fm.stringWidth(text) / 2; bgColor = new Color(red, green, blue); runner = new Thread(this); runner.start(); } public void paintComponent(Graphics comp) { Graphics2D comp2D = (Graphics2D) comp; comp2D.setColor(bgColor); comp2D.fillRect(0, 0, 400, 200); Color textColor = Color.getHSBColor(hue, saturation, brightness); comp2D.setColor(textColor); comp2D.setFont(textFont); comp2D.drawString(text, textX, 30); } void pause(int duration) { try { Thread.sleep(duration); } catch (InterruptedException e) { } } public void run() { Thread thisThread = Thread.currentThread(); while (runner == thisThread) { pause(75); brightness += 0.05; if (brightness > 1) { brightness = (float) 0.0; pause(75); } repaint(); } } }