You are on page 1of 5

import import import import import import

java.io.*; java.util.*; java.awt.*; java.awt.geom.*; java.awt.event.*; java.text.DecimalFormat;

//=============================================clasa puncte===================== =============================== class punct { float x,y; punct (){ //constructor nul this.x=0; this.y=0; } punct(float x, float y) { //constructor this.x = x; this.y = y; } static punct multsc(float z, punct q) { //"supraincarcare" inmultire pun ct cu scalar float a = z*q.x; float b = z*q.y; return new punct(a,b); } static punct add(punct z, punct q) { //"supraincarcare" adunare a doua p uncte float a = z.x + q.x; float b = z.y + q.y; return new punct(a,b); } } //=============================================clasa casteljau(contine si fereas tra)==================================================== class casteljau extends Frame implements ActionListener, KeyListener{ punct[][] c; //vectorul contine schema de casteljau punct[] curba; //contine punctele ce definesc curba int n, punctecitite, zoom = 100; //n - numarul de puncte de control Button ok,interpol,poli,ok2,next; Label z,t; TextField zt,tt,ntf,xtf,ytf; //ntf-numarul de puncte de control, xtf-x t ext field, y text field... TextArea ta; Boolean interpolari=true,poligon=true; //variabile pentru casteljau() { //**********************creaza fereastra si adauga controalele super("Curbe Bezier"); Panel p1 = new Panel(); p1.setBackground(Color.gray); add(p1, BorderLayout.SOUTH); z = new Label("Zoom:"); z.setForeground(Color.white); p1.add(z);

zt = new TextField("100",3); p1.add(zt); zt.addKeyListener(this); t = new Label("Raport:"); t.setForeground(Color.white); p1.add(t); tt = new TextField("0.5",3); p1.add(tt); tt.addKeyListener(this); ok = new Button("Refresh"); p1.add(ok); poli = new Button("Arata/Ascunde Poligon"); p1.add(poli); interpol = new Button("Arata/Ascunde Constructie"); p1.add(interpol); ok.addActionListener(this); poli.addActionListener(this); interpol.addActionListener(this); Panel p2 = new Panel(); p2.setBackground(Color.gray); add(p2, BorderLayout.NORTH); Label l1 = new Label("Numarul de puncte de control:"); l1.setForeground(Color.white); p2.add(l1); ntf = new TextField("",3); p2.add(ntf); ok2 = new Button("Start"); p2.add(ok2); ok2.addActionListener(this); Label blank = new Label("\t\t"); p2.add(blank); Label l2 = new Label("x="); l2.setForeground(Color.white); p2.add(l2); xtf = new TextField("",3); xtf.setEditable(false); p2.add(xtf); Label l3 = new Label("y="); l3.setForeground(Color.white); p2.add(l3); ytf = new TextField("",3); ytf.setEditable(false); p2.add(ytf); next = new Button("Next point"); p2.add(next); Panel p3 = new Panel(); p3.setBackground(Color.gray); add(p3, BorderLayout.EAST); ta = new TextArea("",40,20,TextArea.SCROLLBARS_BOTH); ta.setEditable(false); p3.add(ta, BorderLayout.CENTER); Scanner sc = new Scanner(System.in); addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); }

public void actionPerformed(ActionEvent e){ if (e.getActionCommand().equals("Refresh")){ this.zoom = Integer.parseInt(this.zt.getText()); float t = Float.parseFloat(this.tt.getText()); this.algCasteljau(t); this.repaint(); } if (e.getActionCommand().equals("Arata/Ascunde Constructie")){ this.interpolari = !this.interpolari; this.repaint(); } if (e.getActionCommand().equals("Arata/Ascunde Poligon")){ this.poligon = !this.poligon; this.repaint(); } if (e.getActionCommand().equals("Start")){ this.ok2.removeActionListener(this); this.n = Integer.parseInt(this.ntf.getText()); //***************************creeaza vectorul conform sc hemei de casteljau c = new punct[n][]; int q = 0; for (int i=n; i>0; i--){c[q] = new punct[i]; q++;} this.ntf.setEditable(false); this.xtf.setEditable(true); this.ytf.setEditable(true); this.next.addActionListener(this); } if (e.getActionCommand().equals("Next point")){ if (this.punctecitite<this.n) { int i = this.n - this.punctecitite; float a = Float.parseFloat(this.xtf.getText()); float b = Float.parseFloat(this.ytf.getText()); this.c[0][i] = new punct(a,b); this.ta.append("("+this.c[0][i].x+";"+this.c[0][ i].y+")\n"); } this.punctecitite++; //this.calcul(); } } public void keyTyped(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyPressed(KeyEvent e){ int key=e.getKeyCode(); if (key==KeyEvent.VK_ENTER){ this.zoom = Integer.parseInt(this.zt.getText()); float t = Float.parseFloat(this.tt.getText()); this.algCasteljau(t); this.repaint(); } } //=============================================rescrie metoda paint pentru desen area curbei==================================================== public void paint (Graphics g) {

DecimalFormat df = new DecimalFormat("#.#");//pentru afisarea pu nctelor cu precizia dorita Graphics2D g2D; g2D = (Graphics2D) g; //folosesc graphics2d pentru a putea trage liniile cu parametrii float for (int i=0; i<this.curba.length-1; i++) { //uneste punctele pe ntru a desena curba bezier Line2D linie = new Line2D.Float(this.curba[i].x*this.zoo m,this.curba[i].y*this.zoom,this.curba[i+1].x*this.zoom,this.curba[i+1].y*this.z oom); g2D.draw (linie); } if (this.poligon==true) for (int i=0; i<this.n-1; i++) { //deseneaza poligonul d e control g2D.setPaint(Color.blue); Line2D linie2 = new Line2D.Float(this.c[0][i].x* this.zoom,this.c[0][i].y*this.zoom,this.c[0][i+1].x*this.zoom,this.c[0][i+1].y*t his.zoom); g2D.draw(linie2); } if (this.poligon==true) for (int i=0; i<this.n; i++) { //scrie coordonatele punc telor de control g2D.setPaint(Color.blue); g2D.drawString("("+df.format(this.c[0][i].x)+";" +df.format(this.c[0][i].y)+")",this.c[0][i].x*this.zoom,this.c[0][i].y*this.zoom ); } if (this.interpolari==true) for (int i=1; i<this.n; i++) //deseneaza interpolarile d in algoritmul de casteljau for (int j=0; j<this.c[i].length-1; j++) { g2D.setPaint(Color.green); Line2D linie3 = new Line2D.Float (this.c[i][j].x*this.zoom,this.c[i][j].y*this.zoom,this.c[i][j+1].x*this.zoom,th is.c[i][j+1].y*this.zoom); g2D.draw(linie3); } if (this.interpolari==true) for (int i=1; i<this.n; i++) //scrie coordonatelor punct elor de casteljau intermediare for (int j=0; j<this.c[i].length; j++) { g2D.setColor(Color.green); g2D.drawString("("+df.format(thi s.c[i][j].x)+";"+df.format(this.c[i][j].y)+")",this.c[i][j].x*this.zoom,this.c[i ][j].y*this.zoom); } } //=============================================metoda de calcul a punctelor curb ei bezier==================================================== void calcul() { int i,j = 0,curent = 0; this.curba = new punct[1002]; //vectorul va avea 1002 de puncte (primul si ultimul punct = punctele de control) for (float r=0; r<=1; r+=0.001) { //calculeaza punctele curbei this.algCasteljau(r); //aplica algoritmul de casteljau p entru fiecare raport...

this.curba[curent] = this.c[n-1][0]; curent++; } this.curba[this.curba.length-1] = this.c[0][0]; //ultimul punct = punctul de control try { //scrie in fisierul txt continutul vectorului curba (punct ele de pe curba) BufferedWriter writer = new BufferedWriter(new FileWrite r("puncte.txt")); writer.write("x\ty\n"); for (i=0; i<this.curba.length; i++) writer.write(this.cu rba[i].x+"\t"+this.curba[i].y+"\n"); writer.close(); } catch (IOException e) { System.out.println(e); } this.algCasteljau(0.5f); //recalculeaza algoritmul pentru raport ul 0,5 pentru a pastra valorile in vectorul c -> pentru desenul initial } //=============================================algoritmul de casteljau========== ========================================== void algCasteljau(float r) { for (int i=1; i<this.n; i++) for (int j=0; j<this.c[i].length; j++) { punct a = punct.multsc((1-r),this.c[i-1] [j+1]); punct b = punct.multsc((r),this.c[i-1][j ]); this.c[i][j] = punct.add(a,b); } } //=============================================functia main===================== =============================== public static void main(String args[]){ Scanner sc = new Scanner(System.in); //System.out.print("Numarul de puncte de control: "); //int pct = sc.nextInt(); casteljau q = new casteljau(); //creeaza fereastra, poligonul de control si calculeaza curba q.setSize(800,800); q.setExtendedState ( java.awt.Frame.MAXIMIZED_BOTH ) ; q.setVisible(true); } }

You might also like