Computer Science 209 Images and GUIs
Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used colors You can also obtain any color in the RGB system with the syntax new Color(r, g, b)
Setting Colors of Components The method setForeground sets the color of the text of a label, button, or field The method setBackgound sets the color of these components and also of a panel
Example: Coloring Components public ColorView(){ setBackground(Color.black); setTitle("Color Example"); JLabel label = new JLabel("A label"); label.setForeground(Color.red); JTextField field = new JTextField("Some text"); field.setForeground(Color.blue); Container c = getContentPane(); c.add(label, BorderLayout.NORTH); c.add(field, BorderLayout.SOUTH); }
Displaying a Playing Card When a card is instantiated, load its image from a disk file Send the card to a panel for display (during instantiation or later) Retrieve the card’s image and display it
Classes MainView CardPanel Card
Example 1: Default Behavior import javax.swing.*; import java.awt.*; public class MainView1 extends JFrame{ public MainView1(){ CardPanel panel = new CardPanel(); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); } Reasonable display when model is not available
Example 2: Card at Instantiation import javax.swing.*; import java.awt.*; public class MainView2 extends JFrame{ public MainView2(){ Card queenSpades = new Card(Suit.spade, 12); queenSpades.turn(); CardPanel panel = new CardPanel(queenSpades); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); }
Example 3: Reset the Card import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainView3 extends JFrame{ public MainView3(){ final Deck deck = new Deck(); final CardPanel panel = new CardPanel(); JButton button = new JButton("Deal"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if (! deck.isEmpty()){ Card card = deck.deal(); card.turn(); panel.setCard(card); }}}); Container c = getContentPane(); c.add(panel, BorderLayout.CENTER); c.add(button, BorderLayout.SOUTH); }
Responsibilities of CardPanel Default display when there is no card (wire frame) Display the back side if not face up Display the face (the card’s image) if face up
The CardPanel Class import javax.swing.*; import java.awt.*; public class CardPanel extends JPanel{ private Card card; public CardPanel(){ this(null); } public CardPanel(Card c){ setBackground(Color.black); card = c; public void setCard(Card c){ repaint();
The paintComponent Method public void paintComponent(Graphics g){ super.paintComponent(g); Icon image; if (card == null){ image = Card.getBack(); g.setColor(Color.yellow); int x = (getWidth() - image.getIconWidth()) / 2; int y = (getHeight() - image.getIconHeight()) / 2; g.drawRect(x, y, image.getIconWidth(),image.getIconHeight()); } else{ image = card.getImage(); image.paintIcon(this, g, x, y);
Responsibilities of Card Load this card’s image at instantiation Load the back side on demand Provide access to this card’s image and to the back side
The Card Class import javax.swing.*; public class Card implements Comparable<Card>{ private boolean faceUp; private Icon image; private static Icon CARD_BACK; public Card(Suit suit, int rank){ faceUp = false; image = getImageFromFile(rank, suit); } public Icon getImage(){ if (faceUp) return image; else return getBack(); # Continued on the next slide . . .
The Card Class public static Icon getBack(){ if (CARD_BACK == null) CARD_BACK = getBackFromFile(); return CARD_BACK; } private Icon getImageFromFile(int rank, Suit suit){ String fileName = "DECK/"; fileName += rank; fileName += Character.toUpperCase(suit.toString().charAt(0)); fileName += ".GIF"; return new ImageIcon(fileName); private static Icon getBackFromFile(){ return new ImageIcon("DECK/CARDBACK.GIF");
Using getResource import javax.swing.*; public class Card implements Comparable<Card>{ private boolean faceUp; private Icon image; private static Icon CARD_BACK; public Card(Suit suit, int rank){ faceUp = false; image = getImageFromFile(rank, suit); if (CARD_BACK == null) CARD_BACK = getBackFromFile(); // All images must } // be loaded in // non-static context # Continued on the next slide . . . In Eclipse, you must use getResource with an object instance to locate an image file on disk
Using getResource public static Icon getBack(){ if (CARD_BACK == null) new Card(Suit.spade, 1); // Sets CARD_BACK return CARD_BACK; } private Icon getImageFromFile(int rank, Suit suit){ String fileName = "DECK/"; fileName += rank; fileName += Character.toUpperCase(suit.toString().charAt(0)); fileName += ".GIF"; return new ImageIcon(getClass().getResource(fileName)); private static Icon getBackFromFile(){ String fileName = "DECK/CARDBACK.GIF";
static Variables and Methods static variables, such as Math.PI, belong to no instance of a class but can be shared by all instances static methods, such as Math.sqrt, are run with the class, not an instance Use static to represent a resource that all instances have in common