Re: Drawing with Extended JPanel Class

philju <[email protected]> Tue, 5 Aug 2008 03:03:23 -0700 (PDT)
Newsgroups gmane.comp.java.netbeans.user-interface
Message-ID <[email protected]>
Hi Wade,

Sorry for the mis-posting, I'm new to this forum!

I thought adding the bean to the palette might be essential, in which case I
have an additional problem...

Below is the code for my extended JPanel, and currently it resides in the
root - "" of the project source folder. I believe I've met all of the
JavaBeans requirements and as such NetBeans registers the class as a valid
bean when attempting add it from the project folder using the palette
manager. I select the bean and the palette category, no message is generated
which leads me to believe the process has been successful. However, when I
access the palette it isn't visible, even following a refresh. 

Dragging the class from the project navigator onto the form for visual
development doesn't result in anything happening either. Any thoughts?

Can I also ask if including a second, non empty constructor would violate
the JavaBeans coding convention?

Please let me know if you would prefer I re-post this in nbusers.

Cheers,
Phil

-------------------------------------------------------------------------------------------
import draw.ValueToColour;
import java.awt.Color;
import java.awt.Graphics;
import java.io.Serializable;
import javax.swing.JPanel;

public class LegendPanel extends JPanel implements Serializable {
    
    private String title;
    private String colour;
    private int min;
    private int max;
    
    public LegendPanel() {
        
    }
    
    public void paint(Graphics g) {
        
        super.paintComponent(g);
        Color c = null; 
        ValueToColour vc = new ValueToColour();
        
        g.clearRect(0, 0, 402, 50);
        
        for (int i = 0; i < 400; i++) {

            // Set chosen colour
            if (colour == "jet") {
                c = vc.toJet(i, 0, 400);
            } else if (colour == "plasma") {
                c = vc.toPlasma(i, 0, 400);
            } else if (colour == "lava") {
                c = vc.toLava(i, 0, 400);
            } else if (colour == "greenscale") {
                c = vc.toGreenscale(i, 0, 400);
            } else {
                c = vc.toGreyscale(i, 0, 400);
            }
            g.setColor(c);
            g.fillRect(i+1, 1, i+1, 25);
        }

        c = new Color(0, 0, 0);
        g.setColor(c);
        g.drawRect(0, 0, 401, 25);
        g.drawString(String.valueOf(min), 0, 40 );
        // In if statement... for correct data type and max value
        
        String maxString = String.valueOf(max);
        g.drawString(maxString, 400 - (maxString.length() * 6), 40 );
        g.drawString( title, 200 - ((title.length() * 6) / 2), 40 );
    }
    
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getColour() {
        return this.colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public int getMin() {
        return this.min;
    }

    public void setMin(int min) {
        this.min = min;
    }

    public int getMax() {
        return this.max;
    }

    public void setMax(int max) {
        this.max = max;
    }
}

-------------------------------------------------------------------------------------------



Wade Chandler wrote:
> 
> Please use nbusers for such questions next time. nbui is not a general
> help forum, but is only used to work on the UI of the IDE itself.
> 
> On your query, the class needs to be either added to the palette and
> dragged onto the form, or dragged onto the form from the project view, as
> long as the project whose form is being dragged onto has all the classes
> being used on its classpath and their dependencies. Then NB will use an
> instance of your class and its interface versus you using a JPanel and
> using your instance polymorphically which uses the JPanel interface.
> 
> *interface = methods and fields provided by the class provided by the
> variable type
> 
> Wade
> 
>  ==================
> Wade Chandler, CCE
> Software Engineer and Developer, Certified Forensic Computer Examiner,
> NetBeans Dream Team Member, and NetBeans Board Member
> http://www.certified-computer-examiner.com
> http://wiki.netbeans.org/wiki/view/NetBeansDreamTeam
> http://www.netbeans.org
> 
> 
> 
> ----- Original Message ----
>> From: philju 
>> To: [email protected]
>> Sent: Monday, August 4, 2008 12:09:52 PM
>> Subject: [nbui] Drawing with Extended JPanel Class
>> 
>> 
>> Hi all,
>> 
>> I have produced a class that extends JPanel in order to override the
>> paint
>> method and ensure that an image is painted on the panel following an a
>> call
>> to the component's repaint() method. The modified paint method includes
>> conditional code that selects the appropriate colour scheme according to
>> a
>> configured string option.
>> 
>> My problem relates to the fact that additional methods to change the
>> string
>> are inaccessible as the object type is fixed as JPanel with NetBeans and
>> not
>> my extended class, therefore the colour of the graphic is fixed according
>> to
>> the value passed as a parameter to the constructor.
>> 
>> How do I either produce a dynamic process to achieve this functionality
>> with
>> an existing JPanel in a way that ensures the configured graphic remains
>> on
>> it following a repaint() or how do I instantiate the object as my
>> extended
>> class and include it in the GUI code generated by NetBeans so that the
>> layout of the form is maintained?
>> 
>> I'm unsure if any code would assist this query, but please let me know if
>> any is required.
>> 
>> Cheers,
>> Phil
>> -- 
>> View this message in context: 
>> http://www.nabble.com/Drawing-with-Extended-JPanel-Class-tp18814353p18814353.html
>> Sent from the Netbeans - UI mailing list archive at Nabble.com.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Drawing-with-Extended-JPanel-Class-tp18814353p18828080.html
Sent from the Netbeans - UI mailing list archive at Nabble.com.