import java.awt.*; import javax.swing.*; public class Danger3 extends JPanel implements Runnable { // the frame that loaded this panel DangerFrame3 parent; 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; // the background color of the panel Color bgColor; Thread runner; public Danger3(String warning, DangerFrame3 parent, int red, int green, int blue) { text = warning; FontMetrics fm = getFontMetrics(textFont); textX = 200 - fm.stringWidth(text) / 2; // store the frame that contains this panel this.parent = parent; // create the background color bgColor = new Color(red, green, blue); runner = new Thread(this); runner.start(); } public void paintComponent(Graphics comp) { Graphics2D comp2D = (Graphics2D) comp; comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 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) { // do nothing } } public void run() { Thread thisThread = Thread.currentThread(); while (runner == thisThread) { // set the speed based on the frame's slider value int speed = 100 - parent.speed.getValue(); pause(speed); brightness += 0.05; if (brightness > 1) { brightness = (float) 0.0; pause(75); } repaint(); } } }