aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Good User Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Good User Interface" Watch "Good User Interface" New topic
Author

Good User Interface

Sbusiso Nhlumayo
Greenhorn

Joined: Sep 22, 2003
Posts: 6
Hi
I want to create a star button such that when you click
the defining rectangle, the button does not respond but
only when you click the star figure.
A sample code will be acceptable.
Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi,
Welcome to JavaRanch!
OK, now that we're in the right forum: have a look at this document about the lightweight component framework.
[ September 24, 2003: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
praveen kumar gudapati
Greenhorn

Joined: Sep 26, 2003
Posts: 15
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;

public class NextButton
extends JButton {
Shape shape;
public NextButton() {
// This allows us to paint a round background.
setContentAreaFilled(false);
}
public NextButton(String label) {
super(label);
// These statements enlarge the button so that it
// becomes a circle rather than an oval.
/* Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width,
size.height);
setPreferredSize(size);*/
// This call causes the JButton not to paint
// the background.
// This allows us to paint a round background.
setContentAreaFilled(false);
}
// Paint the round background and label.
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
// You might want to make the highlight color
// a property of the RoundButton class.
g.setColor(Color.lightGray);
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
g.setColor(getBackground());
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
if (isEnabled()) {
g.setColor(new Color(255, 240, 220));
}
int[] xpoints = {
0, getSize().width / 2, getSize().width / 2, getSize().width - 1, getSize().width / 2,
getSize().width / 2, 0
};
int[] ypoints = {
getSize().height / 4, getSize().height / 4, 0, getSize().height / 2 - 1,
getSize().height - 1, getSize().height * 3 / 4, getSize().height * 3 / 4
};
int npoints = 7;
g.fillPolygon(xpoints, ypoints, npoints);
// This call will paint the label and the
// focus rectangle.
super.paintComponent(g);
}
//************************************************************************************************************************************
// Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
int[] xpoints = {
0, getSize().width / 2, getSize().width / 2, getSize().width - 1, getSize().width / 2,
getSize().width / 2, 0
};
int[] ypoints = {
getSize().height / 4, getSize().height / 4, 0, getSize().height / 2 - 1,
getSize().height - 1, getSize().height * 3 / 4, getSize().height * 3 / 4
};
int npoints = 7;
g.drawPolygon(xpoints, ypoints, npoints);
}
//************************************************************************************************************************************
// Hit detection.
public boolean contains(int x, int y) {
// If the button has changed size,
// make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
int[] xpoints = {
0, getSize().width / 2, getSize().width / 2, getSize().width - 1,
getSize().width / 2, getSize().width / 2, 0
};
int[] ypoints = {
getSize().height / 4, getSize().height / 4, 0, getSize().height / 2 - 1,
getSize().height - 1, getSize().height * 3 / 4, getSize().height * 3 / 4
};
int npoints = 7;
shape = new Polygon(xpoints, ypoints, npoints);
}
return shape.contains(x, y);
}
//************************************************************************************************************************************
}
This is just an example how you can customize the button shapes, so you can change the xpoints, ypoints and npoints to get your desired shape.
Sbusiso Nhlumayo
Greenhorn

Joined: Sep 22, 2003
Posts: 6
Thanks a lot.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Good User Interface