UsingSwing

UsingSwing - Graphic Interface Programming II Using Swing...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
IT Uppsala universitet Graphic Interface Programming II Using Swing and OOP – more advanced issues
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UU/IT 4/22/10 | #2 @ UU/IT Good interface programming Swing is an EXTENDIBLE toolkit Build your OWN components Many times you need the same (similar) solution several times Make a general (reusable) solution
Background image of page 2
UU/IT 4/22/10 | #3 @ UU/IT Example: ImagePanel Working with images Want to use a JPanel with an image drawn on it Extend the JPanel!
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UU/IT 4/22/10 | #4 @ UU/IT ImagePanel public class ImagePanel extends JPanel { private Image img; public ImagePanel(String img) { this(new ImageIcon(img).getImage()); } public ImagePanel(Image img) { this.img = img; Dimension size = new Dimension(img.getWidth(null),img.getHeight(null)); setPreferredSize(size); setMinimumSize(size); setMaximumSize(size); setSize(size); setLayout(null); } public void paintComponent(Graphics g) { g.drawImage(img,0,0,null); } }
Background image of page 4
UU/IT 4/22/10 | #5 @ UU/IT Labels too? public class ImageLabel extends JLabel { public ImageLabel(String img) { this(new ImageIcon(img)); } public ImageLabel(ImageIcon icon) { setIcon(icon); setIconTextGap(0); setBorder(null); setText(null); setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null)); } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UU/IT 4/22/10 | #6 @ UU/IT ImageButtons? import java.awt.*; import javax.swing.*; public class ImageButton extends JButton { public ImageButton(String img) { this(new ImageIcon(img)); } public ImageButton(ImageIcon icon) { setIcon(icon); setMargin(new Insets(0,0,0,0)); setIconTextGap(0); setBorderPainted(false); setBorder(null); setText(null); setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null)); } }
Background image of page 6
UU/IT 4/22/10 | #7 @ UU/IT Reusable! All these Components can be extended in turn They are (extended) JPanels, JLabels and JButtons … but with a twist – Your twist
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
UU/IT 4/22/10 | #8 @ UU/IT ReUsable Methods? Why not just make a method library? //Create and set up a colored label. private JLabel createColoredLabel(String text, Color color, Point origin) { JLabel label = new JLabel(text); label.setVerticalAlignment(JLabel.TOP); label.setHorizontalAlignment(JLabel.CENTER); label.setOpaque(true); label.setBackground(color); label.setForeground(Color.black); label.setBorder(BorderFactory.createLineBorder(Color.black)); label.setBounds(origin.x, origin.y, 140, 140); return label; }
Background image of page 8
UU/IT 4/22/10 | #9 @ UU/IT ReUsable Methods ? //A Colored Label Class with Black Border
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/01/2011 for the course 1MD 003 taught by Professor Larsoestreicher during the Spring '10 term at Uppsala.

Page1 / 41

UsingSwing - Graphic Interface Programming II Using Swing...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online