Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将圆放在直线的中心?_Java_Draw - Fatal编程技术网

Java 如何将圆放在直线的中心?

Java 如何将圆放在直线的中心?,java,draw,Java,Draw,我有一条对角线,还有一个距离为100米的圆。问题是这些圆实际上并不在直线的中心。我知道这很简单,但我只是不知道怎么做。。有人能帮我把圆放在直线的中心吗 以下是我迄今为止所尝试的: public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setBackground(Color.white); int x0_pixel = 0; int y0_

我有一条对角线,还有一个距离为100米的圆。问题是这些圆实际上并不在直线的中心。我知道这很简单,但我只是不知道怎么做。。有人能帮我把圆放在直线的中心吗

以下是我迄今为止所尝试的:

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

    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.white);

    int x0_pixel = 0;
    int y0_pixel = 0;

    int x1_pixel = getWidth();
    int y1_pixel = getHeight();

    int x0_world = 0;
    int y0_world = 0;
    double x1_world = 2000; // meters
    double y1_world = 1125; // meters

    double x_ratio = (double) x1_pixel / x1_world;
    double y_ratio = (double) y1_pixel / y1_world;

    int xFrom = 0;
    int yFrom = 0;

    double xTo = x1_world;
    double yTo = y1_world;

    int FromX_pixel = convertToPixelX(xFrom, x_ratio);
    int FromY_pixel = convertToPixelY(y1_pixel, yFrom, y_ratio);

    int ToX_pixel = convertToPixelX((int) xTo, x_ratio);
    int ToY_pixel = convertToPixelY(y1_pixel, (int) yTo, y_ratio);

    g2d.setColor(Color.RED);
    g2d.drawLine(FromX_pixel, FromY_pixel, ToX_pixel, ToY_pixel);

    double theta = Math.atan(yTo / xTo);

    int len = (int) Math.sqrt(xTo * xTo + yTo * yTo);

    int interval = 100;

    final double cosTheta = Math.cos(theta);
    final double sinTheta = Math.sin(theta);

    for (int distance = xFrom; distance <= len; distance += interval)
    {

        double distance_x = distance * cosTheta;
        double distance_y = distance * sinTheta;

        int x_circle_pixel = convertToPixelX(distance_x, x_ratio);
        int y_circle_pixel = convertToPixelY(y1_pixel, distance_y, y_ratio);

        g2d.drawOval(x_circle_pixel, y_circle_pixel, 50, 50);

        g2d.setColor(Color.BLUE);

    }

    Toolkit.getDefaultToolkit().

            sync();

    g2d.dispose();

}

private static int convertToPixelY(int y_offset, double y_world, double y_ratio)
{
    return (int) (y_offset - (y_world * y_ratio));
}

private static int convertToPixelX(double x_world, double x_ratio)
{
    return (int) (x_world * x_ratio);
}
public void绘制(图形g)
{
超级油漆(g);
Graphics2D g2d=(Graphics2D)g;
g2d.立根背景(颜色:白色);
int x0_像素=0;
int y0_像素=0;
int x1_pixel=getWidth();
int y1_pixel=getHeight();
int x0_world=0;
int y0_world=0;
双x1_world=2000;//米
双y1_世界=1125;//米
双x_比率=(双)x1_像素/x1_世界;
双y_比率=(双)y1_像素/y1_世界;
int xFrom=0;
int yFrom=0;
双xTo=x1_世界;
双yTo=y1_世界;
int FromX_pixel=convertToPixelX(xFrom,x_比率);
int-FromY_像素=转换像素(y1_像素,yFrom,y_比率);
int-ToX_像素=convertToPixelX((int)xTo,x_比率);
int ToY_pixel=转换像素(y1_pixel,(int)yTo,y_比率);
g2d.setColor(Color.RED);
g2d.绘制线(从X_像素、从Y_像素、从ToX_像素、玩具_像素);
双θ=Math.atan(yTo/xTo);
int len=(int)Math.sqrt(xTo*xTo+yTo*yTo);
整数区间=100;
最终双cosTheta=数学cos(θ);
最后一个双sinTheta=Math.sin(θ);

对于(int distance=xFrom;distance绘制椭圆时,前两个参数是容纳椭圆的矩形的左上角。接下来的两个参数是同一个边界矩形的宽度和高度。当前代码将左上角放在直线本身上,但实际需要的是直线的中心边界矩形必须放置在直线上。解决问题的方法是将左上角移动直径的1/2。您的代码应该如下所示:

public class GraphicsFoo extends JPanel {
   // avoid using magic numbers:
   private static final int CIRCLE_DIAMETER = 50; 

   //....

   // override a JComponent's paintComponent method, not its paint method
   @Override
   protected void paintComponent(Graphics g) {
       super.paintComponent(g);

       Graphics2D g2d = (Graphics2D) g;
       g2d.setBackground(Color.white);

       // make your graphics smooth
       g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
             RenderingHints.VALUE_ANTIALIAS_ON);

       // ...

       final double cosTheta = Math.cos(theta);
       final double sinTheta = Math.sin(theta);

       for (int distance = xFrom; distance <= len; distance += interval)
       {

           //....

           // *** here's the key: ***    
           g2d.drawOval(
              x_circle_pixel - CIRCLE_DIAMETER / 2, 
              y_circle_pixel - CIRCLE_DIAMETER / 2, 
              CIRCLE_DIAMETER, CIRCLE_DIAMETER);

           g2d.setColor(Color.BLUE);

       }
公共类Graphicsfo扩展了JPanel{
//避免使用幻数:
专用静态最终整数圆直径=50;
//....
//重写JComponent的paintComponent方法,而不是其paint方法
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g;
g2d.立根背景(颜色:白色);
//使您的图形平滑
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
// ...
最终双cosTheta=数学cos(θ);
最后一个双sinTheta=Math.sin(θ);

对于(int distance=xFrom;distance关于代码所做的事情的一些注释和描述将非常有用。同时,an将为您提供最有希望的快速有用的答案。很抱歉反应太晚,实际上我的问题是绘制椭圆形的唯一部分,您给了我确切的代码..tnx..有效..我接受您的答案..-)