0

I am trying to plot a graph using the java 2d graphics library and I thought I had it. I want to plot in the coordinate system where 0,0 is in the center of the panel on the left edge. I used the following code and it seemed to give me the result I needed.

private void doDraw(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  AffineTransform saveAT = g2d.getTransform();
  // get the height of the panel
  int height = getHeight();
  // Find the middle of the panel
  double yTrans = ((double)height)/2.0;
  AffineTransform tform = AffineTransform.getTranslateInstance( 0.0, yTrans);
  g2d.setTransform(tform);
  //draw the line for the x-axis.
  g2d.drawLine(0,  0, 100, 0);
  //restore the old transform
  g2d.setTransform(saveAT);
}

This plotted the origin centered in the window.

The problem shows itself when I added a menu. Then the origin was offset in the y direction about twice the size of the menu higher then it should be. Do I need to account for the size of the menu and other containers that I add to the panel?

private void doDraw(Graphics g) {
  Graphics2D g2d = (Graphics2D) g.create();

  int height = getHeight();
  double yTrans = ((double)height)/2.0;
  AffineTransform tform = AffineTransform.getTranslateInstance( 0.0, yTrans);
  g2d.transform(tform);
  //draw the line for the x-axis.
  g2d.drawLine(0,  0, 100, 0);
}

works, thank you for your help

Sting
  • 363
  • 6
  • 18
  • Are you saying that what you observed was that restoring the old transform did not work, as in if you commented out the last line you would still see the same results? – NESPowerGlove Apr 21 '14 at 19:58
  • No, The line which is supposed to be the origin was higher in the JPanel when the menu was added then without the Menu. Without the menu, the origin was centered in the JPanel. – Sting Apr 21 '14 at 20:07
  • In your paintComponent method, do you call the super method, and then call this private draw method? Also, can you just use the transform method instead? To use that method, you would create a new Graphics object from g.Create(), then with that new object call transform, then draw your line, and call dispose on it. It leaves the Graphics object used by the rest of Swing alone. – NESPowerGlove Apr 21 '14 at 20:15
  • It's hard to tell from the given code. Maybe the `getHeight()` of the panel changed due to the added component and the surrounding layout manager? However, I'd **strongly** recommend you to use a JPanel for drawing and for drawing **only**. If you deliberately want to place other components on this `JPanel`, then you should try to provide an http://stackoverflow.com/help/mcve where it is easier to see what might be wrong. Also, "off topic" but for the long term: Consider what you will do when you want to plot a function in the range `x=0.1` to `x=0.2`. You'll have to generalize this.... – Marco13 Apr 21 '14 at 20:18
  • @Marco13, The other components are not within the JPanel, it is just for the graph, the probelm is, when they are added to the parent JPanel Component, it affects the size of the graphics JPanel and the transform doesn't work correctly. – Sting Apr 21 '14 at 20:39
  • @NESPowerGlove, yes the paintComponent calls super.paintComponent(g). – Sting Apr 21 '14 at 20:40
  • Components "outside" the panel should not affect the drawing "in" the panel. To rephrase the question from NESPowerGlove: How exactly are you calling this `doDraw` method? (You should call it ONLY from your `paintComponent` method. Particularly, you should not have a call to `someComponent.getGraphics` in your code...) – Marco13 Apr 21 '14 at 20:41
  • doDraw(g) is only called in paintComponent(g). When I change the transform as NESPowerGlove suggests it works correctly. Thank you. – Sting Apr 21 '14 at 20:45

1 Answers1

1

You might try the approach outlined here. Override paintComponent() to obtain a graphics context relative to the enclosing panel, rather than the enclosing frame.

To center the origin at the left edge, use

g2d.translate(0, h / 2);

To get upright, cartesian coordinates, use

g2d.scale(1, -1);
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045