Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.*;
- import java.awt.geom.Line2D;
- public class Linha extends Figura
- {
- protected Ponto p1, p2;
- public Linha (int x1, int y1, int x2, int y2)
- {
- this (x1, y1, x2, y2, Color.BLACK);
- }
- public Linha (int x1, int y1, int x2, int y2, Color cor)
- {
- super(cor);
- this.p1 = new Ponto (x1,y1,cor);
- this.p2 = new Ponto (x2,y2,cor);
- }
- public Linha (String l)
- {
- StringTokenizer quebrador = new StringTokenizer(l,":");
- quebrador.nextToken();
- int x1 = Integer.parseInt(quebrador.nextToken());
- int y1 = Integer.parseInt(quebrador.nextToken());
- int x2 = Integer.parseInt(quebrador.nextToken());
- int y2 = Integer.parseInt(quebrador.nextToken());
- Color cor = new Color (Integer.parseInt(quebrador.nextToken()),
- Integer.parseInt(quebrador.nextToken()),
- Integer.parseInt(quebrador.nextToken()));
- this.p1 = new Ponto (x1,y1,cor);
- this.p2 = new Ponto (x2,y2,cor);
- this.cor = cor;
- }
- public void setP1 (int x, int y)
- {
- this.p1 = new Ponto (x,y,this.getCor());
- }
- public void setP2 (int x, int y)
- {
- this.p2 = new Ponto (x,y,this.getCor());
- }
- public Ponto getP1 ()
- {
- return this.p1;
- }
- public Ponto getP2 ()
- {
- return this.p2;
- }
- public void torneSeVisivel (Graphics g)
- {
- g.setColor(this.cor);
- g.drawLine(this.p1.getX(), this.p1.getY(),
- this.p2.getX(), this.p2.getY());
- }
- public String toString()
- {
- return "l:" +
- this.p1.getX() +
- ":" +
- this.p1.getY() +
- ":" +
- this.p2.getX() +
- ":" +
- this.p2.getY() +
- ":" +
- this.getCor().getRed() +
- ":" +
- this.getCor().getGreen() +
- ":" +
- this.getCor().getBlue();
- }
- public int espaco(int x, int y){
- int i = 2;
- int areaX = x - i / 2;
- int areaY = y - i / 2;
- Line2D linha_aux;
- linha_aux = new Line2D.Double();
- linha_aux.setLine((int)this.p1.getX(), (int)this.p1.getY(), (int)this.p2.getX(), (int)this.p2.getY());
- if(linha_aux.intersects(areaX, areaY, i, i)) {
- return 1;
- }
- else{
- return 0;
- }
- }
- public void mover(int x, int y)
- {
- double distx = Math.sqrt(Math.pow(this.p1.getX() - x,2));
- double disty = Math.sqrt(Math.pow(this.p1.getY() - y,2));
- if(x >= this.p1.getX()){
- if(y >= this.p1.getY()){
- this.p2.setX((int)(this.p2.getX() + distx));
- this.p2.setY((int)(this.p2.getY() + disty));
- this.p1.setX(x);
- this.p1.setY(y);
- }
- else{
- this.p2.setX((int)(this.p2.getX() + distx));
- this.p2.setY((int)(this.p2.getY() - disty));
- this.p1.setX(x);
- this.p1.setY(y);
- }
- }
- else{
- if(y >= this.p1.getY()){
- this.p2.setX((int)(this.p2.getX() - distx));
- this.p2.setY((int)(this.p2.getY() + disty));
- this.p1.setX(x);
- this.p1.setY(y);
- }
- else{
- this.p2.setX((int)(this.p2.getX() - distx));
- this.p2.setY((int)(this.p2.getY() - disty));
- this.p1.setX(x);
- this.p1.setY(y);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement