Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.21 KB | None | 0 0
  1. package asddad;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. public class Point extends Shape{
  7. private int x;
  8. private int y;
  9.  
  10. public Point() {}
  11. public Point(int x, int y, boolean selected) {
  12.     this.x = x;
  13.     this.y = y;
  14.     setSelected(selected);
  15. }
  16. public Point(int x,int y,boolean selected,Color color) {
  17.     this(x,y,selected);
  18.     this.setColor(color);
  19. }
  20. public boolean equals(Object o) {
  21.     if(o instanceof Point) {
  22.     return true;   
  23.     }return true;
  24. }
  25. public double distance(int x2, int y2) {
  26.     double dx = x - x2;
  27.     double dy = y - y2;
  28.     double d = Math.sqrt(dx*dx + dy * dy);
  29.     return d;
  30. }
  31. public double distance(Point p) {
  32.     double dx= x - p.getX();
  33.     double dy= y - p.getY();
  34.     double d = Math.sqrt(dx*dx + dy * dy);
  35.     return d;
  36. }
  37. public boolean contains(int x,int y) {
  38.    
  39.     return this.distance(x,y)<=3;
  40. }
  41.  
  42. public int getX() {
  43.     return x;
  44. }
  45.  
  46. public void setX(int x) {
  47.     this.x = x;
  48. }
  49.  
  50. public int getY() {
  51.     return y;
  52. }
  53.  
  54. public void setY(int y) {
  55.     this.y = y;
  56. }
  57. public String toString(){
  58.     return "("+this.x+","+this.y+")";
  59. }
  60. //@Override
  61. public void draw(Graphics g) {
  62.     g.drawLine(this.x-2,this.y,this.x+2,this.y);
  63.     g.drawLine(this.x,this.y-2,this.x,this.y+2);
  64.     g.setColor(getColor());
  65.    
  66.     if(isSelected()) {
  67.         g.setColor(Color.BLUE);
  68.         g.drawRect(this.x-3, this.y-3,6,6);
  69.     }
  70.    
  71. }
  72. @Override
  73. public void moveBy(int byX, int byY) {
  74.     this.setX(this.x+byX);
  75.     this.setY(byY+this.y);
  76. }
  77. @Override
  78. public int compareTo(Object o) {
  79.     if(o instanceof Point) {
  80.         Point p=(Point)o;
  81.         return (int) (this.distance(x,y)-p.distance(p.getX(),p.getY()));
  82.     }
  83.     return 0;
  84. }
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. package asddad;
  94.  
  95. import java.awt.Color;
  96. import java.awt.Graphics;
  97.  
  98. public class Circle extends SurfaceShape {
  99. private Point center;
  100. private int radius;
  101. public Circle() {}
  102. public Circle(Point center, int radius, boolean selected) {
  103.     this.center = center;
  104.     this.radius = radius;
  105.     setSelected(selected);
  106. }
  107. public Circle(Point center, int radius, boolean selected,Color color) {
  108.     this.center = center;
  109.     this.radius = radius;
  110.     setSelected(selected);
  111.     setColor(color);
  112. }
  113. public Circle(Point center, int radius, boolean selected,Color color,Color innerColor) {
  114.     this.center = center;
  115.     this.radius = radius;
  116.     setSelected(selected);
  117.     setColor(color);
  118.     setInnerColor(innerColor);
  119. }
  120. public boolean contains(int x,int y){
  121.     return center.distance(x,y)<radius;
  122. }
  123. public Point getCenter() {
  124.     return center;
  125. }
  126. public void setCenter(Point center) {
  127.     this.center = center;
  128. }
  129. public int getRadius() {
  130.     return radius;
  131. }
  132. public void setRadius(int radius) throws Exception {
  133.     if(radius>0)
  134.     {
  135.         this.radius = radius;      
  136.     }else {
  137.         throw new NumberFormatException("Radius has to be a value greater than zero");
  138.     }
  139.  
  140. }
  141. public String toString() {
  142.     return "Center=" + center + ", radius=" + radius;
  143. }
  144. @Override
  145. public void draw(Graphics g) {
  146.     g.setColor(getColor());
  147.     g.drawOval(center.getX()-radius,center.getY()-radius,radius*2-2,radius*2-2);
  148.     this.fill(g);
  149.     if(isSelected()){
  150.         g.setColor(Color.blue);
  151.         g.drawRect(center.getX()-3,center.getY()-3,6,6);
  152.         g.drawRect(center.getX()-3-radius,center.getY()-3,6,6);
  153.         g.drawRect(center.getX()-3+radius,center.getY()-3,6,6);
  154.         g.drawRect(center.getX()-3,center.getY()-radius-3,6,6);
  155.         g.drawRect(center.getX()-3,center.getY()+radius-3,6,6);    
  156.     }
  157. }
  158. @Override
  159. public void fill(Graphics g) {
  160.     g.setColor(getInnerColor());
  161.     g.fillOval(center.getX()-radius+1,center.getY()-radius+1,radius*2,radius*2);
  162. }
  163. @Override
  164. public void moveBy(int byX, int byY) {
  165. this.center.moveBy(byX, byY);
  166. }
  167. @Override
  168. public int compareTo(Object o) {
  169.     if(o instanceof Circle) {
  170.         return this.radius-((((Circle) o).getRadius()));
  171.     }
  172.     return 0;
  173. }
  174. @Override
  175. public double area() {
  176. return radius*radius*Math.PI;
  177. }
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. package asddad;
  187.  
  188. import java.awt.Color;
  189. import java.awt.Graphics;
  190. import java.util.ArrayList;
  191. import java.util.Iterator;
  192.  
  193. import javax.swing.JFrame;
  194. import javax.swing.JPanel;
  195.  
  196. public class Drawing extends JPanel {
  197.  
  198.     public static void main(String[] args) {
  199.        
  200.         JFrame frame=new JFrame("asddddddddddda");
  201.         frame.setSize(800,600);
  202.         Drawing drawing=new Drawing();
  203.         frame.getContentPane().add(drawing);
  204.         frame.setVisible(true);
  205.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  206.                
  207.     }
  208.     public void paint(Graphics g) {
  209.         Point p=new Point();
  210.         Point p2=new Point(10,20,false);
  211.         Point p1=new Point(20,30,false);       
  212.         Donut d=new Donut(p,20,30,false);
  213.        
  214.         //System.out.println(d.toString());
  215.        
  216.         ArrayList<Shape>shapes=new ArrayList<Shape>();
  217.         shapes.add(p);
  218.         shapes.add(d);
  219.         shapes.add(p1);
  220.         shapes.add(p2);
  221.         Iterator<Shape>it=shapes.iterator();
  222.         while(it.hasNext()) {
  223.             System.out.println("Selected:"+it.next().isSelected());
  224.         }
  225.         shapes.get(2).draw(g);
  226.         shapes.get(shapes.size()-1);
  227.        
  228.         shapes.get(3).draw(g);
  229.        
  230.         Line newLine=new Line(new Point(150,150,false),new Point(350,350,false),false);
  231.         shapes.add(newLine);
  232.         shapes.remove(1);
  233.        
  234.         while(it.hasNext())
  235.         {
  236.             it.next().setSelected(true);
  237.         }
  238.         while(it.hasNext())
  239.         {
  240.             it.next().draw(g);
  241.            
  242.         }
  243.         while(it.hasNext())
  244.         {
  245.             if(it.next() instanceof SurfaceShape)
  246.             {
  247.                 it.next().draw(g);
  248.             }
  249.         }
  250.     }
  251. }
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. package asddad;
  261.  
  262. import java.awt.Color;
  263. import java.awt.Graphics;
  264.  
  265. public class Donut extends Circle {
  266. private int innerRadius;
  267.  
  268. public Donut() {}
  269. public Donut(Point center,int innerRadius,int radius,boolean selected) {
  270.     super(center,radius,selected);
  271.     this.innerRadius=innerRadius;
  272. }
  273. public boolean equals(Object obj)
  274. {
  275.     if(obj instanceof Donut)
  276.     {
  277.         Donut krofna=(Donut)obj;
  278.         if(krofna.getRadius()==this.getRadius() &&
  279.                 krofna.getInnerRadius()==this.getInnerRadius() &&
  280.                 krofna.getCenter().equals(this.getCenter())) {
  281.             return true;
  282.         }else return false;            
  283.     }else return false;
  284. }
  285. public boolean contains(int x,int y) {
  286.     double dFromCenter=this.getCenter().distance(x, y);
  287.     return super.contains(x, y) && dFromCenter >= innerRadius;
  288. }
  289. public int getInnerRadius() {
  290.     return innerRadius;
  291. }
  292. public void setInnerRadius(int innerRadius) {
  293.     this.innerRadius = innerRadius;
  294. }
  295. public String toString() {
  296.     return super.toString()+"Inner Radius:"+innerRadius;
  297. }
  298. public void draw(Graphics g)
  299. {  
  300.    
  301.     g.drawOval(this.getCenter().getX()-getRadius()/2,getCenter().getY()-getRadius()/2, getRadius(), getRadius());  
  302.     g.drawOval(this.getCenter().getX() - getInnerRadius() ,getCenter().getY() - getInnerRadius(), innerRadius*2, innerRadius*2);
  303.    
  304. }
  305. public void fill(Graphics g) {
  306.    
  307.     super.fill(g);
  308.     g.setColor(Color.white);
  309.     g.fillOval(this.getCenter().getX() - getInnerRadius() ,getCenter().getY() - getInnerRadius(), innerRadius*2, innerRadius*2);
  310. }
  311. @Override
  312. public int compareTo(Object o) {
  313.    
  314.     return 0;
  315. }
  316. }
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324. package asddad;
  325.  
  326. import java.awt.Color;
  327. import java.awt.Graphics;
  328.  
  329. public class Line extends Shape{
  330. private Point startPoint;
  331. private Point endPoint;
  332. private boolean selected;
  333.  
  334. public Line() {}
  335. public Line(Point startPoint, Point endPoint, boolean selected) {
  336.     this.startPoint = startPoint;
  337.     this.endPoint = endPoint;
  338.     this.selected = selected;
  339. }
  340. public Line(Point startPoint,Point endPoint,boolean selected,Color color) {
  341.     this(startPoint,endPoint,selected);
  342.     setColor(color);
  343. }
  344. public double length() {
  345.     return startPoint.distance(endPoint);
  346. }
  347. public boolean contains(int x,int y) {
  348.     if(startPoint.distance(x,y)+endPoint.distance(x,y)-length()<=0.05){
  349.         return true;
  350.     }else return false;
  351. }
  352. public Point getStartPoint() {
  353.     return startPoint;
  354. }
  355. public void setStartPoint(Point startPoint) {
  356.     this.startPoint = startPoint;
  357. }
  358. public Point getEndPoint() {
  359.     return endPoint;
  360. }
  361. public void setEndPoint(Point endPoint) {
  362.     this.endPoint = endPoint;
  363. }
  364. public boolean isSelected() {
  365.     return selected;
  366. }
  367. public void setSelected(boolean selected) {
  368.     this.selected = selected;
  369. }
  370. public Point middleOfLine() {
  371.     int middleByX=(this.startPoint.getX()+this.endPoint.getX())/2;
  372.     int middleByY=(this.startPoint.getY()+this.endPoint.getY())/2;
  373.     Point p=new Point(middleByX,middleByY,false);
  374.     return p;
  375.    
  376. }
  377. @Override
  378. public void draw(Graphics g) {
  379.     g.drawLine(getStartPoint().getX(),getStartPoint().getY(), getEndPoint().getX(),getEndPoint().getY());
  380.     if (isSelected())
  381.     {
  382.         g.setColor(Color.BLUE);
  383.         g.drawRect(this.startPoint.getX()-10,this.startPoint.getY()-10, 20, 20);
  384.         g.drawRect(this.endPoint.getX()-10,this.endPoint.getY()-10, 20, 20);
  385.         g.drawRect(this.startPoint.getX()-10,this.startPoint.getY()-10, 20, 20);
  386.         g.drawRect(middleOfLine().getX()-10,middleOfLine().getY()-10,20,20);
  387.     }
  388. }
  389. @Override
  390. public void moveBy(int byX, int byY) {
  391.     this.startPoint.moveBy(byX, byY);
  392.     this.endPoint.moveBy(byX, byY);
  393. }
  394. @Override
  395. public int compareTo(Object o) {
  396.     if(o instanceof Line)
  397.     {
  398.         Line pomocna=(Line)o;
  399.         return (int) (this.length()-pomocna.length());
  400.     }
  401.     return 0;
  402. }
  403. }
  404.  
  405.  
  406.  
  407.  
  408. package asddad;
  409.  
  410. public interface Moveable {
  411.    
  412.     void moveBy(int byX,int byY);  
  413. }
  414.  
  415.  
  416.  
  417.  
  418. package asddad;
  419.  
  420. import java.awt.Color;
  421. import java.awt.Graphics;
  422.  
  423. public class Rectangle extends SurfaceShape{
  424.    
  425.     private int height;
  426.     private int width;
  427.     private Point upperLeft;
  428.  
  429. public Rectangle() {}
  430. public Rectangle(int height, int width, Point upperLeft, boolean selected) {
  431.    
  432.     this.height = height;
  433.     this.width = width;
  434.     this.upperLeft = upperLeft;
  435.     setSelected(selected);
  436. }
  437. public Rectangle(int height, int width, Point upperLeft, boolean selected,Color color) {
  438.     this(width,height,upperLeft,selected);
  439.  
  440. }
  441. public boolean equals(Object o)
  442. {
  443.     if(o instanceof Rectangle) {
  444.         Rectangle pomocni=(Rectangle) o;
  445.         if(height==pomocni.getHeight()
  446.                 && width==pomocni.getWidth()
  447.                 && upperLeft.equals(pomocni.getUpperLeft())){
  448.             return true;
  449.         }else return false;
  450.     }else return false;
  451. }
  452. public boolean contains(int x,int y){
  453.     if(x<=upperLeft.getX()+width &&
  454.             y<=upperLeft.getY()+height &&
  455.             x>=upperLeft.getX() &&
  456.             y>=upperLeft.getY()){
  457.         return true;
  458.     }else return false;
  459. }
  460. public boolean contains(Point p){
  461.     if(p.getX()<=upperLeft.getX()+width &&
  462.             p.getY()<=upperLeft.getY()+height &&
  463.             p.getX()>=upperLeft.getX() &&
  464.             p.getY()>=upperLeft.getY()){
  465.         return true;
  466.     }else return false;
  467. }
  468. public double area() {
  469.     return height*width;
  470. }
  471. public int getHeight() {
  472.     return height;
  473. }
  474. public void setHeight(int height) {
  475.     this.height = height;
  476. }
  477. public int getWidth() {
  478.     return width;
  479. }
  480. public void setWidth(int width) {
  481.     this.width = width;
  482. }
  483. public Point getUpperLeft() {
  484.     return upperLeft;
  485. }
  486. public void setUpperLeft(Point upperLeft) {
  487.     this.upperLeft = upperLeft;
  488. }
  489. @Override
  490. public void fill(Graphics g) {
  491.     g.setColor(getInnerColor());
  492.     g.fillRect(this.upperLeft.getX(), this.upperLeft.getY(), this.width, this.height);
  493.    
  494. }
  495. @Override
  496. public void draw(Graphics g) {
  497.     g.setColor(getColor());
  498.     g.drawRect(this.upperLeft.getX(),this.upperLeft.getY(),this.width,this.height);
  499.     this.fill(g);
  500.     if(this.isSelected()) {
  501.         g.setColor(Color.blue);
  502.         g.drawRect(this.getUpperLeft().getX()-3, this.getUpperLeft().getY()-3, 6, 6);
  503.         g.drawRect(this.getUpperLeft().getX()-3+width, this.getUpperLeft().getY()-3, 6, 6);
  504.         g.drawRect(this.getUpperLeft().getX()-3, this.getHeight()+height,6,6);
  505.         g.drawRect(this.getUpperLeft().getX()+width, this.getUpperLeft().getY()-3,6,6);    
  506.     }
  507.    
  508. }
  509. @Override
  510. public void moveBy(int byX, int byY) {
  511. this.upperLeft.moveBy(byX, byY);   
  512. }
  513. @Override
  514. public int compareTo(Object o) {
  515.     if(o instanceof Rectangle)
  516.     {
  517.         Rectangle pomocni=(Rectangle)o;
  518.         return (int)(this.area()-pomocni.area());
  519.     }else return 0;
  520. }
  521. }
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530. package asddad;
  531.  
  532. import java.awt.Color;
  533. import java.awt.Graphics;
  534.  
  535. public abstract class Shape implements Moveable,Comparable{
  536.  
  537.     private boolean selected;
  538.     private Color color;
  539.    
  540.     public abstract boolean contains(int x,int y);
  541.     public abstract void draw(Graphics g);
  542.    
  543.     public boolean isSelected() {
  544.         return selected;
  545.     }
  546.     public void setSelected(boolean selected) {
  547.         this.selected = selected;
  548.     }
  549.     public Color getColor() {
  550.         return color;
  551.     }
  552.     public void setColor(Color color) {
  553.         this.color = color;
  554.     }
  555.    
  556. }
  557.  
  558.  
  559.  
  560.  
  561.  
  562. package asddad;
  563.  
  564. import java.awt.Color;
  565. import java.awt.Graphics;
  566.  
  567. public abstract class SurfaceShape extends Shape {
  568.  
  569.     private Color innerColor;
  570.    
  571.     public abstract double area();
  572.     public abstract void fill(Graphics g);
  573.  
  574.     public Color getInnerColor() {
  575.         return innerColor;
  576.     }
  577.     public void setInnerColor(Color innerColor) {
  578.         this.innerColor = innerColor;
  579.     }
  580. }
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588. package asddad;
  589.  
  590. import java.util.HashMap;
  591.  
  592. public class Vezba {
  593.  
  594.     public static void main(String[]args)
  595.     {
  596.         //Hashmape 
  597.        
  598.         Point p2=new Point(10,20,false);
  599.         Point p3=new Point(300,50,false);
  600.         Rectangle r1=new Rectangle(30,20,p2,false);
  601.         HashMap<String,Shape> map=new HashMap<String,Shape>(); 
  602.        
  603.         map.put("point", p2);
  604.         map.put("rectangle", r1);
  605.         map.put("Point",p3);
  606.        
  607.         System.out.println("point from map is:"+map.get("point"));
  608.        
  609.         Point p11=new Point(50,50,false);
  610.         map.put("point", p11);
  611.         System.out.println("point from map is:"+map.get("point"));
  612.        
  613.         Circle c3=new Circle(new Point(50,50,false),40,false);
  614.         try {
  615.             c3.setRadius(-150);
  616.         }
  617.         catch(Exception e){    
  618.             e.printStackTrace();
  619.         }
  620.         finally{
  621.            
  622.         }
  623.        
  624.        
  625.            
  626.    
  627.     }
  628. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement