?
Solved

Inverting y-axis for drawing graph

Posted on 2003-02-20
9
Medium Priority
?
872 Views
Last Modified: 2009-11-01
Hey Experts,
Just wondering if there is a simple way you can change the y-coordinate system of the Java Graphics object so that a -ve value is drawn below the origin and a +ve value is drawn above.  i.e. if I draw a point at 30,-45 it is really being drawn at 30,45.  I am drawing a grid but my y-axis is inverted.  Short of negating every y-value in my drawGrid method, is there any other way to do this?
0
Comment
Question by:robin_at_mri
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7987564
You can. This move the origin to its usual position and y increases upwards:

public void paint(Graphics _g) {
     java.awt.Graphics2D g = (java.awt.Graphics2D)_g;
     // Move origin to 'normal' location, i.e. bottom left corner
     g.translate(0, getBounds().height);
     // Multiply y coordinates so that y increases 'normally', i.e. upwards
     g.scale(1.0, -1.0);
     // Red line
     g.setPaint(Color.red);
     // from origin to (40,40)
     g.drawLine(0,0,40,40);
}
0
 

Author Comment

by:robin_at_mri
ID: 7988419
So the problem wtih that approach is that it is mangling my axis labels.  a -10 is being printed upside down.  If I invert the scaling(again, to flip text right side up), the axis will be printed in the wrong position.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7989176
Then you need to restore the default transform to do 'normal' stuff. Save it at the beginning with

AffineTransform originalTransform = g.getTransform();

// Shift the axes then do your standard Cartesian stuff

g.setTransform(originalTransform);

// Now we're back in user/device space coordinates

// Do your labels

// Back to Cartesian if you want
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 7993872
It might be more convenient still to have two transforms - the default, and your 'normal axes' one, then you can just switch between them.

0
 

Author Comment

by:robin_at_mri
ID: 7995446
But if I switch to a normal transformation won't my 20 label for 20 units in the y-axis be drawn in the -20 position?


Rob
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7996636
Not if you call it with the default (identity) transformation.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 80 total points
ID: 7996652
The point is that things are drawn with the transformation that's set at the time of the call, so you can have a whole sequence of different tranforms.
0
 

Author Comment

by:robin_at_mri
ID: 8280285
Sorry that it took so long to award the points!!!

Rob
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8284735
;-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question