tags:

views:

45

answers:

2
+1  Q: 

java 2D drawing

hi , im trying to make a swing app that draws a function's graph(simple for now ex. x+2) but i'm having problems to make mathematical coordinates of my points depending on screen coordinates . I want it to simply draw a line that goes from P1(0,1) to P2(1,2) inside my graph.

here is my code :

import java.awt.*;
import javax.swing.*;  
public class Graph extends JPanel {

   protected void paintComponent(Graphics g) {
     int YP1,YP2;
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

      int h = getHeight();
         int w = getWidth();
         // Draw axeX.
         g2.draw(new Line2D.Double(0, h/2, w, h/2)); //to make axisX in the middle
         // Draw axeY.
         g2.draw(new Line2D.Double(w/2,h,w/2,0));//to make axisY in the middle of the panel

                  //line between P1(0,1) and P2(1,2) to draw function x+1
    Point2D P1 = new Point2D.Double(w/2,(h/2)+1);
    Point2D P2 = new Point2D.Double((w/2)+1,(h/2)+2);
     g2.draw(new Line2D.Double(P1,P2));
}
public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(new Graphe());
      f.setSize(400,400);
      f.setLocation(200,200);
      f.setVisible(true);
  }
}

thanks.

+1  A: 

The center of your coordinate system (0,0) is painted at (w/2, h/2). The missing part is the scale, in other words: how many pixels make one unit on the x axis and y axis.

So usually you multiply your unit value by the scaling factor (like 10, if you want 10 pixel per unit) and add the offset of the axis from the left or lower boundary. The annoying part is the (0,0) on screen coordinates is the upper left corner and height counts from top to bottom (reversed y axis). That makes it a bit more complicated:

 xOnScreenPos =  (xUnit * xScale) + xScaleOffset;
 yOnScreenPos = -(yUnit * yScale) + yScaleOffset;
Andreas_D
+1  A: 
import java.awt.*;
import javax.swing.*;  
public class Graph extends JPanel {
   private static final int UNIT = 20;
   protected void paintComponent(Graphics g) {
     int YP1,YP2;
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;

      int h = getHeight();
         int w = getWidth();
         // Draw axeX.
         g2.draw(new Line2D.Double(0, h/2, w, h/2)); //to make axisX in the middle
         // Draw axeY.
         g2.draw(new Line2D.Double(w/2,h,w/2,0));//to make axisY in the middle of the panel

                  //line between P1(0,1) and P2(1,2) to draw function x+1
    Point2D P1 = new Point2D.Double(w/2,(h/2)+ UNIT);
    Point2D P2 = new Point2D.Double((w/2)+ UNIT,(h/2)+ 2*UNIT);  //considering 20 = 1 unit in your syste,
     g2.draw(new Line2D.Double(P1,P2));
}
public static void main(String[] args) {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(new Graphe());
      f.setSize(400,400);
      f.setLocation(200,200);
      f.setVisible(true);
  }
}   

Tryout this code read comments to get the solution

org.life.java