⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mycircle.java

📁 java实现pplive
💻 JAVA
字号:
package com.moveShape;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;

public class MyCircle implements Shape 
{
	
	private String name;
	//the center of the circle
	private Point center;
	private Point startPoint;
	private Point ULpoint,LRpoint;
	
	private int radius;
	
	private Color color;
	private String colorName;
	private boolean state;
	
	private Point mouseSource;
	
//	registered observers
	private ArrayList<MyObserver> myObservers; 
	
	public MyCircle()
	{
		this(new Point(200,150),80);
	}
	
	public MyCircle(Point center, int radius)
	{
		startPoint=new Point();
		startPoint.x=center.x-radius;
		startPoint.y=center.y-radius;
		this.center=center;
		this.radius=radius;
		this.ULpoint=new Point();
		this.ULpoint.x=startPoint.x;
		this.ULpoint.y=startPoint.y;
		this.LRpoint=new Point();
		this.LRpoint.x=this.ULpoint.x+radius*2;
		this.LRpoint.y=this.ULpoint.y+radius*2;
		this.state=false;
		this.color=Color.yellow;
		this.colorName="yellow";
		this.myObservers=new ArrayList<MyObserver>();
		this.name="Rectangle";
	}
	
	
	public void setColor(Color color,String colorName)
	{
		this.color=color;
		this.colorName=colorName;
	}
	
	public void draw(Graphics g) 
	{
		g.setColor(color);
		g.fillOval(startPoint.x,startPoint.y, radius*2, radius*2);
		
		
		
		
	}

	public boolean isInside(Point p) 
	{
		double xdistance=Math.pow((center.getX()-p.getX()),2.0);
		double ydistance=Math.pow((center.getY()-p.getY()),2.0);
		
		double distance=Math.sqrt(xdistance+ydistance);
		
		if(distance<=(double)radius)
		{
			
			
			state=true;
		}
		else
		{
			state=false;
		}
		
		mouseSource=p;
		return state;
	}

	public void move(Point mouseDestination) 
	{
		if(state==true)
		{
			
			Point destination=new Point();
			
			destination.x=startPoint.x+(mouseDestination.x-mouseSource.x);
			destination.y=startPoint.y+(mouseDestination.y-mouseSource.y);
			
			this.startPoint=destination;
			
			center.x=startPoint.x+radius;
			center.y=startPoint.y+radius;
			
			this.mouseSource=mouseDestination;
			
		}
		
	}

	public void changeColor(Color color,String colorName) 
	{

		if(state==true)
		{
			
			this.color=color;
			this.colorName=colorName;
			for(MyObserver observer: myObservers)
			{
				observer.update(color);
			}
		}
		
	}

	public void registerObserver(MyObserver observer) 
	{
		myObservers.add(observer);
		
	}
	
	public void deleteObserver(MyObserver observer)
	{
		myObservers.remove(observer);
	}

	
	public void save(String fileName,int num)
	{
		try
		{
			FileOutputStream out=new FileOutputStream(new File(fileName),true);
			String name = "name"+num+" = circle\n";
			String pointX="Point_x"+num+" = "+center.x+"\n";
			String pointY="Point_y"+num+" = "+center.y+"\n";
			String radius="Radius"+num+" = "+this.radius+"\n";
			String color="Color"+num+" = "+this.colorName+"\n";
			out.write(name.getBytes());
			out.write(pointX.getBytes());
			out.write(pointY.getBytes());
			out.write(radius.getBytes());
			out.write(color.getBytes());
			out.flush();
			out.close();
			
		}catch(IOException e)
		{
			
		}
		
		
		
	}
	
	public void setState(boolean state)
	{
		this.state=state;
	}

	public void contain(Shape shape)
	{
		// TODO Auto-generated method stub
		
	}

	public ArrayList<Shape> out()
	{
		// TODO Auto-generated method stub
		return null;
	}

	public String getDescrption()
	{
		// TODO Auto-generated method stub
		return name;
	}

	public Point getLRpoint()
	{
		// TODO Auto-generated method stub
		return this.LRpoint;
	}

	public Point getULpoint()
	{
		// TODO Auto-generated method stub
		return this.ULpoint;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -