import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Toolkit; import javax.swing.JComponent; /** * The panel to hold the image of venus and a message. * * @author Barbara Lerner */ public class VenusPanel extends JComponent { // Two constants to hold the 2 message choices. private static final String HI = "Hello from Venus!"; private static final String BYE = "Bye from Venus!"; // The message currently displayed. private String msg = HI; /** * Draw the component * @param g the graphics object to draw on. */ public void paintComponent(Graphics g) { // Find out how big the panel is. Dimension d = getSize(); // Make the background black by drawing a black rectangle the size of the panel. g.setColor(Color.black); g.fillRect(0, 0, d.width, d.height); // Select the font and color for the message. 24 is the font size. // The color is specified in RGB. Each parameter can range from 0 to 255. g.setFont(new Font("Helvetica", Font.BOLD, 24)); g.setColor(new Color(255, 215, 0)); // gold color // Draw the message at coordinate (40, 25), 40 pixels from the left and 25 pixels from the // top of the panel. g.drawString(msg, 40, 25); // Read the image from the file and draw it at (20, 60). g.drawImage(Toolkit.getDefaultToolkit().getImage("Venus.gif"), 20, 60, this); } /** * Display the hi message */ public void sayHi() { msg = HI; // Cause the panel to be redrawn with the new message. //repaint(); } /** * Display the bye message. */ public void sayBye() { msg = BYE; //repaint(); } }