📄 graph.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.Color;
import javax.swing.JComponent;
public class Graph extends JFrame implements ActionListener{
JButton shrink=null;
double minX,minY,maxX,maxY;
double scaleX,scaleY;
Cluster[] clusters;
Color[] colors={Color.BLACK,Color.MAGENTA,Color.RED,Color.BLUE,Color.CYAN,Color.PINK,Color.GREEN,Color.ORANGE,Color.YELLOW};
public Graph(Cluster[] clusts){
super("Graph");
clusters=clusts;
shrink=new JButton("Shrink The Representative Points");
Container c = getContentPane();
c.setLayout(new BorderLayout());
{ JPanel jp=new JPanel(); jp.setLayout(new FlowLayout(FlowLayout.RIGHT));
jp.add(shrink); c.add(jp,BorderLayout.SOUTH);
}
//c.add(shrink);
calMinMaxPoints();
shrink.addActionListener(this);
setSize(1025,750);
setVisible(true);
}
public void paint(Graphics g){
System.out.println("paint called..");
validate();
g.translate(50,50);
setBackground(Color.white);
g.setColor(Color.RED);
g.drawLine(0,0,950,0);
g.drawLine(0,0,0,650);
drawBoundaries(g);
g.setColor(Color.BLACK);
plotPoints(g);
// g.drawOval(100,100,3,3);
// g.drawOval(105,100,3,3);
}
public void drawBoundaries(Graphics g){
double i=0;int j=0;
g.setColor(Color.blue);
// points for X-axis
for(i=0,j=0; i<=950+scaleX; i+=scaleX,j++){
g.drawLine((int)i,0,(int)i,3);
g.drawString(""+((int)Math.floor(minX)+j),(int)(i-10),-5);
}
// points for Y-axis
for(i=0,j=0; i<=650; i+=scaleY,j++){
g.drawLine(0,(int)i,3,(int)i);
g.drawString(""+((int)Math.floor(minY)+j),-25,(int)i);
}
}
public void calMinMaxPoints(){
int i=0,j=0;
double x,y;
for(i=0;i<clusters.length;i++)
for(j=0;j<clusters[i].getClusterSize();j++){
DataPoint dp=clusters[i].getDataPoint(j);
x=dp.getX();
y=dp.getY();
if( i==0 && j==0){
minX=maxX=x;
minY=maxY=y;
}
else{
if(minX>x) minX=x;
if(maxX<x) maxX=x;
if(minY>y) minY=y;
if(maxY<y) maxY=y;
}
}
System.out.println("\nminX is "+minX+" minY is "+minY+" maxX is "+maxX+" maxY is "+maxY);
minX=Math.floor(minX/10)*10; //((int)(minX/10)-1)*10;
minY=Math.floor(minY/10)*10; //((int)(minY/10)-1)*10;
maxX=Math.ceil(maxX/10)*10; //((int)(maxX/10)+1)*10;
maxY=Math.ceil(maxY/10)*10; //((int)(maxY/10)+1)*10;
if(minX<0) minX=0;
if(maxX<0) maxX=0;
scaleX=((950.0)/(maxX-minX));
scaleY=((650.0)/(maxY-minY));
System.out.println("\nminX is "+minX+" minY is "+minY+" maxX is "+maxX+" maxY is "+maxY+"scalX is: "+scaleX+" scaleY is "+scaleY);
}
public void plotPoints(Graphics g){
int i,j;
int x,y;
// plot the cluster points
for(i=0; i<clusters.length; i++){
g.setColor(colors[i]);
for(j=0; j<clusters[i].getClusterSize(); j++){
DataPoint dp=clusters[i].getDataPoint(j);
x=(int)dp.getX();
y=(int)dp.getY();
x=(int)((x-minX)*scaleX);
y=(int)((y-minY)*scaleY);
g.drawOval(x,y,5,5);
}
}
// plot the Representative Points
for(i=0; i<clusters.length; i++){
g.setColor(colors[i]);
for(j=0; j<clusters[i].getNoOfRepPoints(); j++){
DataPoint dp=clusters[i].getRepPoint(j);
System.out.println("("+dp.getX()+","+dp.getY()+")");
x=(int)dp.getX();
y=(int)dp.getY();
x=(int)((x-minX)*scaleX);
y=(int)((y-minY)*scaleY);
g.drawOval(x,y,0,0);
g.drawOval(x,y,1,1);
g.drawOval(x,y,2,2);
g.drawOval(x,y,3,3);
g.drawOval(x,y,4,4);
}
}
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==shrink){
String s=JOptionPane.showInputDialog(null, "Enter the Shrinkage Value (<1)","Enter the Shrinkage Value",JOptionPane.INFORMATION_MESSAGE);
double d=Double.parseDouble(s);
int i,j;
int x,y,x1,y1;
Graphics g=getGraphics();
// plot the Representative Points
for(i=0; i<clusters.length; i++){
g.setColor(colors[i]);
for(j=0; j<clusters[i].getNoOfRepPoints(); j++){
DataPoint dp=clusters[i].getRepPoint(j);
DataPoint dp1=clusters[i].getMean();
x=(int) dp.getX();
y=(int) dp.getY();
double meanx=dp1.getX();
double meany=dp1.getY();
System.out.println("("+x+","+y+")\t mean is"+"("+meanx+","+meany+")");
double dist;
double m;
/* dist=Math.sqrt(((meanx-x)*(meanx-x))+((meany-y)*(meany-y)));
dist=dist*((double)(1-d));
m=((meany-y)/(meanx-x));
double theta=Math.atan(m);
System.out.print("\nd is: "+d+"Before shrink: x is :"+x+" y is: "+y+"\t");
x1=(int)(meanx+(dist*Math.cos(Math.toRadians(theta))));
y1=(int)(meany+(dist*Math.sin(Math.toRadians(theta))));*/
double med[]=new double[2];
double rep[]=new double[2];
med[0]=meanx;
med[1]=meany;
rep[0]=x;
rep[1]=y;
double shrinkPoint[]=shrink1.shrink1(med,rep,(1-d));
System.out.println("Shrink Point x is"+shrinkPoint[0]+"Shrink Point Y is"+shrinkPoint[0]);
x1=(int)shrinkPoint[0];
y1=(int)shrinkPoint[1];
System.out.print("Before scale: x is :"+x+" y is: "+y+" ");
x1=(int)((x1-minX)*scaleX);
y1=(int)((y1-minY)*scaleY);
System.out.println("After: x is :"+x1+" y is: "+y1);
g.setColor(Color.white);
g.drawOval(x,y,0,0);
g.drawOval(x,y,1,1);
g.drawOval(x,y,2,2);
g.drawOval(x,y,3,3);
g.drawOval(x,y,4,4);
g.setColor(colors[i]);
g.drawOval(x1,y1,0,0);
g.drawOval(x1,y1,1,1);
g.drawOval(x1,y1,2,2);
g.drawOval(x1,y1,3,3);
g.drawOval(x1,y1,4,4);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -