Advertisement
Guest User

linhajava

a guest
May 25th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.54 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.*;
  3. import java.awt.geom.Line2D;
  4.  
  5. public class Linha extends Figura
  6. {
  7.     protected Ponto p1, p2;
  8.    
  9.     public Linha (int x1, int y1, int x2, int y2)
  10.     {
  11.         this (x1, y1, x2, y2, Color.BLACK);
  12.     }
  13.    
  14.     public Linha (int x1, int y1, int x2, int y2, Color cor)
  15.     {
  16.         super(cor);
  17.  
  18.         this.p1 = new Ponto (x1,y1,cor);
  19.         this.p2 = new Ponto (x2,y2,cor);
  20.     }
  21.  
  22.     public Linha (String l)
  23.     {
  24.         StringTokenizer quebrador = new StringTokenizer(l,":");
  25.  
  26.         quebrador.nextToken();
  27.  
  28.         int   x1  = Integer.parseInt(quebrador.nextToken());
  29.         int   y1  = Integer.parseInt(quebrador.nextToken());
  30.  
  31.         int   x2  = Integer.parseInt(quebrador.nextToken());
  32.         int   y2  = Integer.parseInt(quebrador.nextToken());
  33.  
  34.         Color cor = new Color (Integer.parseInt(quebrador.nextToken()),
  35.                                Integer.parseInt(quebrador.nextToken()),
  36.                                Integer.parseInt(quebrador.nextToken()));
  37.  
  38.         this.p1  = new Ponto (x1,y1,cor);
  39.         this.p2  = new Ponto (x2,y2,cor);
  40.         this.cor = cor;
  41.     }
  42.  
  43.     public void setP1 (int x, int y)
  44.     {
  45.         this.p1 = new Ponto (x,y,this.getCor());
  46.     }
  47.  
  48.     public void setP2 (int x, int y)
  49.     {
  50.         this.p2 = new Ponto (x,y,this.getCor());
  51.     }
  52.  
  53.     public Ponto getP1 ()
  54.     {
  55.         return this.p1;
  56.     }
  57.  
  58.     public Ponto getP2 ()
  59.     {
  60.         return this.p2;
  61.     }
  62.  
  63.     public void torneSeVisivel (Graphics g)
  64.     {
  65.         g.setColor(this.cor);
  66.         g.drawLine(this.p1.getX(), this.p1.getY(),
  67.                    this.p2.getX(), this.p2.getY());
  68.     }
  69.  
  70.     public String toString()
  71.     {
  72.         return "l:" +
  73.                this.p1.getX() +
  74.                ":" +
  75.                this.p1.getY() +
  76.                ":" +
  77.                this.p2.getX() +
  78.                ":" +
  79.                this.p2.getY() +
  80.                ":" +
  81.                this.getCor().getRed() +
  82.                ":" +
  83.                this.getCor().getGreen() +
  84.                ":" +
  85.                this.getCor().getBlue();
  86.     }
  87.    
  88.     public int espaco(int x, int y){
  89.         int i = 2;
  90.         int areaX = x - i / 2;
  91.         int areaY = y - i / 2;     
  92.        
  93.         Line2D linha_aux;
  94.         linha_aux = new Line2D.Double();
  95.         linha_aux.setLine((int)this.p1.getX(), (int)this.p1.getY(), (int)this.p2.getX(), (int)this.p2.getY());
  96.        
  97.         if(linha_aux.intersects(areaX, areaY, i, i)) {
  98.             return 1;
  99.         }
  100.         else{
  101.             return 0;
  102.         }
  103.  
  104.     }
  105.    
  106.     public void mover(int x, int y)
  107.     {  
  108.         double distx = Math.sqrt(Math.pow(this.p1.getX() - x,2));
  109.         double disty = Math.sqrt(Math.pow(this.p1.getY() - y,2));
  110.        
  111.         if(x >= this.p1.getX()){
  112.             if(y >= this.p1.getY()){
  113.                 this.p2.setX((int)(this.p2.getX() + distx));
  114.                 this.p2.setY((int)(this.p2.getY() + disty));
  115.                 this.p1.setX(x);
  116.                 this.p1.setY(y);
  117.             }
  118.             else{
  119.                 this.p2.setX((int)(this.p2.getX() + distx));
  120.                 this.p2.setY((int)(this.p2.getY() - disty));
  121.                 this.p1.setX(x);
  122.                 this.p1.setY(y);
  123.             }
  124.         }
  125.         else{
  126.             if(y >= this.p1.getY()){
  127.                 this.p2.setX((int)(this.p2.getX() - distx));
  128.                 this.p2.setY((int)(this.p2.getY() + disty));
  129.                 this.p1.setX(x);
  130.                 this.p1.setY(y);
  131.             }
  132.             else{
  133.                 this.p2.setX((int)(this.p2.getX() - distx));
  134.                 this.p2.setY((int)(this.p2.getY() - disty));
  135.                 this.p1.setX(x);
  136.                 this.p1.setY(y);
  137.             }
  138.         }
  139.        
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement