• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Good User Interface

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic