0

I'am currently working on graphing program in Java/Swing. Currently I made JPanel with axes and markers on it.

My question is how can I merge loops for drawing negative with positive value markers?

Currently I made something like this:

 private void DrawAxisMarkers(Graphics g) {

    //X  MARKERS
    g.setColor(Color.red);
    for (int xI = 0; xI <= this.getWidth(); xI += MatherProperties.SCALE) {
        g.drawLine(xI * MatherProperties.SCALE+ origin.x, markerSize + origin.y, xI * MatherProperties.SCALE + origin.x, -markerSize + origin.y);
    }
    for (int xI = 0; xI <= this.getWidth(); xI += MatherProperties.SCALE) {
        g.drawLine(-xI * MatherProperties.SCALE + origin.x, markerSize + origin.y, -xI * MatherProperties.SCALE + origin.x, -markerSize + origin.y);
    }

    //Y MARKERS
    g.setColor(Color.blue);
    for (int yI = 0; yI <= this.getHeight(); yI += MatherProperties.SCALE) {
        g.drawLine(markerSize + origin.x, yI * MatherProperties.SCALE + origin.y, -markerSize + origin.x, yI * MatherProperties.SCALE + origin.y);
    }
    for (int yI = 0; yI <= this.getHeight(); yI += MatherProperties.SCALE) {
        g.drawLine(markerSize + origin.x, -yI * MatherProperties.SCALE + origin.y, -markerSize + origin.x, -yI * MatherProperties.SCALE + origin.y);
    }
}

I don't have any ideas how to merge them :\ Also the origin of these axes is in the middle but user will be able to change origin.

The graph:

5
  • Move g.drawLine(-xI * MatherProperties.SCALE + origin.x, markerSize + origin.y, -xI * MatherProperties.SCALE + origin.x, -markerSize + origin.y); up so it is within the first loop. Similar for the y-axis.
    – Andrew S
    Jan 7, 2019 at 21:29
  • @AndrewS I'm such an idiot ;-; Jan 7, 2019 at 21:46
  • Nah - that's what reviews are for!
    – Andrew S
    Jan 7, 2019 at 21:48
  • Remove the second loop and make the first loop go from -this.getWidth() to this.getWidth() - or turn the g.drawLine call into a method that takes xI as an argument so that you can call it with both -xI and xI Jan 7, 2019 at 21:57
  • Anyway, Thanks for help! Jan 7, 2019 at 21:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.