Advertisement
maha_kaal

Disegna Linea

Oct 12th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. //Applet
  2. package disegnaLinea;
  3.  
  4. import java.applet.Applet;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.MouseMotionListener;
  8. import java.awt.*;
  9. import java.util.*;
  10. public class Disegna extends Applet implements MouseMotionListener, MouseListener {
  11.  
  12.     /**
  13.      *
  14.      */
  15.     private static final long serialVersionUID = 4382358404236741121L;
  16.    
  17.    
  18.     public void init(){
  19.         addMouseMotionListener(this);
  20.         addMouseListener(this);
  21.     }
  22.    
  23.     //private int x1, y1, x2, y2;
  24.    
  25.     private Vector <Linea> coordinate = new Vector <Linea>();
  26.     /*private Image OffScreenImage;
  27.     public void start(){
  28.         OffScreenImage = createImage(getSize().width, getSize().height);
  29.     }*/
  30.    
  31.     public void paint(Graphics g){
  32.  
  33.         for(int i = 1; i < coordinate.size()-1; i++){
  34.             if (coordinate.get(i).getConnected())
  35.             {
  36.                 g.drawLine(coordinate.get(i-1).getX(), coordinate.get(i-1).getY(), coordinate.get(i).getX(), coordinate.get(i).getY());
  37.             }
  38.         }
  39.        
  40.     }
  41.    
  42.     /*public void update(Graphics g){
  43.         if(OffScreenImage != null){
  44.             Graphics offScreenGraphics = OffScreenImage.getGraphics();
  45.             offScreenGraphics.setColor(getBackground());
  46.             //offScreenGraphics.fillRect(0, 0, getSize().width, getSize().height);
  47.             offScreenGraphics.setColor(g.getColor());
  48.             paint(offScreenGraphics);
  49.             g.drawImage(OffScreenImage, 0, 0, this);
  50.         }
  51.     }*/
  52.  
  53.    
  54.     @Override
  55.     public void mouseDragged(MouseEvent e) {
  56.         /*if(x1 == 0 && y1 == 0){
  57.             x1 = e.getX();
  58.             y1 = e.getY();
  59.             x2 = e.getX();
  60.             y2 = e.getY();
  61.            
  62.         }else{
  63.             x1 = x2;
  64.             y1 = y2;
  65.             x2 = e.getX();
  66.             y2 = e.getY();
  67.         }
  68.        
  69.         getGraphics().drawLine(x1, y1, x2, y2);*/
  70.        
  71.         coordinate.add(new Linea(e.getX(), e.getY(), true));
  72.         repaint();
  73.     }
  74.  
  75.     @Override
  76.     public void mouseMoved(MouseEvent e) {
  77.         // TODO Auto-generated method stub
  78.     }
  79.  
  80.     @Override
  81.     public void mouseClicked(MouseEvent arg0) {
  82.         // TODO Auto-generated method stub
  83.        
  84.     }
  85.  
  86.     @Override
  87.     public void mouseEntered(MouseEvent arg0) {
  88.         // TODO Auto-generated method stub
  89.        
  90.     }
  91.  
  92.     @Override
  93.     public void mouseExited(MouseEvent arg0) {
  94.         // TODO Auto-generated method stub
  95.        
  96.     }
  97.  
  98.     @Override
  99.     public void mousePressed(MouseEvent e) {
  100.         coordinate.add(new Linea(e.getX(), e.getY(), false));
  101.        
  102.     }
  103.  
  104.     @Override
  105.     public void mouseReleased(MouseEvent e) {
  106.         /*x1 = 0;
  107.         y1 = 0;*/
  108.     }
  109.  
  110. }
  111.  
  112. //Classe da Vectorizzare
  113. package disegnaLinea;
  114.  
  115. public class Linea {
  116.     private int x, y;
  117.     private boolean isConnected;
  118.    
  119.     public Linea(int _x, int _y, boolean isConnected){
  120.         this.x = _x;
  121.         this.y = _y;
  122.         this.isConnected = isConnected;
  123.     }
  124.    
  125.     public int getX(){
  126.         return this.x;
  127.     }
  128.    
  129.     public int getY(){
  130.         return this.y;
  131.     }
  132.    
  133.     public boolean getConnected()
  134.     {
  135.         return isConnected;
  136.     }
  137.    
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement