import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; /** Fyller en del av frame-vinduet med fylte sirkler, én om gangen med en liten pause mellom hver. */ public class FillDemo extends JFrame implements ActionListener { public static final int WIDTH = 300; public static final int HEIGHT = 200; public static final int FILL_WIDTH = 300; public static final int FILL_HEIGHT = 100; public static final int CIRCLE_SIZE = 10; public static final int PAUSE = 100; //millisekunder private JButton startButton; private JButton avsluttButton; private JPanel box; public static void main(String[] args) { FillDemo gui = new FillDemo( ); gui.setVisible(true); } public FillDemo( ) { setSize(WIDTH, HEIGHT); setTitle("FillDemo"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout( )); box = new JPanel( ); add(box, BorderLayout.CENTER); JPanel buttonPanel = new JPanel( ); buttonPanel.setLayout(new FlowLayout( )); startButton = new JButton("Start"); startButton.addActionListener(this); buttonPanel.add(startButton); avsluttButton = new JButton("Avslutt"); avsluttButton.addActionListener(this); buttonPanel.add(avsluttButton); add(buttonPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { if (e.getSource() == avsluttButton) System.exit(0); else fill( ); } public void fill( ) { Graphics g = box.getGraphics( ); for (int y = 0; y < FILL_HEIGHT; y = y + CIRCLE_SIZE) for (int x = 0; x < FILL_WIDTH; x = x + CIRCLE_SIZE) { g.fillOval(x, y, CIRCLE_SIZE, CIRCLE_SIZE); doNothing(PAUSE); } } public void doNothing(int milliseconds) { try { Thread.sleep(milliseconds); } catch(InterruptedException e) { System.out.println("Unexpected interrupt"); System.exit(0); } } }