从一点到点绘制随机线(也随机生成)



我需要一点Java方面的帮助。我需要在两点之间创建一些曲线。应用程序需要创建随机数量的曲线,这些曲线将从随机生成的起点连接到随机生成的终点。

这是我到目前为止的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tryproject;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import javax.swing.JPanel;
/**
 *
 * @author Q
 */
public class GUI extends JPanel{
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        ArrayList<Point> jaa = new ArrayList<Point>();
 Random ja = new Random();
Random p = new Random();
int x = ja.nextInt(50) +50;
int y = ja.nextInt(150) + 150;
Point pocetnaTacka = new Point(Math.abs(ja.nextInt(150)),Math.abs(ja.nextInt(150)));
Point zavrsmaTacka = new Point(Math.abs(ja.nextInt(300) ) ,Math.abs(ja.nextInt(300)));
int lx = pocetnaTacka.x;
int ly = pocetnaTacka.y; 
jaa.add(pocetnaTacka);
int rastojanje = (pocetnaTacka.x - zavrsmaTacka.x)/10;
for (int i = 1;i< Math.abs(ja.nextInt(8));i++){
        x =  lx + Math.abs(ja.nextInt(50));
        y =  ly + Math.abs(ja.nextInt(50));
         jaa.add(new Point(x,y));
        lx = x;
        ly = y;
     }
Graphics2D g2 = (Graphics2D) g;
    RenderingHints rh = new RenderingHints(
             RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHints(rh);
jaa.add(zavrsmaTacka);
Point lastpp = (Point)pocetnaTacka.clone();
for(Point pp: jaa)
{
    QuadCurve2D q = new QuadCurve2D.Float();
// draw QuadCurve2D.Float with set coordinates
q.setCurve(lastpp.x, lastpp.y, pp.x/3, pp.y/3, pp.x, pp.y);
  g2.draw(q);
    lastpp = (Point) pp.clone();
}
for(Point aa: jaa)
{
    System.out.println(aa.x + " , "+aa.y);
}
    } 

}

这就是它应该的样子,但我不能这样理解:http://www.dodaj.rs/f/2U/qT/4LubxcQN/everytime.png并且每次应用程序运行时都会生成两个新点并曲线到..我尝试过 sin ,cos 函数,但我无法让它在两点之间工作。谁能帮我.

提前谢谢。

我解决了这个问题。

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tryproject2;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.QuadCurve2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.JPanel;
/**
 *
 * @author Q
 */
public class GUI extends JPanel {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        ArrayList<Point> jaa = new ArrayList<Point>();
        Random pocetnaikrajnja = new Random();
              RenderingHints rh = new RenderingHints(
                      RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHints(rh);
        g2.setColor(Color.BLUE);
        Point pocetnaTacka = new Point(Math.abs(pocetnaikrajnja.nextInt(150)), Math.abs(pocetnaikrajnja.nextInt(200) + 100));
        g2.fillOval(pocetnaTacka.x,pocetnaTacka.y, 5, 5);

        jaa.add(pocetnaTacka);
        Point poslednjatacka = (Point) pocetnaTacka.clone();
        for (int i = 1;i< 6;i++){
            Point p = new Point(poslednjatacka.x + Math.abs(pocetnaikrajnja.nextInt(30) + 30),pocetnaTacka.y - Math.abs(pocetnaikrajnja.nextInt(30) + 30));
            poslednjatacka = p;
            jaa.add(p);
        }
         Point zavrsnaTacka  = null;
       zavrsnaTacka = new Point(jaa.get(jaa.size()-1).x+Math.abs(pocetnaikrajnja.nextInt(15) + 15),jaa.get(jaa.size()-1).y-70);

        jaa.add(zavrsnaTacka);
        g2.setColor(Color.RED);
        g2.fillOval(zavrsnaTacka.x,zavrsnaTacka.y, 5, 5);
        for(int i=0;i<jaa.size()-1;i++)
        {
               QuadCurve2D q = new QuadCurve2D.Float();
//if(i%2==0){
q.setCurve(jaa.get(i).x, jaa.get(i).y,jaa.get(i).x/1.2,jaa.get(i).y-Math.abs(pocetnaikrajnja.nextInt(35) + 35), jaa.get(i+1).x, jaa.get(i+1).y);
//}
//else
//{
  //  q.setCurve(jaa.get(i).x, jaa.get(i).y,jaa.get(i).x/1.2,jaa.get(i).y+Math.abs(pocetnaikrajnja.nextInt(35) + 35), jaa.get(i+1).x, jaa.get(i+1).y);
//}
  g2.draw(q);
}
        /**
         * Sakriven kod za grane.
        for(int i=1;i<jaa.size()-Math.abs(pocetnaikrajnja.nextInt(2)+1);i= i + Math.abs(pocetnaikrajnja.nextInt(5)))
        {
              QuadCurve2D q = new QuadCurve2D.Float();
              if(i%2==0)
                q.setCurve(jaa.get(i).x, jaa.get(i).y,jaa.get(i).x/1.2,jaa.get(i).y/1.2, jaa.get(i).x + Math.abs(pocetnaikrajnja.nextInt(5) + 7), jaa.get(i).y - Math.abs(pocetnaikrajnja.nextInt(10) + 60));
              else
                    q.setCurve(jaa.get(i).x, jaa.get(i).y,jaa.get(i).x/1.2,jaa.get(i).y/1.2, jaa.get(i).x + Math.abs(pocetnaikrajnja.nextInt(5) + 7), jaa.get(i).y + Math.abs(pocetnaikrajnja.nextInt(10) + 60));

              g2.draw(q);
        }
        */

    }
}

相关内容

  • 没有找到相关文章

最新更新