Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Point extends Shape{
- private int x;
- private int y;
- public Point() {}
- public Point(int x, int y, boolean selected) {
- this.x = x;
- this.y = y;
- setSelected(selected);
- }
- public Point(int x,int y,boolean selected,Color color) {
- this(x,y,selected);
- this.setColor(color);
- }
- public boolean equals(Object o) {
- if(o instanceof Point) {
- return true;
- }return true;
- }
- public double distance(int x2, int y2) {
- double dx = x - x2;
- double dy = y - y2;
- double d = Math.sqrt(dx*dx + dy * dy);
- return d;
- }
- public double distance(Point p) {
- double dx= x - p.getX();
- double dy= y - p.getY();
- double d = Math.sqrt(dx*dx + dy * dy);
- return d;
- }
- public boolean contains(int x,int y) {
- return this.distance(x,y)<=3;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public String toString(){
- return "("+this.x+","+this.y+")";
- }
- //@Override
- public void draw(Graphics g) {
- g.drawLine(this.x-2,this.y,this.x+2,this.y);
- g.drawLine(this.x,this.y-2,this.x,this.y+2);
- g.setColor(getColor());
- if(isSelected()) {
- g.setColor(Color.BLUE);
- g.drawRect(this.x-3, this.y-3,6,6);
- }
- }
- @Override
- public void moveBy(int byX, int byY) {
- this.setX(this.x+byX);
- this.setY(byY+this.y);
- }
- @Override
- public int compareTo(Object o) {
- if(o instanceof Point) {
- Point p=(Point)o;
- return (int) (this.distance(x,y)-p.distance(p.getX(),p.getY()));
- }
- return 0;
- }
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Circle extends SurfaceShape {
- private Point center;
- private int radius;
- public Circle() {}
- public Circle(Point center, int radius, boolean selected) {
- this.center = center;
- this.radius = radius;
- setSelected(selected);
- }
- public Circle(Point center, int radius, boolean selected,Color color) {
- this.center = center;
- this.radius = radius;
- setSelected(selected);
- setColor(color);
- }
- public Circle(Point center, int radius, boolean selected,Color color,Color innerColor) {
- this.center = center;
- this.radius = radius;
- setSelected(selected);
- setColor(color);
- setInnerColor(innerColor);
- }
- public boolean contains(int x,int y){
- return center.distance(x,y)<radius;
- }
- public Point getCenter() {
- return center;
- }
- public void setCenter(Point center) {
- this.center = center;
- }
- public int getRadius() {
- return radius;
- }
- public void setRadius(int radius) throws Exception {
- if(radius>0)
- {
- this.radius = radius;
- }else {
- throw new NumberFormatException("Radius has to be a value greater than zero");
- }
- }
- public String toString() {
- return "Center=" + center + ", radius=" + radius;
- }
- @Override
- public void draw(Graphics g) {
- g.setColor(getColor());
- g.drawOval(center.getX()-radius,center.getY()-radius,radius*2-2,radius*2-2);
- this.fill(g);
- if(isSelected()){
- g.setColor(Color.blue);
- g.drawRect(center.getX()-3,center.getY()-3,6,6);
- g.drawRect(center.getX()-3-radius,center.getY()-3,6,6);
- g.drawRect(center.getX()-3+radius,center.getY()-3,6,6);
- g.drawRect(center.getX()-3,center.getY()-radius-3,6,6);
- g.drawRect(center.getX()-3,center.getY()+radius-3,6,6);
- }
- }
- @Override
- public void fill(Graphics g) {
- g.setColor(getInnerColor());
- g.fillOval(center.getX()-radius+1,center.getY()-radius+1,radius*2,radius*2);
- }
- @Override
- public void moveBy(int byX, int byY) {
- this.center.moveBy(byX, byY);
- }
- @Override
- public int compareTo(Object o) {
- if(o instanceof Circle) {
- return this.radius-((((Circle) o).getRadius()));
- }
- return 0;
- }
- @Override
- public double area() {
- return radius*radius*Math.PI;
- }
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.util.ArrayList;
- import java.util.Iterator;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- public class Drawing extends JPanel {
- public static void main(String[] args) {
- JFrame frame=new JFrame("asddddddddddda");
- frame.setSize(800,600);
- Drawing drawing=new Drawing();
- frame.getContentPane().add(drawing);
- frame.setVisible(true);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void paint(Graphics g) {
- Point p=new Point();
- Point p2=new Point(10,20,false);
- Point p1=new Point(20,30,false);
- Donut d=new Donut(p,20,30,false);
- //System.out.println(d.toString());
- ArrayList<Shape>shapes=new ArrayList<Shape>();
- shapes.add(p);
- shapes.add(d);
- shapes.add(p1);
- shapes.add(p2);
- Iterator<Shape>it=shapes.iterator();
- while(it.hasNext()) {
- System.out.println("Selected:"+it.next().isSelected());
- }
- shapes.get(2).draw(g);
- shapes.get(shapes.size()-1);
- shapes.get(3).draw(g);
- Line newLine=new Line(new Point(150,150,false),new Point(350,350,false),false);
- shapes.add(newLine);
- shapes.remove(1);
- while(it.hasNext())
- {
- it.next().setSelected(true);
- }
- while(it.hasNext())
- {
- it.next().draw(g);
- }
- while(it.hasNext())
- {
- if(it.next() instanceof SurfaceShape)
- {
- it.next().draw(g);
- }
- }
- }
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Donut extends Circle {
- private int innerRadius;
- public Donut() {}
- public Donut(Point center,int innerRadius,int radius,boolean selected) {
- super(center,radius,selected);
- this.innerRadius=innerRadius;
- }
- public boolean equals(Object obj)
- {
- if(obj instanceof Donut)
- {
- Donut krofna=(Donut)obj;
- if(krofna.getRadius()==this.getRadius() &&
- krofna.getInnerRadius()==this.getInnerRadius() &&
- krofna.getCenter().equals(this.getCenter())) {
- return true;
- }else return false;
- }else return false;
- }
- public boolean contains(int x,int y) {
- double dFromCenter=this.getCenter().distance(x, y);
- return super.contains(x, y) && dFromCenter >= innerRadius;
- }
- public int getInnerRadius() {
- return innerRadius;
- }
- public void setInnerRadius(int innerRadius) {
- this.innerRadius = innerRadius;
- }
- public String toString() {
- return super.toString()+"Inner Radius:"+innerRadius;
- }
- public void draw(Graphics g)
- {
- g.drawOval(this.getCenter().getX()-getRadius()/2,getCenter().getY()-getRadius()/2, getRadius(), getRadius());
- g.drawOval(this.getCenter().getX() - getInnerRadius() ,getCenter().getY() - getInnerRadius(), innerRadius*2, innerRadius*2);
- }
- public void fill(Graphics g) {
- super.fill(g);
- g.setColor(Color.white);
- g.fillOval(this.getCenter().getX() - getInnerRadius() ,getCenter().getY() - getInnerRadius(), innerRadius*2, innerRadius*2);
- }
- @Override
- public int compareTo(Object o) {
- return 0;
- }
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Line extends Shape{
- private Point startPoint;
- private Point endPoint;
- private boolean selected;
- public Line() {}
- public Line(Point startPoint, Point endPoint, boolean selected) {
- this.startPoint = startPoint;
- this.endPoint = endPoint;
- this.selected = selected;
- }
- public Line(Point startPoint,Point endPoint,boolean selected,Color color) {
- this(startPoint,endPoint,selected);
- setColor(color);
- }
- public double length() {
- return startPoint.distance(endPoint);
- }
- public boolean contains(int x,int y) {
- if(startPoint.distance(x,y)+endPoint.distance(x,y)-length()<=0.05){
- return true;
- }else return false;
- }
- public Point getStartPoint() {
- return startPoint;
- }
- public void setStartPoint(Point startPoint) {
- this.startPoint = startPoint;
- }
- public Point getEndPoint() {
- return endPoint;
- }
- public void setEndPoint(Point endPoint) {
- this.endPoint = endPoint;
- }
- public boolean isSelected() {
- return selected;
- }
- public void setSelected(boolean selected) {
- this.selected = selected;
- }
- public Point middleOfLine() {
- int middleByX=(this.startPoint.getX()+this.endPoint.getX())/2;
- int middleByY=(this.startPoint.getY()+this.endPoint.getY())/2;
- Point p=new Point(middleByX,middleByY,false);
- return p;
- }
- @Override
- public void draw(Graphics g) {
- g.drawLine(getStartPoint().getX(),getStartPoint().getY(), getEndPoint().getX(),getEndPoint().getY());
- if (isSelected())
- {
- g.setColor(Color.BLUE);
- g.drawRect(this.startPoint.getX()-10,this.startPoint.getY()-10, 20, 20);
- g.drawRect(this.endPoint.getX()-10,this.endPoint.getY()-10, 20, 20);
- g.drawRect(this.startPoint.getX()-10,this.startPoint.getY()-10, 20, 20);
- g.drawRect(middleOfLine().getX()-10,middleOfLine().getY()-10,20,20);
- }
- }
- @Override
- public void moveBy(int byX, int byY) {
- this.startPoint.moveBy(byX, byY);
- this.endPoint.moveBy(byX, byY);
- }
- @Override
- public int compareTo(Object o) {
- if(o instanceof Line)
- {
- Line pomocna=(Line)o;
- return (int) (this.length()-pomocna.length());
- }
- return 0;
- }
- }
- package asddad;
- public interface Moveable {
- void moveBy(int byX,int byY);
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Rectangle extends SurfaceShape{
- private int height;
- private int width;
- private Point upperLeft;
- public Rectangle() {}
- public Rectangle(int height, int width, Point upperLeft, boolean selected) {
- this.height = height;
- this.width = width;
- this.upperLeft = upperLeft;
- setSelected(selected);
- }
- public Rectangle(int height, int width, Point upperLeft, boolean selected,Color color) {
- this(width,height,upperLeft,selected);
- }
- public boolean equals(Object o)
- {
- if(o instanceof Rectangle) {
- Rectangle pomocni=(Rectangle) o;
- if(height==pomocni.getHeight()
- && width==pomocni.getWidth()
- && upperLeft.equals(pomocni.getUpperLeft())){
- return true;
- }else return false;
- }else return false;
- }
- public boolean contains(int x,int y){
- if(x<=upperLeft.getX()+width &&
- y<=upperLeft.getY()+height &&
- x>=upperLeft.getX() &&
- y>=upperLeft.getY()){
- return true;
- }else return false;
- }
- public boolean contains(Point p){
- if(p.getX()<=upperLeft.getX()+width &&
- p.getY()<=upperLeft.getY()+height &&
- p.getX()>=upperLeft.getX() &&
- p.getY()>=upperLeft.getY()){
- return true;
- }else return false;
- }
- public double area() {
- return height*width;
- }
- public int getHeight() {
- return height;
- }
- public void setHeight(int height) {
- this.height = height;
- }
- public int getWidth() {
- return width;
- }
- public void setWidth(int width) {
- this.width = width;
- }
- public Point getUpperLeft() {
- return upperLeft;
- }
- public void setUpperLeft(Point upperLeft) {
- this.upperLeft = upperLeft;
- }
- @Override
- public void fill(Graphics g) {
- g.setColor(getInnerColor());
- g.fillRect(this.upperLeft.getX(), this.upperLeft.getY(), this.width, this.height);
- }
- @Override
- public void draw(Graphics g) {
- g.setColor(getColor());
- g.drawRect(this.upperLeft.getX(),this.upperLeft.getY(),this.width,this.height);
- this.fill(g);
- if(this.isSelected()) {
- g.setColor(Color.blue);
- g.drawRect(this.getUpperLeft().getX()-3, this.getUpperLeft().getY()-3, 6, 6);
- g.drawRect(this.getUpperLeft().getX()-3+width, this.getUpperLeft().getY()-3, 6, 6);
- g.drawRect(this.getUpperLeft().getX()-3, this.getHeight()+height,6,6);
- g.drawRect(this.getUpperLeft().getX()+width, this.getUpperLeft().getY()-3,6,6);
- }
- }
- @Override
- public void moveBy(int byX, int byY) {
- this.upperLeft.moveBy(byX, byY);
- }
- @Override
- public int compareTo(Object o) {
- if(o instanceof Rectangle)
- {
- Rectangle pomocni=(Rectangle)o;
- return (int)(this.area()-pomocni.area());
- }else return 0;
- }
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public abstract class Shape implements Moveable,Comparable{
- private boolean selected;
- private Color color;
- public abstract boolean contains(int x,int y);
- public abstract void draw(Graphics g);
- public boolean isSelected() {
- return selected;
- }
- public void setSelected(boolean selected) {
- this.selected = selected;
- }
- public Color getColor() {
- return color;
- }
- public void setColor(Color color) {
- this.color = color;
- }
- }
- package asddad;
- import java.awt.Color;
- import java.awt.Graphics;
- public abstract class SurfaceShape extends Shape {
- private Color innerColor;
- public abstract double area();
- public abstract void fill(Graphics g);
- public Color getInnerColor() {
- return innerColor;
- }
- public void setInnerColor(Color innerColor) {
- this.innerColor = innerColor;
- }
- }
- package asddad;
- import java.util.HashMap;
- public class Vezba {
- public static void main(String[]args)
- {
- //Hashmape
- Point p2=new Point(10,20,false);
- Point p3=new Point(300,50,false);
- Rectangle r1=new Rectangle(30,20,p2,false);
- HashMap<String,Shape> map=new HashMap<String,Shape>();
- map.put("point", p2);
- map.put("rectangle", r1);
- map.put("Point",p3);
- System.out.println("point from map is:"+map.get("point"));
- Point p11=new Point(50,50,false);
- map.put("point", p11);
- System.out.println("point from map is:"+map.get("point"));
- Circle c3=new Circle(new Point(50,50,false),40,false);
- try {
- c3.setRadius(-150);
- }
- catch(Exception e){
- e.printStackTrace();
- }
- finally{
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement