import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.plaf.*; /* This program should compile and run on both Sun JDK and libgcj. It has been used for compatibility testing. */ public class AWTDemo extends Panel { public static void main(String[] args) { Frame frame = new Frame("Component test"); frame.setBackground(Color.pink); Panel panel = new AWTDemo(); panel.setLayout(null); frame.add(panel, BorderLayout.CENTER); // Supports both heavyweight Panel heavy = new HeavyCross(); heavy.setLayout(null); heavy.setBackground(Color.orange); heavy.setBounds(70, 50, 50, 100); // ... and lightweight within heavyweight Component hlight = new LightCross(); hlight.setBackground(Color.cyan); hlight.setBounds(10, 10, 50, 30); heavy.add(hlight); // ... and heavyweight within heavyweight Component hheavy = new HeavyCross(); hheavy.setBackground(Color.magenta); hheavy.setBounds(10, 60, 50, 30); heavy.add(hheavy); panel.add(heavy); // ... and lightweight components Container light = new LightCross(); light.setBackground(Color.green); light.setBounds(180, 50, 50, 100); // ... and lightweight within lightweight Component llight = new LightCross(); llight.setBackground(Color.cyan); llight.setBounds(10, 10, 50, 30); light.add(llight); // ... and heavyweight within lightweight Component lheavy = new HeavyCross(); lheavy.setBackground(Color.magenta); lheavy.setBounds(10, 60, 50, 30); light.add(lheavy); panel.add(light); JButton swing = new JButton(); swing.setText("Swing"); swing.setBounds(100, 85, 100, 30); ActionListener swingAL = new PrintMessageAction("Swing button pressed"); swing.getModel().addActionListener(swingAL); panel.add(swing); Button awtB = new Button("AWT"); awtB.setBounds(100, 130, 100, 30); ActionListener awtAL = new PrintMessageAction("AWT button pressed"); awtB.addActionListener(awtAL); panel.add(awtB); CImage cimg = new CImage(); cimg.setBounds(10, 10, 70, 70); panel.add(cimg); frame.pack(); /* Bragging mode ON: When location of frame is not specified, our XToolkit allows the window manager to choose position freely. The Sun JDK does not. */ frame.setVisible(true); } public Dimension getPreferredSize() { return new Dimension(300, 200); } /** Bragging mode ON: our XToolkit will automatically set window hints based on the getMinimumSize/getMaximumSize methods. The Sun JDK does not. */ public Dimension getMinimumSize() { return new Dimension(100, 50); } public void paint(Graphics gfx) { /* Must remember to paint parent, or the lightweight components won't be painted. */ super.paint(gfx); Insets insets = getInsets(); gfx.translate(insets.left, insets.top); gfx.setColor(Color.red); gfx.drawLine(50, 30, 250, 150); gfx.setColor(Color.white); gfx.drawLine(50, 40, 250, 160); gfx.setColor(Color.blue); gfx.drawLine(50, 50, 250, 170); } } class HeavyCross extends Panel { public void paint(Graphics gfx) { /* Must remember to paint parent, or the lightweight components won't be painted. */ super.paint(gfx); Dimension dim = getSize(); gfx.setColor(Color.black); gfx.drawRect(0, 0, dim.width-1, dim.height-1); gfx.drawLine(0, 0, dim.width-1, dim.height-1); gfx.drawLine(dim.width-1, 0, 0, dim.height-1); } } class LightCross extends Container { public void paint(Graphics gfx) { /* Must remember to paint parent, or the lightweight components won't be painted. */ super.paint(gfx); Dimension dim = getSize(); gfx.setColor(Color.black); gfx.drawRect(0, 0, dim.width-1, dim.height-1); gfx.drawLine(0, 0, dim.width-1, dim.height-1); gfx.drawLine(dim.width-1, 0, 0, dim.height-1); Graphics copy = gfx.create(); // no.rwr.awt.Debug.testClipping(this, copy); } } class CImage extends Canvas { Image img; public void paint(Graphics gfx) { if (img == null) { img = getToolkit().getImage("../jcnix/testimage.jpg"); } gfx.drawImage(img, 0, 0, this); } } class PrintMessageAction implements ActionListener { String message; public PrintMessageAction(String message) { this.message = message; } public void actionPerformed(ActionEvent event) { System.out.println(message); } }