Bem, criei esse programa da curva de Bezier em java, porém quando eu mudo os retângulos de lugar, a tela fica piscando, o que é bem chato, alguém poderia me ajudar a melhorar isso, ou qualquer outra coisa do código.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class Bezier {
public static void main(String[] args) {
Janela j = new Janela();
j.setBackground(Color.black);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j.setSize(750,750);
j.setVisible(true);
j.addMouseListener(j);
j.addMouseMotionListener(j);
}
}
class Janela extends JFrame implements MouseListener,MouseMotionListener{
int a0=50,a1=400,b0=250,b1=200,c0=450,c1=200,d0=650,d1=400;
boolean cb,cc,ft=true,d=true;
double x,y;
Rectangle r1 = new Rectangle(0,0,8,8);
Rectangle r2 = new Rectangle(0,0,8,8);
Rectangle r3 = new Rectangle(0,0,8,8);
Rectangle r4 = new Rectangle(0,0,8,8);
public void paint(Graphics g2){
Graphics2D g = (Graphics2D)g2;
g.setBackground(Color.black);
g.setColor(Color.white);
if(ft==true){
r1.setLocation(a0,a1);
r2.setLocation(b0,b1);
r3.setLocation(c0,c1);
r4.setLocation(d0,d1);
ft=false;
}
if(d==true){
g.clearRect(1,1,1000,1000);
}
g.fill(r1);
g.fill(r2);
g.fill(r3);
g.fill(r4);
Bezier(g2);
}
public void Bezier(Graphics g2){
Graphics2D g = (Graphics2D) g2;
for(double w = 0;w<=1;w=w+0.01){
double z = 1-w;
x =(w*w*w)*a0+3*(w*w)*z*b0+3*w*(z*z)*c0+(z*z*z)*d0;
y =(w*w*w)*a1+3*(w*w)*z*b1+3*w*(z*z)*c1+(z*z*z)*d1;
Line2D l = new Line2D.Double(x,y,x,y);
g.draw(l);
}
}
public void updateval(MouseEvent e){
if(cb==true){
b0=+e.getX();b1=+e.getY();
}
if(cc==true){
c0=+e.getX();c1=+e.getY();
}
r2.setLocation(b0, b1);
r3.setLocation(c0, c1);
repaint();
}
public void mousePressed(MouseEvent e){
d=true;
if(r2.contains(e.getX(),e.getY())){
cb=true;
updateval(e);
}
if(r3.contains(e.getX(),e.getY())){
cc=true;
updateval(e);
}
}
public void mouseDragged(MouseEvent e){
if(cb==true){
updateval(e);
}
if(cc==true){
updateval(e);
}
}
public void mouseReleased(MouseEvent e){
d=false;
if(r2.contains(e.getX(),e.getY())){
if(cb=false){
updateval(e);
}
}
if(r3.contains(e.getX(),e.getY())){
if(cc=false){
updateval(e);
}
}
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
}