This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Good User Interface

 
Sbusiso Nhlumayo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
praveen kumar gudapati
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic