I have a JPanel with a GridBagLayout. In one place a JPanel is shown,
which has a lot of custom paintig on it. What I wanted to achieve was
have a rectangle move with the mouse over the JPanel with the custom
painting, without changing the painting and repaintig the whole JPanel
everytime the mouse moves.
So what I did was make another JPanel with the same size as the other
one and place it over the one with the custom painting, futhermore I
made it non-opaque. I figured that only the Rectangle on this new
JPanel would be repainted() and always shown above the other custom
painting.
Everything seems to work fine, since I found out that nothing is wrong
with the code as it is. Yet the recangle is not shown, although I have
the feeling it is painted and directly painted over again.
Does anyone have a clue what point I might be missing? Or any idea how
to solve that problem in another way.
Thank you for answering in advance I'm desperatly waiting.
Andrew Thompson - 14 Dec 2004 17:27 GMT
> I have a JPanel with a GridBagLayout. In one place a JPanel is shown,
> which has a lot of custom paintig on it. What I wanted to achieve was
> have a rectangle move with the mouse over the JPanel with the custom
> painting, without changing the painting and repaintig the whole JPanel
> everytime the mouse moves.
Store your complex rendering in a BufferedImage.
In paintComponent(), paint the rectangle on the BI and simply
Graphics.drawImage() the result.
HTH

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Csaba Nebel - 14 Dec 2004 18:57 GMT
Hi
My problem is: You can add a JPanel into JSsrollPane.
When the JPanel size is bigger than the JScrollPane size than you can scroll the JPanel.
Thx.
Andrew Thompson - 14 Dec 2004 21:10 GMT
> My problem is: You can add a JPanel into JSsrollPane.
That does not seem like a problem.
> When the JPanel size is bigger than the
> JScrollPane size than you can scroll the JPanel.
Which would be the behaviour expected of a JScrollPane.
Again, what is the problem?
I suggest you
- Supply a piece of code that displays the behaviour[1]
- Describe how it (actual) behaves for you
- Describe how you would *like it* (expected) to behave.
- Detail the differences between 'actual' and 'expected'
Thus far you've described point two, but the other three are
a mystery.
[1] <http://www.physci.org/codes/sscce.jsp>
Also, please hit the 'enter' key before your lines become long.
About 72 characters wide is the accepted maximum.
HTH

Signature
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane
Andrei Kouznetsov - 16 Dec 2004 09:27 GMT
>I have a JPanel with a GridBagLayout. In one place a JPanel is shown,
> which has a lot of custom paintig on it. What I wanted to achieve was
> have a rectangle move with the mouse over the JPanel with the custom
> painting, without changing the painting and repaintig the whole JPanel
> everytime the mouse moves.
public class MouseHandler extends MouseAdapter {
Rectangle lastRect;
public void mouseDragged(MouseEvent e) {
Component comp = e.getComponent();
Graphics g = comp.getGraphics();
g.setXORMode(someColor);
if(lastRect != null) {
g.drawRect(lastRect.x, lastRect.y, lastRect.width, lastRect.height);
}
//you should only implement computeRect();
Rectangle r = computeRect();
g.drawRect(r.x, r.y, r.width, r.height);
lastRect = r;
g.dispose();
}
}//end MouseHandler
if you become performance problems wile drawing (big) rectangle you should
draw 4 lines instead!
> Everything seems to work fine, since I found out that nothing is wrong
> with the code as it is. Yet the recangle is not shown, although I have
> the feeling it is painted and directly painted over again.
> Does anyone have a clue what point I might be missing? Or any idea how
> to solve that problem in another way.
post some example code

Signature
Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities