我需要一点关于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 函数,但我无法让它在两点之间工作。有谁能够帮我 。
提前致谢。