Enoon
Enoon

Reputation: 421

JPanel does not paint when scaling with Affine Trasform

my application layout is something like: custom JFrame (which just handles the creation of the gui) which contains a standard JPanel which contains a custom JPanel

Inside the custom JPanel, which is called MinimapPanel, I changed the paint method:

    //in a constructor:
    scaledTransform = new AffineTransform(); = new AffineTransform();
    scaledTransform = new AffineTransform();
    scaledTransform.scale(scaleAmount, scaleAmount);
    //...
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setTransform(scaledTransform);
    mapDrawer.paintMinimap(g, seatMap, miniViewHandler);//it just calls a bunch of fillRect

    if (viewRect != null) {
        g.setColor(new Color(255, 0, 0));
        int x = viewRect.x;
        int y = viewRect.y;
        g.drawRect(x, y, Math.abs(viewRect.width), Math.abs(viewRect.height));
    }
    g2d.setTransform(plainTransform);
}

Everything works fine if I don't apply the trasform, or if the scaling is by 1.0 (none), but if I scale, everytime the JFrame repaints, the MinimapPanel stays blank.

Any ideas on what I could be doing wrong?

Upvotes: 3

Views: 618

Answers (1)

StanislavL
StanislavL

Reputation: 57421

Don't use clear transforms. Previous components add their own transformations e.g. translate() to child position etc. Rather call the code like this

AffineTransform old=g2d.getTransform();
//do your changes here
g2d.scale(scaleAmount, scaleAmount);
//paint
g2d.setTransform(old);

Upvotes: 5

Related Questions