painting on a JPanel in a Java Application

Ok, I know I keep coming here for Java help. I would love to go to a newsgroup but I honestly have no idea how to access them. :frowning:

Here is the situation.

Class: CubeWindow, extends JFrame, implements WindowListener and KeyListener.

CubeWindow constructor creates two JPanels and some components that will go inside the JPanels. It listens to itself for KeyEvents and WindowEvents.

Inner Class CubeScroller extends JSlider implements ChangeListener.

CubeScroller constructor calls JSlider constructor and listens to itself for stateChange events.



if (source == rControl) {
	int r = rControl.getValue();
	int g = gControl.getValue();
	int b = bControl.getValue();
        String rgb = new String("RGB Setting: " + r + ", " + g + ", " + b + ".");
	rgbDisplay.setText(rgb);
	Color newColor = new Color(r, g, b);
	paper.setBackground(newColor);

“paper” is one of the JPanels. It has no components put inside of it. The code above is simply one of the cases for the stateChange event for one of three very similar sliders.

So, to be perfectly clear, JFrame holds two JPanels. One JPanel is empty and is the target of events from the JSliders. the other JPanel holds the JSliders.

When I use the code given above, the “paper” JPanel changes color very nicely. I can access the whole color spectrum. No problems.

NOW, i eventually want to draw in the paper frame. When I attempt to do so I create another inner class called Paper which extends JPanel. I call the super() constructor for the Paper constructor (and do nothing else) and override its paint method to draw a single line (this is ALL the Paint() method does). that is the entirety of the class definition. Thus, instead of “paper” being a JPanel it is now a “Paper” object, sublassed from JPanel. Seems simple enough…

In the control JPanel are two sliders which set the x and y coordinates for a line (the other point stays 0, 0). Each time those particular sliders change I call paper.repaint().

If I then attempt to run the code it ends up that I cannot change the background color at all, ghosts of the slider I’m using appears in the paper panel (no matter which slider), and the line gets drawn but old ones don’t disappear.

What the hell is going on??? Why is anything different? What do i need to put in that overridden paint() method to get the background to correctly display, and the lines to work, and the damn ghosts to go away?

I can display any code which may help, or email the entire file (just a few k and its only one class file-- everything is inner classes when necessary). But I am totally baffled. It seems since I subclass JPanel nothing should really be causing this. :confused: :mad:

I should add, if I have the red green and blue sliders change the color of the line to draw there is no problem either… (I think that’s how I got it to work, but it still never cleared the Paper panel, but no ghosts anyway). So there is definitely som strange interaction with the paper.setBackground and its overridden paint() method.

I did hunt all over Sun’s website for some ideas, and I have been reading my Java books for further advice, but nothing was apparent. (I’ve been working on this all day, nonstop, frustrating fucking thing).

Every time you call repaint you are just drawing a line on top of whatever else was there. You probably need to also draw the background, or call super.paint or something. Can you post the paint methods at least?

Also, I don’t understand - it seems like you say you have a “paper” JPanel that changes color nicely, but then you have an inner class that paints the line? Why doesn’t your paper JPanel paint the line? Seems like if you have a subclass of JPanel that is working OK, just adding a subclassed paint() method should do it.

If you are doing this because you are just using a vanilla JPanel, you could do an anonymous inner class like


JPanel paperJPanel = new JPanel() {
  public void paint(Graphics g) {
    super.paint();
    // paint line here
  }
};

I guess the main thing I want to say is that if you are happy with some behavior and just want to add a little bit, you must use super.foo(). If you want to completely throw out the behavior and make your own possibly empty behavior, do not call super.foo().

Fucking a, it was that simple, wasn’t it? sigh

It works correctly now, thank you.

Can I turn this into a thread about how to access newsgroups or should I just keep coming to my faithful doper friends?

To access Usenet groups, just get the NNTP server address for your ISP (something like news.domain.com is typical, similar to many mailservers being mail.domain.com) and then grab a Usenet client. I use FreeAgent as a client, but there are lots of freeware clients available and a lot of browsers handle news too. Try the list here:
http://dir.yahoo.com/Computers_and_Internet/Software/Internet/Usenet/

Once you have a client and a server, check out the comp.lang.java.* groups. They have pretty active forums.

If Usenet is too much trouble, there are also some good Java-specific web forums at the Sun site:
http://forum.java.sun.com/forum.jsp?forum=4

Hey micco! You helped me on Java before. I must say that last week I couldn’t even get a window to close. I think I am coming along nicely :slight_smile:

Thanks for the Usenet help…

Also wanted to add, this particular excersize is drawing a representation of a 3D cube and rotating it, and it almost works right now. I can stretch the “face” but for some reason it doesn’t actually rotate :frowning:

anyone who wants to see the code for this is welcome to help me on the project. (I know someone here asked before about 3D representations-- ultrafilter maybe?)