javaswinggraphics2dlineto

GeneralPath won't draw using calculated coordinate


I'm stumped. I'm trying to draw a line in a Graphics 2D environment based upon a calculated value, and GeneralPath won't do it. Basically, I want to draw a straight line based on the formula y=sx+b where x is a provided value, s is the tangent of an angle in degrees, and b is the y-intercept. solve for y and draw to {x, y}. During testing, I added some dummy points to make it draw at all, which it does, but when I add my formula, lineTo ignores that coordinate pair.

Here's my code for the problem:

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;

public class Sketch extends JPanel{
    
    public Sketch(String num) {
        super();
    }

    public void paintComponent(Graphics comp){
        super.paintComponent(comp);
        Graphics2D comp2D = (Graphics2D) comp;
        comp2D.setColor(Color.white);//set background color
        Rectangle2D.Double background = new Rectangle2D.Double(0F, 0F, 260F, 220F);//create background
        comp2D.fill(background);
        comp2D.setColor(Color.black);

        Double minX = 74.0;
        Double minY = 62.0;
        Double maxX = 146.0;
        Double maxY = 158.0;
        Double r = 157.5;
        double offsetDir = r + 90;
        double[] offsetLT = new double[2];

        //where X is 0 and Y is 1:
        double offsetX = 15 * Math.cos(Math.toRadians(offsetDir));
        double offsetY = 15 * Math.sin(Math.toRadians(offsetDir));
        offsetLT[0] = 110+offsetX;
        offsetLT[1] = 110+offsetY;
        Double cLT = offsetLT[1] - (Math.tan(Math.toRadians(r))*offsetLT[0]);

        GeneralPath trough = new GeneralPath();
        Double currentY;
        currentY = Math.tan(Math.toRadians(r))*minX+cLT;
        trough.moveTo(minX, minY);
        trough.lineTo(110D, currentY);
        trough.lineTo(maxX-15, maxY);
        comp2D.draw(trough);
    }
}

This is a class within a bigger package, so there's no main here on purpose. I've also chopped this down from a much larger code block to try to only show what's relevant.

*Edit: Answering camickr's response, I have a GUI with a CardLayout. The sketch is called as a new sketch and added to that card. The code for that reads:

Sketch sku = new Sketch(currentStructure);
sketchCard.add(sku, currentStructure);

Said card sits in a JFrame with a JPanel holding other cards including sketchCard. As for the question about size, I've added the code I used to create a background. This appears to control the size.

I've tried changing from GeneralPath to Path2D.Double thinking that the problem was mixing Float and Double, but no change. Can anybody explain why it won't draw to {110D, currentY}?

Thanks


Solution

  • Your code works for me. Below is your code with some additions, namely a main method that creates a JFrame and adds an instance of your Sketch class to it.

    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Sketch extends JPanel{
        
        public Sketch(String num) {
            super();
        }
    
        public void paintComponent(Graphics comp){
            super.paintComponent(comp);
            Graphics2D comp2D = (Graphics2D) comp;
            comp2D.setColor(Color.white);//set background color
            Rectangle2D.Double background = new Rectangle2D.Double(0F, 0F, 260F, 220F);//create background
            comp2D.fill(background);
            comp2D.setColor(Color.black);
    
            Double minX = 74.0;
            Double minY = 62.0;
            Double maxX = 146.0;
            Double maxY = 158.0;
            Double r = 157.5;
            double offsetDir = r + 90;
            double[] offsetLT = new double[2];
    
            //where X is 0 and Y is 1:
            double offsetX = 15 * Math.cos(Math.toRadians(offsetDir));
            double offsetY = 15 * Math.sin(Math.toRadians(offsetDir));
            offsetLT[0] = 110+offsetX;
            offsetLT[1] = 110+offsetY;
            Double cLT = offsetLT[1] - (Math.tan(Math.toRadians(r))*offsetLT[0]);
    
            GeneralPath trough = new GeneralPath();
            Double currentY;
            currentY = Math.tan(Math.toRadians(r))*minX+cLT;
            trough.moveTo(minX, minY);
            trough.lineTo(110D, currentY);
            trough.lineTo(maxX-15, maxY);
            comp2D.draw(trough);
        }
    
        public static void main(String[] args) {
            EventQueue.invokeLater(() -> {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                Sketch sketch = new Sketch("0");
                frame.add(sketch);
                frame.setSize(450, 300);
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            });
        }
    }
    

    Here is the window I get when I run the above code.

    screen capture of running code

    Note that I use Eclipse 2023-12 on Windows 11 so the above code was compiled with the Eclipse IDE builder and run using the Eclipse JRE which is Java 17.