Forum Stats

  • 3,783,607 Users
  • 2,254,809 Discussions
  • 7,880,481 Comments

Discussions

Jbuilder and custom components.

843807
843807 Member Posts: 46,582
edited Feb 17, 2003 2:43AM in Java Desktop Applications
Hi, I'm using Jbuilder 7 and the article from swing connection about painting objects using the 2d libraries (http://java.sun.com/products/jfc/tsc/articles/swing2d/index.html#CUSTOM_PAINTING).

I created a rounded translucent button by overriding the paintcomponent method of JButton. When I add this button to a form (regardless of whether I am using the designer or not) and then switch to design mode for that form, jbuilder hangs. The program compiles, and my buttons look suitably pretty, but that's no consolation if I can't use any of the visual design tools.

I did some experimenting and found the following:
- if I remove overridden paintComponent method, the problem doesn't occur
- if I delete the public modifier for the class (make it accessible only to its package), then the designer doesn't crash, but shows the component as a "red bean". Having a red bean is ok, but I need all my packages to have access to this button.

If anyone could tell me how to use my component and still be able to visually design my form it would be great.

cheers

david

Comments

  • 843807
    843807 Member Posts: 46,582
    i thought the source code might be useful.

    public class SeeThroughButton extends JButton {

    private Color bgColor;
    private Color darkColor;
    private int opacity = 75;
    private static Image glassImage = new ImageIcon("images/button4.png").getImage();

    public SeeThroughButton(String label) {
    super(label);
    setContentAreaFilled(false);
    }
    public SeeThroughButton() {
    super();
    setContentAreaFilled(false);
    }

    public boolean isOpaque() {
    return false;
    }
    public void setOpacity(int value) {
    opacity = (value < 255) ? value : 255;
    }
    public int getOpacity() {return opacity;}

    public void setBackground(Color col) {
    bgColor = new Color(col.getRed(), col.getGreen(), col.getBlue(), opacity);
    darkColor = new Color(col.darker().getRed(), col.darker().getGreen(), col.darker().getBlue(), opacity);
    }
    protected void paintComponent(Graphics g) {
    int width = getWidth();
    int height = getHeight();

    if (getModel().isArmed()) {
    g.setColor(darkColor);
    g.fillRoundRect(0, 0, width, height, 10, 10);
    }
    else {
    g.setColor(bgColor);
    g.fillRoundRect(0, 0, width, height, 10, 10);
    }

    super.paintComponent(g);

    g.drawImage(glassImage,0,0,width,height,this);
    }
    protected void paintBorder(Graphics g) {
    if (getModel().isArmed()) {
    g.setColor(bgColor.darker());
    g.drawRoundRect(0, 0, getSize().width-1,
    getSize().height-1, 10, 10);
    }
    else {
    g.setColor(Color.white);
    g.drawRoundRect(1, 1, getSize().width-1,
    getSize().height-1, 10, 10);
    g.setColor(bgColor.darker());
    g.drawRoundRect(0, 0, getSize().width-1,
    getSize().height-1, 10, 10);
    }
    }
    }
  • 843807
    843807 Member Posts: 46,582
    hi,

    you should take a look at:

    http://java.sun.com/products/javabeans/
    http://java.sun.com/products/javabeans/training.html
  • 843807
    843807 Member Posts: 46,582
    Thanks for the response. The problem is more jbuilder specific than that though. Just in case anyone else is having the same problem, i'll explain what i've learnt in the last few days.

    jbuilder has a very particular image loading mechanism for the visual designer. When it cannot find an image that it is using for the paintcomponent routine, the designer hangs. The correct calls for loading an image are:

    private static Image glassImage;
    private static ImageIcon image1;
    image1 = new ImageIcon(transbuttons.SeeThroughButton.class.getResource("button4.png"));
    glassImage = image1.getImage();

    change the names of the buttons and objects to suit your own setup of course. Do it in one line if you feel like it.

    When you load the image in this way, everything is fine. Introspection and other bean properties aren't required if you are inheriting from a swing component like I am. A good side effect of loading the image this way is that it also can be loaded from a jar file when you are distributing.

    - david
  • 843807
    843807 Member Posts: 46,582
    try:
    String fileName =
    JavaMain.class.getProtectionDomain().getCodeSource().getLocation().getFile()
    This will return the file name of either the .class file or .jar file -
    depending on where it was loaded from.
This discussion has been closed.