java - Drawing a Line - Maximum Point -


i drew line @ angle based on slider.

i trying make line's end y coordinate number (let's 300), if @ angle.

any ideas on how this? here work on line far:

double angle = intangle; angle = angle * math.pi / 180; double length = 300;  graphics.setcolor(color.red);  double startx = 300; double starty = 100; double endx = startx + length * math.cos(angle); double endy = starty + length * math.sin(angle); double end2x; double end2y;    double dblangle; double angle2; int intangle2; double start2x = endx; double start2y = endy; intangle2 = 180 - intangle; angle2 = intangle2; angle2 = (angle2 * math.pi / 180); end2x = (start2x - length * math.cos(angle2)); end2y = (start2y - length * math.sin(angle2));  int intendx = (int)endx; int intendy = (int)endy;  if(blnbutton == true){    graphics.draw(new line2d.double(startx, starty, endx, endy));        graphics.draw(new line2d.double(start2x, start2y, end2x, end2y));        

}

there's simpler way, basically, can calculate 2 points on circle based on angle , inverse of angle (angle - 360)

with circle radius of 150, give line of 300, example

example

the red line line center of circle point on circle represented given angel. blue inverse. each line 150 pixels line, meaning together, 300 pixels in length.

this examples draws separately, realistically, draw single line

import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.eventqueue; import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.ellipse2d; import java.awt.geom.line2d; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jslider; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.event.changeevent; import javax.swing.event.changelistener;  public class test {      public static void main(string[] args) {         new test();     }      public test() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          public testpane() {             setlayout(new borderlayout());              drawpane drawpane = new drawpane();             add(drawpane);              jslider slider = new jslider(0, 100);             add(slider, borderlayout.south);             slider.addchangelistener(new changelistener() {                 @override                 public void statechanged(changeevent e) {                     drawpane.setangleindegrees(360d * (slider.getvalue() / 100d));                 }             });             slider.setvalue(0);         }      }      public class drawpane extends jpanel {          private double angle;          @override         public dimension getpreferredsize() {             return new dimension(400, 400);         }          @override         protected void paintcomponent(graphics g) {             super.paintcomponent(g);             graphics2d g2d = (graphics2d) g.create();              // radius of circle             double r = 150;             // degrees radians...             double radians = math.toradians(angle);              // end point on circle...             int endx = (int) math.round(r * math.cos(radians));             int endy = (int) math.round(r * math.sin(radians));                          // start point on circle, 360 degress             // start angle             radians = math.toradians(angle - 360);             int startx = (int) math.round(r * math.cos(radians));             int starty = (int) math.round(r * math.sin(radians));              // offset ellipse (center of screen)             double x = (getwidth() / 2d) - r;             double y = (getwidth() / 2d) - r;              g2d.setcolor(color.light_gray);             g2d.draw(new ellipse2d.double(x, y, r * 2, r * 2));             // center of circle...             x = (getwidth() / 2d);             y = (getwidth() / 2d);              // 1 single line             //g2d.setcolor(color.black);             //g2d.draw(new line2d.double(x - startx, y - starty, x + endx, y + endy));              g2d.setcolor(color.red);             g2d.draw(new line2d.double(x, y, x - startx, y - starty));             g2d.setcolor(color.blue);             g2d.draw(new line2d.double(x, y, x + endx, y + endy));              g2d.dispose();         }          public void setangleindegrees(double value) {             if (angle != value) {                 angle = math.min(math.max(value, 0), 360);                 repaint();             }         }      }  } 

or along lines...


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -