All pastes #422735 Raw Edit

Untitled

public text v1 · immutable
·published 2007-04-03 17:27 UTC
rendered paste body
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class DrawArcs extends JFrame //implements ActionListener
{
  private JRadioButton low;
  private JRadioButton med;
  private JRadioButton high;
  private JRadioButton stop;
  
 public DrawArcs()
 {
  setTitle("Show Arcs");
  setLayout(new BorderLayout());
  ArcsPanel panel = new ArcsPanel();
  add(panel, BorderLayout.CENTER);
  panel.radioButtons();
 }

 public static void main (String [] args)
 {
  DrawArcs frame = new DrawArcs();

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(250, 300);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
  
 }


 class ArcsPanel extends JPanel implements ActionListener
 {
  private JRadioButton low;
  private JRadioButton medium;
  private JRadioButton high;
  private JRadioButton stop;
  public int spin;
  
  public void paintComponent(Graphics g)
  {
   super.paintComponent(g);
 
   int xCenter = getWidth() / 2;
   int yCenter = getHeight() / 2;
   int radius = (int)(Math.min(getWidth(), getHeight()) * 0.4);

   int x = xCenter - radius;
   int y = yCenter - radius;
   g.setColor(Color.black);
   g.drawOval(xCenter - radius, yCenter - radius, 2 * radius, 2 * radius);
   g.setColor(Color.black);
   g.drawRoundRect(xCenter - radius, yCenter - radius, 2*radius, 2*radius, 50, 50);
   g.setColor(Color.red);
   g.fillArc(x, y, 2 * radius, 2 * radius, spin+0, 30);
   g.setColor(Color.green);
   g.fillArc(x, y, 2 * radius, 2 * radius, spin+90, 30);
   g.setColor(Color.blue);
   g.fillArc(x, y, 2 * radius, 2 * radius, spin+180, 30);
   g.setColor(Color.yellow);
   g.fillArc(x, y, 2 * radius, 2 * radius, spin+270, 30);
  }
 public void actionPerformed(ActionEvent e){}
  public void radioButtons()
  {
    JPanel p1 = new JPanel();
    p1.setLayout(new GridLayout(1,1));
    p1.add(low = new JRadioButton("low", false));
    p1.add(med = new JRadioButton("med", false));
    p1.add(high = new JRadioButton("high", false));
    p1.add(stop = new JRadioButton("stop", false));
    add(p1, BorderLayout.WEST);
    /*
    low.setMnemonic('L');
    med.setMnemonic('M');
    high.setMnemonic('H');
    stop.setMnemonic('S');
   */ 
    ButtonGroup btg = new ButtonGroup();
    System.out.println("WORKS!");
    
    btg.add(low);
    btg.add(med);
    btg.add(high);
    btg.add(stop);
    stop.setSelected(true);
    
    low.addActionListener(new ActionListener()
   {
     public void actionPerformed(ActionEvent e)
     {
      spin+=10;
      if (spin>=360)
        spin-=360;
      repaint();
     }
   });

  med.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    spin+=20;
    if (spin>=360)
      spin-=360;
    repaint();
   }
  });

  high.addActionListener(new ActionListener()
  {
   public void actionPerformed(ActionEvent e)
   {
    spin+=30;
    if (spin>=360)
      spin-=360;
    repaint();
   }
  });
  }
 }
}