Skip to main content

(Last?) Painter refactoring

5 replies [Last post]
Anonymous

I'd like to change the signature of Painter.paint() to use a
generified Object instead JComponent. This means painters could be
used for more than just skinning components, but that their usage
would still be typesafe. Let me show you what I mean. JXComponent
could have methods like this:

setBackgroundPainter(Painter painter)

and any painter which only worked with components would look like this:

public class FooComponentPainter implements Painter {
public void paint(Graphics2D g, JComponent comp, int width, int
height);
}

A painter that would draw on top of any object (or even 'null') would
look like this:

public class MattePainter implements Painter {
public void paint(Graphics2D g, E obj, int width, int height);
}

I probably don't have the syntax right. At least it gives you an idea
of how it would work. It would let us do things like make a painter
just for use with the JXMapViewer, or create painters that work on
non-Swing components like the Netbeans Graph API.

So what do you think?

- Josh

- Blasting forth in three part harmony!

[att1.html]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mikaelgrev
Offline
Joined: 2006-09-27

I think it looks good. Just make sure the JavaDoc is good for the developers coming from 1.4.

Cheers,

Joshua Marinacci

Absolutely. Most people won't need to worry about it because the
standard painters will work with anything. You only need to worry
about it for your own custom painters.

- Josh

On Dec 6, 2006, at 12:01 PM, jdnc-interest@javadesktop.org wrote:

> I think it looks good. Just make sure the JavaDoc is good for the
> developers coming from 1.4.
>
> Cheers,
> [Message sent by forum member 'mikaelgrev' (mikaelgrev)]
>
> http://forums.java.net/jive/thread.jspa?messageID=183427
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

- Blasting forth in three part harmony!

[att1.html]

Sam Berlin

In order to do this, the signature would be something like:
void setBackgroundPainter(Painter painter);

So JXComponent would have:
void setBackgroundPainter(Painter painter);

and JXPanel would have:
void setBackgroundPainter(Painter painter);

Unfortunately, there's no way to automatically extend the type to the
current class it's being defined it. So every subclass of JXComponent
would need to define its own signature. Even worse, because the
generic signatures are different, JXPanel can't say
'super.setBackgroundPainter(painter)', because there's the chance that
the painter you set in JXPanel has JXPanel as it's generified type,
which JXComponent doesn't allow.

Generics might not be the best here. :/

Sam

On 12/6/06, Joshua Marinacci wrote:
> Absolutely. Most people won't need to worry about it because the standard
> painters will work with anything. You only need to worry about it for your
> own custom painters.
>
> - Josh
>
>
> On Dec 6, 2006, at 12:01 PM, jdnc-interest@javadesktop.org wrote:
>
> I think it looks good. Just make sure the JavaDoc is good for the developers
> coming from 1.4.
>
> Cheers,
> [Message sent by forum member 'mikaelgrev' (mikaelgrev)]
>
> http://forums.java.net/jive/thread.jspa?messageID=183427
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net
>
> - Blasting forth in three part harmony!
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

rbair
Offline
Joined: 2003-07-08

> So what do you think?

As you know, I'm in favor of the idea. There are a lot of painters (even ones we've written so far) that don't really need the component to do their thing. For example, the shape painters couldn't really care less. By using Object instead of JComponent, we allow painters to be used for a lot more use cases, and allow a greater chance to reuse.

I've always been a bit queesy about having Painter reference JComponent.

Richard

Joshua Marinacci

Agreed. I don't know why it took me so long to do it. It seems
obvious in retrospect. :)

I do have one question on generics that I'm hoping someone can help
me with.

If I have two painters, one that is generic and one that only works
on labels, how can I structure the JXComponent api to not allow the
label painter to be set on the JXPanel?

Ex:

public FooPainter implements Painter {
public void paint(Graphics g, Object o, int w, int h) { }
}
public LabelPainter implements Painter {
public void paint(Graphics g, JXLabel l, int w, int h) { }
}

I want this to work:
JXLabel label = new JXLabel();
label.setBackgroundPainter(new FooPainter());
label.setBackgroundPainter(new LabelPainter());
JXPanel panel = new JXPanel();
panel.setBackgroundPainter(new FooPainter());
panel.setBackgroundPanel(new LabelPainter());

The last line should be a compile error. If not then it will fail at
runtime with a class cast exception. What should be the signature of
setBackgroundPainter to make this work?

- J

On Dec 5, 2006, at 4:18 PM, jdnc-interest@javadesktop.org wrote:

>> So what do you think?
>
> As you know, I'm in favor of the idea. There are a lot of painters
> (even ones we've written so far) that don't really need the
> component to do their thing. For example, the shape painters
> couldn't really care less. By using Object instead of JComponent,
> we allow painters to be used for a lot more use cases, and allow a
> greater chance to reuse.
>
> I've always been a bit queesy about having Painter reference
> JComponent.
>
> Richard
> [Message sent by forum member 'rbair' (rbair)]
>
> http://forums.java.net/jive/thread.jspa?messageID=183060
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jdnc-unsubscribe@jdnc.dev.java.net
> For additional commands, e-mail: jdnc-help@jdnc.dev.java.net

- Blasting forth in three part harmony!

[att1.html]