Java swing JPanel painting

Discussion in 'Java' started by dom.k.black@googlemail.com, Apr 8, 2009.

  1. Guest

    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
    , Apr 8, 2009
    #1
    1. Advertising

  2. Jan Thomä Guest

    On 2009-04-08 15:37:40 +0200, ""
    <> said:
    > 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
    Jan Thomä, Apr 8, 2009
    #2
    1. Advertising

  3. Mark Space Guest

    Jan Thomä wrote:
    > On 2009-04-08 15:37:40 +0200, ""
    > <> said:
    >> 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.
    >


    revalidate(), I think, on the highest level component you resize, I
    think. Check the docs though, this is off-the-cuff.
    Mark Space, Apr 8, 2009
    #3
  4. wrote:
    > 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.

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 8, 2009
    #4
  5. Daniel Pitts Guest

    Mark Space wrote:
    > Jan Thomä wrote:
    >> On 2009-04-08 15:37:40 +0200, ""
    >> <> said:
    >>> 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.
    >>

    >
    > 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.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Apr 9, 2009
    #5
  6. Guest

    On 9 Apr, 16:47, Daniel Pitts
    <> wrote:
    > Mark Space wrote:
    > > Jan Thomä wrote:
    > >> On 2009-04-08 15:37:40 +0200, ""
    > >> <> said:
    > >>> 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.

    >
    > >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 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.
    >
    > --
    > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>- Hide quoted text -
    >
    > - Show quoted text -


    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);
    , Apr 10, 2009
    #6
  7. Guest

    On 10 Apr, 22:01, ""
    <> wrote:
    > On 9 Apr, 16:47, Daniel Pitts
    >
    >
    >
    >
    >
    > <> wrote:
    > > Mark Space wrote:
    > > > Jan Thomä wrote:
    > > >> On 2009-04-08 15:37:40 +0200, ""
    > > >> <> said:
    > > >>> 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.

    >
    > > >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 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.

    >
    > > --
    > > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > 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?
    , Apr 10, 2009
    #7
  8. In article
    <>,
    "" <> wrote:

    > > 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);
    > >

    > 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>.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Apr 11, 2009
    #8
  9. wrote:
    > On 10 Apr, 22:01, ""
    > <> wrote:
    >> On 9 Apr, 16:47, Daniel Pitts
    >>
    >>
    >>
    >>
    >>
    >> <> wrote:
    >>> Mark Space wrote:
    >>>> Jan Thomä wrote:
    >>>>> On 2009-04-08 15:37:40 +0200, ""
    >>>>> <> said:
    >>>>>> 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.
    >>>> 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 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.
    >>> --
    >>> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>- Hide quoted text -
    >>> - Show quoted text -

    >> 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?


    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);
    }
    });
    }
    }

    --

    Knute Johnson
    email s/nospam/knute2009/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Apr 11, 2009
    #9
  10. Roedy Green Guest

    On Wed, 8 Apr 2009 06:37:40 -0700 (PDT), ""
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >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.
    Roedy Green, Apr 11, 2009
    #10
  11. Roedy Green Guest

    On Fri, 10 Apr 2009 17:34:59 -0700, Knute Johnson
    <> wrote, quoted or indirectly quoted
    someone who said :

    >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.
    Roedy Green, Apr 11, 2009
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesse Pavel

    Swing Menu Painting, 2D Demo

    Jesse Pavel, Feb 15, 2004, in forum: Java
    Replies:
    2
    Views:
    812
    Jesse Pavel
    Feb 18, 2004
  2. matt

    Swing Painting Problems

    matt, Mar 28, 2007, in forum: Java
    Replies:
    2
    Views:
    350
  3. Kgbol
    Replies:
    4
    Views:
    1,138
  4. Evan

    Swing painting problems

    Evan, Mar 8, 2008, in forum: Java
    Replies:
    1
    Views:
    404
    Peter Duniho
    Mar 8, 2008
  5. ahd292

    Painting in JPanel

    ahd292, Dec 17, 2008, in forum: Java
    Replies:
    2
    Views:
    2,504
    Knute Johnson
    Dec 17, 2008
Loading...

Share This Page