Flipping Y axis in a Graphics 2D??

From: Chris Grant (cwgrc2_at_msn.com)
Date: 12/20/03


Date: 19 Dec 2003 17:40:59 -0800

Hello:

I am trying to do something that is supposed to be simple (?). In the
attached code, I have drawn a red dot at top right and blue dot at
lower left. I am tryin to flip the Y axis so I expect the red dot at
the lower right and the blue at the top left using:

    Graphics2D g2d = (Graphics2D)getGraphics();
    g2d.scale(1, -1);

The -1 is supposed to flip the y axis. Nothing draws at all. If you
comment out the g2d.scale(1, -1); line, then everything works fine.
What am I doing wrong. Can you please show how I would correct it to
do what I want?

Thanks,

Chris

package com.lgc.geostats.object.gui.dataconditioning;

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

public class TestTransform extends JFrame {

  private boolean _dragging=false;

  TestTransform() {
    setSize(800, 800);
    setVisible(true);
    drawDots();
  }

  private void drawDots() {

    Graphics2D g2d = (Graphics2D)getGraphics();
    g2d.scale(1, -1);

    int w = getWidth();
    int h = getHeight();

    g2d.setColor(Color.RED);
    g2d.fillOval((int)(0.75 * w), (int)(0.10*h), 15, 15);

    g2d.setColor(Color.BLUE);
    g2d.fillOval((int)(0.25 * w), (int)(0.9 * h), 15, 15);
  }
  public void paintComponent( Graphics g ) {
    drawDots();
  }

  public void update(Graphics g) {
    super.update(g);
    drawDots();
  }

  public void repaint(Graphics g) {
    drawDots();
  }

  public void paint(Graphics g) {
    drawDots();
  }

  public static void main( String args[] )
  {
    try {
      UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName() );
    }
    catch (Exception e) {
      System.out.println("Exception thrown on look and feel");
      e.printStackTrace();
    }
    TestTransform objectApp = new TestTransform();
    objectApp.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  }
}