使用计时器和贝塞尔曲线路径移动对象

时间:2019-12-04 19:59:54

标签: netbeans

所以我必须将防雷对象从点1移动到点3。您实际上可以在图片上看到它。当用户单击屏幕时,也应执行该操作。我尝试使用,但结果却很奇怪。我了解互联网上有数十种信息,但我仍然做不到。谢谢!

    public static int x;
    public static int y;
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       Random c=new Random();
       x=c.nextInt(jPanel1.getWidth());
       y=c.nextInt(jPanel1.getHeight());
       zimet();
    }  
 public void zimet(){
    String obj =(String) jComboBox1.getSelectedItem();     
    if (obj=="Trissturis"){
         Graphics g=jPanel1.getGraphics();  
         g.setColor(Color.red);
         int[]tx=new int[3];
         int[]ty=new int[3];
         tx[0]=x; tx[1]=x+32; tx[2]=x-32;
         ty[0]=y-32; ty[1]=y+16; ty[2]=y+16;
         int n=3;
         Polygon p= new Polygon(tx, ty, n);
         g.fillPolygon(p);
       }else if(obj=="Kvadrats"){
         Graphics g=jPanel1.getGraphics();  
         g.setColor(Color.red);  
         g.fillRect(x-32, y-32, 64, 64);
       }else{
         Graphics g=jPanel1.getGraphics();  
         g.setColor(Color.red); 
         g.fillArc(x-32, y-32, 64, 64, 0, 360);
       }
    }

    public static int[] pX = new int[3];
    public static int[] pY =new int[3];
    private Timer timer = null;
    private void jPanel1MousePressed(java.awt.event.MouseEvent evt) {                                     
        pX[0] = x;
        pY[0] = y;
       Random val=new Random();
        pX[1] = val.nextInt(jPanel1.getWidth());
        pY[1] = val.nextInt(jPanel1.getHeight());
        pX[2] = evt.getX();   //where will be final point of object
        pY[2] = evt.getY();  
        bezje();    
//somwhere here i think the code will be 
    }                  
 public static double t;
    public static double dt;
    public static double[] B =new double[3];
    public static int xn, yn;
    public void bezje(){
     Graphics g=jPanel1.getGraphics(); 
      g.setColor(Color.blue);
        for (int i=0; i<3; i++){
            g.drawOval(pX[i]-2, pY[i]-2, 4, 4);
            g.drawString(String.valueOf(i+1),pX[i]-3,pY[i]-6);
        }
        t = 0;
        dt=0.001;
        g.setColor(Color.black);
        while (t<=1 ){
            B[0] = Math.pow(1-t, 2);
            B[1] = 2*t*(1-t);
            B[2] = t*t;
        xn =(int)Math.round(B[0]*pX[0]+B[1]*pX[1]+B[2]*pX[2]);   
        yn =(int)Math.round(B[0]*pY[0]+B[1]*pY[1]+B[2]*pY[2]); 
            g.drawRect(xn, yn, 1, 1);
            t = t + dt;
    }}


How looks my project

0 个答案:

没有答案