-
RoundButton2
when i run this program nothing happens
// Imports for the GUI classes.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
* RoundButton2.java -
* A custom JComponent that is a round button. The round button
* will be empty until the mouse enters the circle. When the
* mouse enters the circle it will be filled in with a specified
* color. Then when the mouse is pressed the color will change
* to a second specified color. Note that RoundButton "is a"
* MouseListener so it can handle its own MouseEvents.
*
* @author Grant William Braught
* @author Dickinson College
* @version 11/29/2000
*/
public class RoundButton2
extends JComponent
implements MouseListener, MouseMotionListener {
private Color mouseOverColor;
private Color mousePressedColor;
private boolean mouseOver;
private boolean mousePressed;
private int radius;
/**
* Construct a new RoundButton with the specified
* colors for mouse over and mouse pressed events.
*
* @param mouseOverColor the color the button should
* be filled with when the mouse is
* over the button.
* @param mousePressedColor the color the button should be
* filled with when the mouse
* is pressed on the button.
*/
public RoundButton2(Color mouseOverColor,
Color mousePressedColor) {
this.mouseOverColor = mouseOverColor;
this.mousePressedColor = mousePressedColor;
mouseOver = false;
mousePressed = false;
radius = 100;
// Register this obj. as a MouseListener for itself.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
/**
* Default constructor that sets the mouse over color
* to blue and the mouse pressed color to red.
*/
public RoundButton2() {
this(Color.blue, Color.red);
}
/**
* Paint the RoundButton on the screen each time the
* window is redrawn. Recall that the paint() method
* of each JComponent in the content pane is called
* automatically when the window is redrawn. This
* overrides paint() from JComponent so we have control
* over what the RoundButton will look like when it is
* painted.
*
* @param g the Graphics context on which to paint the
* button.
*/
public void paint(Graphics g) {
// Check mouse pressed first because if the mouse
// is pressed it will also be in the button.
if (mousePressed && mouseOver) {
g.setColor(mousePressedColor);
g.fillOval(0,0,2*radius,2*radius);
}
else if (mouseOver) {
g.setColor(mouseOverColor);
g.fillOval(0,0,2*radius,2*radius);
}
else {
g.setColor(Color.black);
g.drawOval(0,0,2*radius,2*radius);
}
}
/**
* Return the ideal size that our button would
* like to be. This overrides the getPreferredSize
* from JComponent which returns 0x0.
*
* @return the preferred size of the RoundButton.
*/
public Dimension getPreferredSize() {
return new Dimension(2*radius, 2*radius);
}
/**
* This method is called by the LayoutManager when it
* places and resizes the component. We'll use the
* information here to adjust the way the RoundButton is
* painted.
*
* @param x the new x coordinate of this RoundButton within
* its parent container.
* @param y the new y coordinate of this RoundButton within
* its parent container.
* @param width the new width of the RoundButton.
* @param height the new height of the RoundButton.
*/
public void setBounds(int x, int y, int width, int height) {
// Make the radius of the circle half as large as the
// smallest dimension.
radius = (int)Math.min(width,height) / 2;
// Pass the changes in the dimesions up to the JComponent
// class that uses them for generating MouseEvents and
// MouseMotion events.
super.setBounds(x,y,width,height);
// Repaint the component. Everytime the bounds are changed
// the component should be repainted.
repaint();
}
/**
* Private method used to determine if a point is inside
* the circle that represents the RoundButton.
*
* @param pt the Point that represents the current
* location of the mouse.
* @return true if pt is within the circle.
*/
private boolean isInside(Point pt) {
// If the distance between pt and the center of
// the circle (radius,radius) is less than or =
// the radius then the mouse is in the circle.
return (pt.distance(new Point(radius,radius)) <= radius);
}
// Methods from the MouseListener Interface.
/**
* Handler called when the mouse is clicked on
* the RoundButton.
*
* @param e reference to a MouseEvent object describing
* the mouse click.
*/
public void mouseClicked(MouseEvent e) {}
/**
* Handler called when the mouse enters the
* RoundButton.
*
* @param e reference to a MouseEvent object describing
* the mouse entry.
*/
public void mouseEntered(MouseEvent e) {}
/**
* Handler called when the mouse exits the
* RoundButton. Again, this is called when the
* mouse exits the bounding rectangle.
*
* @param e reference to a MouseEvent object describing
* the mouse exit.
*/
public void mouseExited(MouseEvent e) {
// This handles exiting at the points where the
// circle contacts the edges of the component.
// These points are missed by the mouseMoved method
// because they are on the circle.
mouseOver = false;
repaint();
}
/**
* Handler called when the mouse button is pressed
* over the RoundButton. Again this is called if the
* button is pressed anywhere within the bounding
* rectangle.
*
* @param e reference to a MouseEvent object describing
* the mouse press.
*/
public void mousePressed(MouseEvent e) {
Point mouseLoc = e.getPoint();
if (isInside(mouseLoc)) {
mousePressed = true;
repaint();
}
}
/**
* Handler called when the mosue button is released
* over the RoundButton. Blah... Blah.. bounding
* rectangle.
*
* @param e reference to a MouseEvent object describing
* the mouse release.
*/
public void mouseReleased(MouseEvent e) {
mousePressed = false;
repaint();
}
// Methods from the MouseMotionListener interface.
/**
* Called when the mouse is moved while over the
* RoundButton. This will check to see if the
* mouse has moved into or out of the circle.
*
* @param e MouseEvent that describes the mouse motion.
*/
public void mouseMoved(MouseEvent e) {
if (isInside(e.getPoint())) {
// Only repaint if the button changes.
if (!mouseOver) {
mouseOver = true;
repaint();
}
}
else {
if (mouseOver) {
mouseOver = false;
repaint();
}
}
}
/**
* Called when the mouse is dragged while over the
* RoundButton. This does the same thing that mouseMoved
* does.
*
* @param e MouseEvent that describes the mouse dragging.
*/
public void mouseDragged(MouseEvent e) {
mouseMoved(e);
}
}
-
Use [ code ] [ /code ] tags to make your code more readable for us in the future please.
-
I made som minor adjustments
a test:
Code:
// Imports for the GUI classes.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
* RoundButton2.java -
* A custom JComponent that is a round button. The round button
* will be empty until the mouse enters the circle. When the
* mouse enters the circle it will be filled in with a specified
* color. Then when the mouse is pressed the color will change
* to a second specified color. Note that RoundButton "is a"
* MouseListener so it can handle its own MouseEvents.
*
* @author Grant William Braught
* @author Dickinson College
* @version 11/29/2000
*/
public class RoundButton2
extends JComponent
implements MouseListener, MouseMotionListener {
private Color mouseOverColor;
private Color mousePressedColor;
private boolean mouseOver;
private boolean mousePressed;
private int radius;
/**
* Construct a new RoundButton with the specified
* colors for mouse over and mouse pressed events.
*
* @param mouseOverColor the color the button should
* be filled with when the mouse is
* over the button.
* @param mousePressedColor the color the button should be
* filled with when the mouse
* is pressed on the button.
*/
public RoundButton2(Color mouseOverColor,
Color mousePressedColor) {
this.mouseOverColor = mouseOverColor;
this.mousePressedColor = mousePressedColor;
mouseOver = false;
mousePressed = false;
radius = 100;
// Register this obj. as a MouseListener for itself.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
/**
* Default constructor that sets the mouse over color
* to blue and the mouse pressed color to red.
*/
public RoundButton2() {
this(Color.blue, Color.red);
}
/**
* Paint the RoundButton on the screen each time the
* window is redrawn. Recall that the paint() method
* of each JComponent in the content pane is called
* automatically when the window is redrawn. This
* overrides paint() from JComponent so we have control
* over what the RoundButton will look like when it is
* painted.
*
* @param g the Graphics context on which to paint the
* button.
*/
public void update(Graphics g) {
// Check mouse pressed first because if the mouse
// is pressed it will also be in the button.
Color c=g.getColor(); // get graphics default color & fill background
g.setColor(Color.white);
g.fillRect(0,0,2 * radius, 2 * radius);
// now get the button color
if (mousePressed && mouseOver) {
g.setColor(mousePressedColor);
}
else if (mouseOver) {
g.setColor(mouseOverColor);
}
else {
g.setColor(Color.black);
}
// fill button click area using the button color
g.fillOval(0, 0, 2 * radius, 2 * radius);
g.setColor(c); // reset graphics default color
}
public void paint(Graphics g) {
update(g);
}
/**
* Return the ideal size that our button would
* like to be. This overrides the getPreferredSize
* from JComponent which returns 0x0.
*
* @return the preferred size of the RoundButton.
*/
public Dimension getPreferredSize() {
return new Dimension(2 * radius, 2 * radius);
}
/**
* This method is called by the LayoutManager when it
* places and resizes the component. We'll use the
* information here to adjust the way the RoundButton is
* painted.
*
* @param x the new x coordinate of this RoundButton within
* its parent container.
* @param y the new y coordinate of this RoundButton within
* its parent container.
* @param width the new width of the RoundButton.
* @param height the new height of the RoundButton.
*/
public void setBounds(int x, int y, int width, int height) {
// Make the radius of the circle half as large as the
// smallest dimension.
radius = (int) Math.min(width, height) / 2;
// Pass the changes in the dimesions up to the JComponent
// class that uses them for generating MouseEvents and
// MouseMotion events.
super.setBounds(x, y, width, height);
// Repaint the component. Everytime the bounds are changed
// the component should be repainted.
repaint();
}
/**
* Private method used to determine if a point is inside
* the circle that represents the RoundButton.
*
* @param pt the Point that represents the current
* location of the mouse.
* @return true if pt is within the circle.
*/
private boolean isInside(Point pt) {
// If the distance between pt and the center of
// the circle (radius,radius) is less than or =
// the radius then the mouse is in the circle.
return (pt.distance(new Point(radius, radius)) <= radius);
}
// Methods from the MouseListener Interface.
/**
* Handler called when the mouse is clicked on
* the RoundButton.
*
* @param e reference to a MouseEvent object describing
* the mouse click.
*/
public void mouseClicked(MouseEvent e) {}
/**
* Handler called when the mouse enters the
* RoundButton.
*
* @param e reference to a MouseEvent object describing
* the mouse entry.
*/
public void mouseEntered(MouseEvent e) {}
/**
* Handler called when the mouse exits the
* RoundButton. Again, this is called when the
* mouse exits the bounding rectangle.
*
* @param e reference to a MouseEvent object describing
* the mouse exit.
*/
public void mouseExited(MouseEvent e) {
// This handles exiting at the points where the
// circle contacts the edges of the component.
// These points are missed by the mouseMoved method
// because they are on the circle.
mouseOver = false;
repaint();
}
/**
* Handler called when the mouse button is pressed
* over the RoundButton. Again this is called if the
* button is pressed anywhere within the bounding
* rectangle.
*
* @param e reference to a MouseEvent object describing
* the mouse press.
*/
public void mousePressed(MouseEvent e) {
Point mouseLoc = e.getPoint();
if (isInside(mouseLoc)) {
mousePressed = true;
repaint();
}
}
/**
* Handler called when the mosue button is released
* over the RoundButton. Blah... Blah.. bounding
* rectangle.
*
* @param e reference to a MouseEvent object describing
* the mouse release.
*/
public void mouseReleased(MouseEvent e) {
mousePressed = false;
repaint();
}
// Methods from the MouseMotionListener interface.
/**
* Called when the mouse is moved while over the
* RoundButton. This will check to see if the
* mouse has moved into or out of the circle.
*
* @param e MouseEvent that describes the mouse motion.
*/
public void mouseMoved(MouseEvent e) {
if (isInside(e.getPoint())) {
// Only repaint if the button changes.
if (!mouseOver) {
mouseOver = true;
repaint();
}
}
else {
if (mouseOver) {
mouseOver = false;
repaint();
}
}
}
/**
* Called when the mouse is dragged while over the
* RoundButton. This does the same thing that mouseMoved
* does.
*
* @param e MouseEvent that describes the mouse dragging.
*/
public void mouseDragged(MouseEvent e) {
mouseMoved(e);
}
public static void main (String [] args) {
RoundButton2 rb=new RoundButton2(Color.red,Color.white);
JFrame f=new JFrame("RoundButton");
f.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER));
f.getContentPane().add(rb);
f.setBounds(20,20,300,300);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}
eschew obfuscation
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