Untitled
unknown
java
3 years ago
4.4 kB
1
Indexable
Never
Camelia Ciolpan package gui; importjava.awt.*; import java.awt.event. *; import java.io. *; import svm.SVM; public class Design extends Panel implernents MouseListener, MouseMotionListener{ SVM svm; Image im; Graphics img; int ww, hh; int Ox, Oy, ex, cy, ccx, ccy; boolean init= true; String coords = ""; public boolean show_ coords = false; public boolean show _line = false; public boolean calculates =false; public int xl, yl, x2, y2; public Design(SVM svm){ this. svm = svm; addMouseListener(this ); addMouseMotionListener(this ); } public voind initO(){ Ox = ww/2; Oy = hh/2; ex= O; cy = O; repaint(); } public void paint(Graphics g){update(g);} public void update(Graphics g){ if(init){ ww = size(). width; hh = size().height; im =createlmage(ww, hh); img = im.getGraphics(); initO(); init = false; } img.setColor(svm.settings.background _ color); img.fillRect(0,0,ww,hh); if(svm.ind.V != null && svm.ind.V[0].getDimension() == 2){ drawAxis(img); if( show _line){ img.setColor(svm.settings.line _ color); img.drawLine(Ox+xl,Oy-yl,Ox+x2,Oy-y2); } for(int i=0;i<svm.ind.V.length;i++){ Point p = new Point(Ox + (int)(svm.ind.V[i].X[0]+0.5), Oy - (int)(svm.ind.V[i].X[l]+0.5)); img. set Color( svm. ind. V[i] .c1.color); int r = svm.settings.point_radius; img.fill Oval(p .x-r ,p. y-r ,2 *r,2*r ); img. set Color(Color. black); img.draw0val(p.x-r,p.y-r,2*r,2*r); } if(show_coords){ img.setColor(svm.settings.string_ color); img.drawString(coords, ccx+ 15, ccy+30); } } if( calculates) img. drawImage( svm. calculates, ( ww-svm. calculates. get Width( this) )/2, (hhsvm. calculates. getHeight( this) )/2, this ); g.drawImage(im,0,0,this); } public void draw Axis(Graphics g){ // deseneaza gridul if(svm.settings.grid){ g.setColor(svm.settings.grid _ color); for(int i=Ox+svm.settings.axis _ min; i<=Ox+svm.settings.axis _ max; i+=svm.settings.grid _size) g.drawLine(i, Oy+svm.settings.axis _ min, i, Oy+svm.settings.axis _ max); for(int j=Oy+svm.settings.axis _ min; j<=Oy+svm.settings.axis _ max; j+=svm.settings.grid _size) g.drawLine(Ox+svm.settings.axis_min,j,Ox+svm.settings.axis_max,j); } // deseneaza axele if(svm.settings.axis ){ g.setColor(svm.settings.axis _ color); g.drawLine(Ox+svm.settings.axis _ min, Oy, Ox+svm.settings.axis _ max, Oy); g.drawLine(Ox, Oy+svm.settings.axis _ min, Ox, Oy+svm.settings.axis _ max); // deseneaza gradatiile if( svm.settings. gradations ){ for(int i=Ox; i<=Ox+svm.settings.axis _ max; i+=svm.settings.axis _gradations) g.drawLine(i, Oy-2, i, Oy+2); for(int i=Ox; i>=Ox+svm.settings.axis _ min; i-=svm.settings.axis _gradations) g.drawLine(i, Oy-2, i, Oy+ 2); for(int j=Oy; j<=Oy+svm.settings.axis _ max; j+=svm.settings.axis _gradations) g.drawLine(Ox-2, j, Ox+ 2, j); for( int j=Oy; j>=Oy+svm.settings. axis_ min; j-=svm. settings.axis _gradations) g.drawLine( Ox-2, j, Ox+ 2, j ); } } } public void setPointsOfLine(float[] w){ show_ line = true; if(Math.abs(w[0]) < Math.abs(w[1])){ xl = svm.settings.axis _ min; yl = (int)((-w[2]-w[0]*x1)/w[1]+0.5); x2 = svm.settings.axis _ max; y2 = (int)((-w[2]-w[0]*x2)/w[l ]+0.5); }else{ yl = svm.settings.axis _ min; x1 = (int)((-w[2]-w[ 1 ]*yl)/w[0]+0.5); y2 = svm.settings.axis _ max; x2 = (int)((-w[2]-w[ 1 ]*y2)/w[0]+0.5); } repaint(); } public void mouseClicked(MouseEvent me) { initO();} public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mouseMoved(MouseEvent me) { ccx = me.getX(); ccy = me.getY(); coords = "(" + (ccx-Ox) + "," + (Oy-ccy) + ")"; if(ccx<=2 || ccx >= ww-5 || ccy<=S || ccy>=hh-5) coords = ""; repaint(); } public void mousePressed(MouseEvent me){ cx= me.getX(); cy = me.getY(); coords = ""; } public void mouseDragged(MouseEvent me) { int x = me.getX(), y = me.getY(); if(svm.ind.V != null && svm.ind.V[0].getDimension() == 2){ cx = x - ex; cy = y - cy; Ox += cx; Oy += cy; cx = x; cy = y; coords=""; repaint(); }} public void mouseReleased(MouseEvent me) { int x = me.getX(), y = me.getY(); } }