-
GUI Problem - Fan Blades DO NOT move
Okay guys, basically I have to get the fan blades moving depending on what setting I select. I've been playing around with everything and I can't do it. I know I'm just missing something. Please Help. Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Assign5 extends JFrame implements ItemListener
{
private JRadioButton jrbOff;
private JRadioButton jrbLow;
private JRadioButton jrbMed;
private JRadioButton jrbHigh;
private Timer timer;
private int counter = 0;
final int TOTAL_NUMBER_OF_DEGREES = 360;
private ButtonGroup btg = new ButtonGroup();
private Blades blades;
public static void main (String [] args)
{
Assign5 frame = new Assign5();
frame.setSize(250,300);
frame.setTitle("Fan Blade Controls");
frame.setLocation(250, 250);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public Assign5()
{
JPanel p1 = new JPanel();
p1.setSize(200, 200);
p1.setLayout(new FlowLayout(FlowLayout.CENTER));
blades = new Blades();
blades.setSize(250, 250);
p1.add(blades);
JPanel p2 = new JPanel();
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
p2.add(jrbOff = new JRadioButton("Off", false));
p2.add(jrbLow = new JRadioButton("Low", false));
p2.add(jrbMed = new JRadioButton("Medium", false));
p2.add(jrbHigh = new JRadioButton("High", false));
jrbOff.setMnemonic('O');
jrbLow.setMnemonic('L');
jrbMed.setMnemonic('M');
jrbHigh.setMnemonic('H');
btg.add(jrbOff);
btg.add(jrbLow);
btg.add(jrbMed);
btg.add(jrbHigh);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(p1, BorderLayout.CENTER);
getContentPane().add(p2, BorderLayout.SOUTH);
jrbOff.addItemListener(this);
jrbLow.addItemListener(this);
jrbMed.addItemListener(this);
jrbHigh.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
if(jrbOff.isSelected())
{
blades.turnOnOff();
}
if(jrbLow.isSelected())
{
blades.turnOnLow();
}
if(jrbMed.isSelected())
{
blades.turnOnMed();
}
if(jrbHigh.isSelected())
{
blades.turnOnHigh();
}
}
class Blades extends JPanel
{
private boolean off;
private boolean low;
private boolean med;
private boolean high;
public Blades()
{
turnOnOff();
}
public void turnOnOff()
{
off = true;
low = false;
med = false;
high = false;
}
public void turnOnLow()
{
off = false;
low = true;
med = false;
high = false;
}
public void turnOnMed()
{
off = false;
low = false;
med = true;
high = false;
}
public void turnOnHigh()
{
off = false;
low = false;
med = false;
high = true;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (off)
{
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.blue);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 0, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 90, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 180, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 270, 30);
repaint();
}
else if (low)
{
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.blue);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 0, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 90, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 180, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 270, 30);
repaint();
}
else if (med)
{
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.blue);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 0, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 90, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 180, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 270, 30);
repaint();
}
else if (high)
{
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.blue);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 0, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 90, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 180, 30);
g.fillArc(x, y, 2* radius, 2 * radius, counter + 270, 30);
repaint();
}
}
public Dimension getPreferredSize()
{
return new Dimension(200, 200);
}
}
}
Similar Threads
-
Replies: 0
Last Post: 10-30-2002, 04:39 AM
-
Replies: 0
Last Post: 05-12-2002, 05:49 AM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Lim Wing Hoe in forum Java
Replies: 7
Last Post: 11-17-2000, 02:12 PM
-
By Peter in forum ASP.NET
Replies: 1
Last Post: 07-11-2000, 09:10 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks