Discussion:
Flipping Y axis in a Graphics 2D??
(too old to reply)
Chris Grant
2003-12-20 01:40:59 UTC
Permalink
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 );
}
}
Raymond DeCampo
2003-12-20 05:40:09 UTC
Permalink
Post by Chris Grant
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
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?
Well, since the top left corner is (0,0), flipping the y-axis causes all
of your rendering to be done above the component where it will not be
seen. What you really want to do is flip about the line y = m, where m
is the y-coordinate of the midpoint of the component. You should be
able to do this by shifting up m pixels, then flipping and then shifting
down m pixels:

// Untested, uncompiled code
int m = getHeight()/2;
// last-specified-first-applied (see java.awt.Graphics2D#transform)
// last shift "down" (positive) m pixels
g2d.translate(0,m);
// flip about y-axis
g2d.scale(1,-1);
// first shift "up" (negative) m pixels
g2d.translate(0,-m);

HTH,
Ray
Josh D.King
2003-12-20 20:11:00 UTC
Permalink
Post by Raymond DeCampo
Post by Chris Grant
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
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?
Well, since the top left corner is (0,0), flipping the y-axis causes all
of your rendering to be done above the component where it will not be
seen. What you really want to do is flip about the line y = m, where m
is the y-coordinate of the midpoint of the component. You should be
able to do this by shifting up m pixels, then flipping and then shifting
// Untested, uncompiled code
int m = getHeight()/2;
// last-specified-first-applied (see java.awt.Graphics2D#transform)
// last shift "down" (positive) m pixels
g2d.translate(0,m);
// flip about y-axis
g2d.scale(1,-1);
// first shift "up" (negative) m pixels
g2d.translate(0,-m);
HTH,
Ray
A simple flip and translate will work as well
g2d.scale(1,-1);
g2d.translate(0,getHeight());

rather than shifting everything halfway, flipping, and then shifting
everything again
Alex Hunsley
2003-12-20 23:04:30 UTC
Permalink
Post by Chris Grant
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
Graphics2D g2d = (Graphics2D)getGraphics();
g2d.scale(1, -1);
The -1 is supposed to flip the y axis.
And it does!
Post by Chris Grant
Nothing draws at all.
This is what I'd expect...
Post by Chris Grant
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?
The reason you don't see anything is: by scaling all y coordinates by
-1, you're causing all positive coords to be plotted at negative
locations (and vice versa).. and the windows' visible space shows Y
coords from 0 up to 800. So you end up plotting stuff off the top of
your window, where you can't see it!

It sounds like you're wanting to mirror the Y coordinates about the
central horizontal line of the window.
So y=800 maps to y=0, and y=0 maps to y=800, with the same idea applying
to everything inbetween.

The equation to this mirroring is: y' = 800 - y.

IF you want to do this transformation in the Graphics2D object, as you
appear to want to do, you do it by first scaling the Y coords by -1
(like you're doing now), and then by adding 800 to all the Y coords (in
other words, a 'translation' of (0, 800)).
This will get you the result you want.
Look at Graphics2D's translate(dx,dy) command for more help with how to
call translation...

alex

Loading...