Java swing JPanel painting

D

dom.k.black

Hi,

I have a prototype, very simple Swing app. It consists of a MainFrame
with a JPanel based component.

I override the JPanel paintComponent() method to plot some data. This
involves quite a few floating point calculations, but not enough to
cause a noticeable delay (ie a small fraction of a second).

It works fine, until I resize the window (causing the JPanel to
resize). Then it messes up, some parts of the plot are correct, some
are displaced upwards in the Y direction. It definitely does a redraw.

I added a button which calls repaint() on the panel. If I press that,
the plot is fine again.

Is there anything I need to do when I resize? Should I call repaint()
after a resize? Is there an onResize event?

Regards

Dom
 
J

Jan Thomä

It works fine, until I resize the window (causing the JPanel to
resize). Then it messes up, some parts of the plot are correct, some
are displaced upwards in the Y direction. It definitely does a redraw.

Is there anything I need to do when I resize? Should I call repaint()
after a resize?

Probably a good idea.
Is there an onResize event?

You can use a component listener to get informed whenever a size change
occurs. Have a look at this link:
http://java.sun.com/docs/books/tutorial/uiswing/events/componentlistener.html

Best

regards,
Jan
 
M

Mark Space

Jan said:
Probably a good idea.

revalidate(), I think, on the highest level component you resize, I
think. Check the docs though, this is off-the-cuff.
 
K

Knute Johnson

Hi,

I have a prototype, very simple Swing app. It consists of a MainFrame
with a JPanel based component.

I override the JPanel paintComponent() method to plot some data. This
involves quite a few floating point calculations, but not enough to
cause a noticeable delay (ie a small fraction of a second).

It works fine, until I resize the window (causing the JPanel to
resize). Then it messes up, some parts of the plot are correct, some
are displaced upwards in the Y direction. It definitely does a redraw.

I added a button which calls repaint() on the panel. If I press that,
the plot is fine again.

Is there anything I need to do when I resize? Should I call repaint()
after a resize? Is there an onResize event?

Regards

Dom

If your painting code is written right, it will repaint automatically
when you resize. I think the suggestion of an SSCCE will allow us to
give you a real answer.
 
D

Daniel Pitts

Mark said:
revalidate(), I think, on the highest level component you resize, I
think. Check the docs though, this is off-the-cuff.
You shouldn't have to call revalidate or repaint when a window gets resized.

There is some other issue going on.
Please post an SSCCE, and don't please don't multi-post.
 
D

dom.k.black

You shouldn't have to callrevalidateor repaint when a window gets resized..

There is some other issue going on.
Please post an SSCCE, and don't please don't multi-post.

Sorry for the multi-post.

I think the problem is I am trying to display 90 degree rotated text.
Within the paint I am doing:

AffineTransform def = ((Graphics2D)gc).getTransform();
AffineTransform at = new AffineTransform();
at.setToRotation(-Math.PI/2.0);
((Graphics2D)gc).setTransform(at);

Then drawing the rotated text, then doing:

((Graphics2D)gc).setTransform(def);
 
D

dom.k.black

Sorry for the multi-post.

I think the problem is I am trying to display 90 degree rotated text.
Within the paint I am doing:

        AffineTransform def = ((Graphics2D)gc).getTransform();
        AffineTransform at = new AffineTransform();
        at.setToRotation(-Math.PI/2.0);
        ((Graphics2D)gc).setTransform(at);

Then drawing the rotated text, then doing:

((Graphics2D)gc).setTransform(def);- Hide quoted text -

- Show quoted text -

Sorry, posted before complete! Damn Google.

When I print using the method in my previous post, ie Graphics2D, it
messes up the first time but works on repaint.

Do I have to do something to initialise Graphics2D?
 
J

John B. Matthews

When I print using the method in my previous post, ie Graphics2D, it
messes up the first time but works on repaint.

Do I have to do something to initialise Graphics2D?

You left out the specification of gc; it may or may not be initialized.

Instead of replacing the AffineTransform of the graphics context with
setTransform(), why not apply an AffineTransform using transform()? You
can rotate around a point on the text's baseline using the method
getRotateInstance(double, double, double):

<http://java.sun.com/javase/6/docs/api/java/awt/geom/AffineTransform.htm>
<http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html>

See especially the warning on setTransform(). Also, consider a small,
complete example: <http://pscode.org/sscce.html>.
 
K

Knute Johnson

Sorry, posted before complete! Damn Google.

When I print using the method in my previous post, ie Graphics2D, it
messes up the first time but works on repaint.

Do I have to do something to initialise Graphics2D?

Your biggest problem was not posting a complete code example or
explaining yourself adequately. There is nothing complicated about
writing code to draw text at a different angle. Please do a search on
SSCCE. And now that we know what you really want, here is a simple
example to do it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class test extends JPanel {
public test() {
setPreferredSize(new Dimension(320,240));
}

public void paintComponent(Graphics g2d) {
Graphics2D g = (Graphics2D)g2d;

g.setColor(Color.WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(Color.BLUE);
g.rotate(Math.PI/2,getWidth()/2,getHeight()/2);
g.drawString("Hello World!",getWidth()/2-35,getHeight()/2);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test t = new test();
f.add(t,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
 
R

Roedy Green

I override the JPanel paintComponent() method to plot some data. This
involves quite a few floating point calculations, but not enough to
cause a noticeable delay (ie a small fraction of a second).

Things to play with:

setOpaque(true/false)

call/do not call super.paintComponent

clear/do not clear the background in your paintComponent.

--
Roedy Green Canadian Mind Products
http://mindprod.com

"The most significant trend in the US industry has been the decline in the amount
of energy recovered compared to energy expended. In 1916, the ratio was about 28
to 1, a very handsome energy return. By 1985, the ratio had dropped to 2 to 1,
and it is still dropping."
~ Walter Youngquist, Professor of Geology

By 2003, it had dropped to 0.5 to 1 in the US, making oil extraction no longer economically viable, no matter how high the price of crude.
 
R

Roedy Green

Please do a search on
SSCCE
see http://mindprod.com/jgloss/sscce.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"The most significant trend in the US industry has been the decline in the amount
of energy recovered compared to energy expended. In 1916, the ratio was about 28
to 1, a very handsome energy return. By 1985, the ratio had dropped to 2 to 1,
and it is still dropping."
~ Walter Youngquist, Professor of Geology

By 2003, it had dropped to 0.5 to 1 in the US, making oil extraction no longer economically viable, no matter how high the price of crude.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top