import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Arc2D; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class ThreadTest extends JFrame implements ActionListener { public static final Color WEDGE_COLOR = new Color(40, 145, 240); public static final long MOUTH_TIME = 150L; public static final int RADIUS = 60; public static final double MAX_MOUTH_ANGLE = 125.0; private WedgeComponent wedgeComponent; private JButton button1; private JButton button2; public ThreadTest() { super("Thread Test"); this.wedgeComponent = new WedgeComponent(); this.wedgeComponent.setPreferredSize(new Dimension(200, 200)); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); this.button1 = new JButton("Button 1"); this.button1.addActionListener(this); this.button2 = new JButton("Button 2"); this.button2.addActionListener(this); buttonPanel.add(this.button1); buttonPanel.add(this.button2); JPanel allPanel = new JPanel(new BorderLayout()); allPanel.add(this.wedgeComponent, BorderLayout.CENTER); allPanel.add(buttonPanel, BorderLayout.SOUTH); setContentPane(allPanel); pack(); new AnimationThread().start(); } public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == this.button1) button1(); else if (source == this.button2) button2(); } private void button1() { this.button1.setEnabled(false); button1Work(); this.button1.setEnabled(true); } private void button1Work() { try { Thread.sleep(500L); } catch (InterruptedException ie) { } } private void button2() { this.button2.setEnabled(false); button2Work(); this.button2.setEnabled(true); } private void button2Work() { try { Thread.sleep(1500L); } catch (InterruptedException ie) { } } public class WedgeComponent extends JComponent { private double mouthPhase; public void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); this.mouthPhase = (System.currentTimeMillis() % MOUTH_TIME) / (double)MOUTH_TIME; double mouthAngle = this.mouthPhase * MAX_MOUTH_ANGLE; Arc2D.Double wd = new Arc2D.Double(100 - RADIUS, 100 - RADIUS, 2 * RADIUS, 2 * RADIUS, mouthAngle / 2, 360.0 - mouthAngle, Arc2D.PIE); g2d.setColor(WEDGE_COLOR); g2d.fill(wd); } } private class AnimationThread extends Thread { public void run() { while (true) { ThreadTest.this.wedgeComponent.repaint(); try { Thread.sleep(25L); } catch (InterruptedException ie) { } } } } public static void main(String[] args) { ThreadTest f = new ThreadTest(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); } }