Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems width redraw / Scrolling a JPanel with 2d graphics

105 views
Skip to first unread message

Paul Emberson

unread,
Oct 24, 2001, 9:30:34 AM10/24/01
to
Hi,

Basically I want to draw some 2d graphics on a large area and be able to
scroll around.

Currently I have a class derived from a JPanel set as the viewport for a
JScrollPane with stuff drawn on the panel. The class implements a
scrollable interface. When I scroll around, everything becomes a mess as
the panel is not redrawn properly.

Here are some bits of code.

public Class DesignPanel extends JPanel implements Scrollable {

//paint on 2d graphics
public void paint(Graphics g) {

Graphics2D g2 = (Graphics2D)g;
g2.draw(new Rectangle2D.Float(10, 10, 100, 100));

//other draw stuff.
}

//scrollable implementation
public void getPreferredScrollableViewportSize() {
return getPreferredSize();
}

public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 5;

}

public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 5;
}

public boolean getScrollableTracksViewportHeight() {
return false;

}

public boolean getScrollableTracksViewportWidth() {
return false;
}

}

public class MyClass
{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(new Dimension(200, 200));

DesignPanel p = new DesignPanel();
p.setPreferredSize(new Dimension(400, 400));

JScrollPane jsp = new JScrollPane(p);

frame.getContentPane().add(jsp);
frame.setVisible(true);
}
}

Paul Emberson

unread,
Oct 24, 2001, 9:33:46 AM10/24/01
to
I should also mention that I tried to setup a ComponentListener that
called repaint() on the DesignPanel component when it moved. This
reduced the mess a bit but didn't fix the problem.

Paul

Babu Kalakrishnan

unread,
Oct 24, 2001, 10:14:01 AM10/24/01
to
On Wed, 24 Oct 2001 14:30:34 +0100, Paul Emberson <pme...@york.ac.uk> wrote:

>Basically I want to draw some 2d graphics on a large area and be able to
>scroll around.

>Currently I have a class derived from a JPanel set as the viewport for a
>JScrollPane with stuff drawn on the panel. The class implements a
>scrollable interface. When I scroll around, everything becomes a mess as
>the panel is not redrawn properly.

> //paint on 2d graphics

> public void paint(Graphics g) {
>
> Graphics2D g2 = (Graphics2D)g;
> g2.draw(new Rectangle2D.Float(10, 10, 100, 100));
>
> //other draw stuff.
> }

Don't override the paint method for Swing components. The default
implementation of paint() performs many more things than simple painting
of the component surface (double buffering for instance). The method you
should override for implementing your custom painting is
paintComponent(Graphics g).

Also in case your code does not fill up the entire surface of the
panel, you should call super.paintComponent() from within this
method so that your super class takes care of it for you.

BK

Linda Radecke

unread,
Oct 24, 2001, 10:35:58 AM10/24/01
to

Paul Emberson wrote:

> Hi,

> Basically I want to draw some 2d graphics on a large area and be able to
> scroll around.

>
> Currently I have a class derived from a JPanel set as the viewport for a
> JScrollPane with stuff drawn on the panel. The class implements a
> scrollable interface. When I scroll around, everything becomes a mess as
> the panel is not redrawn properly.

> Here are some bits of code.

> public Class DesignPanel extends JPanel implements Scrollable {

When I change the line:

> //paint on 2d graphics
> public void paint(Graphics g) {

to:
public void paintComponent(Graphics g) {
super.paintComponent(g);

it did not garble anymore in a short test.


>
> Graphics2D g2 = (Graphics2D)g;
> g2.draw(new Rectangle2D.Float(10, 10, 100, 100));
>
> //other draw stuff.
> }

Instead of:

> //scrollable implementation
> public void getPreferredScrollableViewportSize() {
> return getPreferredSize();
> }

I suppose you have meant:

public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}


[...]


Linda
--
li...@jalice.ch -> http://www.jalice.net
l.ra...@hswzfh.ch -> http://www.hswzfh.ch


Paul Emberson

unread,
Oct 24, 2001, 10:36:05 AM10/24/01
to
Many thanks Babu, works great now.

Paul

0 new messages