YauhenMardan

Untitled

Mar 7th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6.  
  7. public class Polygon extends Figure2D {
  8. // fields
  9.  
  10. private ArrayList<Point> auxiliaryPoints;
  11. private int[] x, y;
  12. private int size;
  13.  
  14. // constructors
  15.  
  16. public Polygon(
  17. Point refPoint,
  18. Point defPoint,
  19. ArrayList<Point> auxiliaryPoints,
  20. Color borderColor,
  21. Color figureColor) {
  22. super(refPoint, defPoint, borderColor, figureColor);
  23. setAuxiliaryPoints(auxiliaryPoints);
  24. }
  25.  
  26. public Polygon(Polygon polygon) {
  27. super(polygon);
  28. }
  29.  
  30. // methods
  31.  
  32. @Override
  33. protected void paintComponent(Graphics g) {
  34. super.paintComponent(g);
  35. g.setColor(getFigureColor());
  36. g.fillPolygon(x, y, size);
  37. g.setColor(getBorderColor());
  38. g.drawRect(
  39. 0,
  40. 0,
  41. Math.abs(getDefPoint().x - getRefPoint().x),
  42. Math.abs(getDefPoint().y - getRefPoint().y));
  43. }
  44.  
  45. // getters and setters
  46.  
  47. public ArrayList<Point> getAuxiliaryPoints() {
  48. return auxiliaryPoints;
  49. }
  50.  
  51. public void setAuxiliaryPoints(ArrayList<Point> auxiliaryPoints) {
  52. this.auxiliaryPoints = auxiliaryPoints;
  53. if(auxiliaryPoints!=null){
  54. setXY();
  55. }
  56. }
  57.  
  58. private void setXY(){
  59. size = this.auxiliaryPoints.size();
  60. x = new int[size];
  61. y = new int[size];
  62. for (int i = 0; i < size; i++) {
  63. x[i] = this.auxiliaryPoints.get(i).x - getRefPoint().x;
  64. y[i] = this.auxiliaryPoints.get(i).y - getDefPoint().y;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment