2

I made a JSlider with Java that changes the angle the line needs to be slanted at.

angle = new JSlider(SwingConstants.HORIZONTAL, 0, 180, 90);
angle.setSize(300, 50);
angle.setLocation(650, 60);
angle.setPaintTicks(true);
angle.setPaintTrack(true);
angle.setMinorTickSpacing(10);
angle.setMajorTickSpacing(30);
angle.setPaintLabels(true);
angle.addChangeListener(this);

thepanel.add(angle);

I want the code to draw that line that implements the angle from the JSlider.

Here is my code:

public void paintComponent(Graphics g){
    super.paintComponent(g);

    int angle = intAngle;
    Graphics2D graphics = (Graphics2D)g;

    int startX = getWidth()/2;
    int startY = getHeight()/2;
    int length = 200;


    int endX = startX + length * (int)Math.cos(Math.toRadians(angle));
    int endY = startY + length * (int)Math.sin(Math.toRadians(angle));

    graphics.drawLine(startX, startY, endX, endY);

}

What is the mathematics behind rotating a line given a value?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Eliscpe
  • 47
  • 1
  • 7

1 Answers1

4

Step 1: Extend JPanel and override paintComponent(). You've mentioned you already do this step, but more info is available here.

Step 2: Get the value of your JSlider into your paintComponent() method.

Step 3: Add a listener to the JSlider that tells your JPanel to repaint itself whenever the value changes.

Step 4: Use basic trigonometry to figure out the X and Y coordinates of the line to draw, then draw it. It might look something like this:

public void paintComponent(Graphics g){
   super.paintComponent(g);
   int angle = getSliderValue(); //you have to implement this function

   int startX = getWidth()/2;
   int startY = getHeight()/2;
   int length = 100;

   int endX = startX + (int)Math.cos(Math.toRadians(angle)) * length;
   int endY = startY + (int)Math.sin(Math.toRadians(angle)) * length;

  g.drawLine(startX, startY, endX, endY);
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • I tried it out, but only 0, 90, and 180 degrees draw out. Any angles in between that disappears. – Eliscpe May 11 '15 at 14:22
  • @Eliscpe Then I suggest you post your updated code in the form of an [MCVE](http://stackoverflow.com/help/mcve). – Kevin Workman May 11 '15 at 14:26
  • I think it's because of the convert to radians. I did a system.out.println and only 0, 1, and 2 radians display, since it's an integer. So is there any way to not use radians? Or do a g.drawLine with Double values instead of Integers. – Eliscpe May 11 '15 at 14:46
  • @Eliscpe I can't really help without an MCVE (edit your question instead of trying to post it as a comment), but the basics are to use doubles to store everything, then just cast to int when you need to actually draw the line. – Kevin Workman May 11 '15 at 14:52
  • But after you convert it back to int, the decimals are removed. – Eliscpe May 11 '15 at 14:55
  • @Eliscpe You don't need the decimal part when drawing, only when storing. Please provide an MCVE- note that this should be a whole program, not just a paintComponent() method. – Kevin Workman May 11 '15 at 14:57