import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mygraph
{
public static void main(String args[])
{
SimpleFrame frame=new SimpleFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class SimpleFrame extends JFrame
{
public SimpleFrame()
{
//设置窗口标题栏的文字
setTitle("My Graph");
//设置窗口的大小
setSize(width,height);
//设置图标
Toolkit tk=Toolkit.getDefaultToolkit();
Image img=tk.getImage("icon.jpg");
setIconImage(img);
//类java.awt.Dimension将组件的宽、高封装在单个对象中
//得到屏幕大小 Java提供了一个ToolKit类 他的getScreenSize方法可以获得屏幕尺寸
Dimension screenSize=tk.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
//框架定位
int x=(screenWidth-width)/2;
int y=(screenHeight-height)/2;
//设置组件的位置
setLocation(x,y);
//设置框架大小可变
setVisible(true);
//创建面板
DrawPanel_1 panel_1=new DrawPanel_1();
Container contentPane=getContentPane();//得到JFrame的内容窗格对象
contentPane.add(panel_1);//将组件加入到内容窗格中
}
public static final int width=800;
public static final int height=700;
}
class DrawPanel_1 extends JPanel implements ActionListener//DrawPanel_1类继承JPanel类并实现ActionListener接口
{
public DrawPanel_1()
{
//创建按钮
MyButton1=new JButton("五边形");
MyButton2=new JButton("五角星");
MyButton3=new JButton("合并");
MyButton4=new JButton("心型线");
MyButton5=new JButton("八边形");
MyButton6=new JButton("半圆");
MyButton7=new JButton("抛物线");
btn1=new JButton("yellow");
btn2=new JButton("桌面颜色");
btn3=new JButton("white");
btn4=new JButton("Exit");
//将按钮加入panel 通过Container类的Add方法经一个组件加入的该容器中
add(MyButton1);
add(MyButton2);
add(MyButton3);
add(MyButton4);
add(MyButton5);
add(MyButton6);
add(MyButton7);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
//注册监听器
MyButton1.addActionListener(this);
MyButton2.addActionListener(this);
MyButton3.addActionListener(this);
MyButton4.addActionListener(this);
MyButton5.addActionListener(this);
MyButton6.addActionListener(this);
MyButton7.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
//注册鼠标监听器
addMouseMotionListener(new MouseMotionHandler());
//获取图像文件
image1=Toolkit.getDefaultToolkit().getImage("icon1.jpg");
image2=Toolkit.getDefaultToolkit().getImage("icon2.gif");
MediaTracker tracker=new MediaTracker(this);
tracker.addImage(image1,1);
tracker.addImage(image2,2);
try
{
tracker.waitForID(1);
}
catch(InterruptedException exception)
{}
}
public void paintComponent(Graphics g) //此方法自动执行 当窗口被重新绘制时 系统会自动调用该方法
{
super.paintComponent(g);
//默认设置背景颜色为白色
if(Buttonout==0)
setBackground(Color.white);
//创建文本框1
Font f=new Font("宋体",Font.BOLD+Font.ITALIC,25);
g.setFont(f);
g.drawString("绘制简单图形",50,100);
//创建文本框2
Font s=new Font("宋体",Font.BOLD,15);
g.setFont(s);
g.drawString("此版权属函远所有 如有雷同 纯属巧合",500,600);
//显示当前鼠标位置
String text="当前鼠标指针位置"+mousex+":"+mousey;
g.drawString(text,10,60);
//得到icon1的尺寸
int imagewidth1=image1.getWidth(this);
int imageheight1=image1.getHeight(this);
//得到icon2的尺寸
int imagewidth2=image2.getWidth(this);
int imageheight2=image2.getHeight(this);
//得到窗口尺寸
int FrameWidth=getWidth();
int FrameHeight=getHeight();
//把图片icon1放到左侧
int xm=20;
int ym=FrameHeight/2;
//把图片icon2放到右侧
int xn=FrameWidth-imagewidth2;
int yn=FrameHeight/4;
//显示图像
g.drawImage(image1,xm,ym,null);
g.drawImage(image2,xn,yn,null);
int width=800;
int height=700;
//画五边形
int x1=width/2;
int y1=height/2;
int radius_1=Math.min(width/3,height/3);
Polygon p_1=new Polygon();
for(int i=0;i<5;i++)
p_1.addPoint( (int)(x1+Math.cos((18+i*72)*Math.PI/180)*radius_1),
(int)(y1-Math.sin((18+i*72)*Math.PI/180)*radius_1)
);
if(Buttonout==1)
{
g.setColor(Color.cyan);
g.drawPolygon(p_1);
}
//五角星 R:内接圆半径 r外接圆半径 r=R*cos(72)
int x2=width/2;
int y2=height/2;
int radius_2=Math.min(width/3,height/3);
Polygon p_2=new Polygon();
int xa[]=new int[5];
int ya[]=new int[5];
int xb[]=new int[5];
int yb[]=new int[5];
for(int i=0;i<5;i++)
{
xa[i]=(int)(x2+Math.cos((18+i*72)*Math.PI/180)*radius_2);
ya[i]=(int)(y2-Math.sin((18+i*72)*Math.PI/180)*radius_2);
}
xb[0]=(int)(x2+Math.tan(36*Math.PI/180)*Math.cos(72*Math.PI/180)*radius_2);
xb[1]=(int)(x2-Math.tan(36*Math.PI/180)*Math.cos(72*Math.PI/180)*radius_2);
xb[2]=(int)(x2-Math.sin(36*Math.PI/180)*Math.cos(72*Math.PI/180)*2*radius_2);
xb[3]=(int)(x2);
xb[4]=(int)(x2+Math.sin(36*Math.PI/180)*Math.cos(72*Math.PI/180)*2*radius_2);
yb[0]=(int)(y2-Math.cos(72*Math.PI/180)*radius_2);
yb[1]=(int)(y2-Math.cos(72*Math.PI/180)*radius_2);
yb[2]=(int)(y2+radius_2/Math.cos(36*Math.PI/180)*Math.cos(72*Math.PI/180)*Math.cos(72*Math.PI/180));
yb[3]=(int)(y2+Math.cos(72*Math.PI/180)/Math.cos(36*Math.PI/180)*radius_2);
yb[4]=(int)(y2+radius_2/Math.cos(36*Math.PI/180)*Math.cos(72*Math.PI/180)*Math.cos(72*Math.PI/180));
for(int i=0;i<5;i++)
{
p_2.addPoint(xa[i],ya[i]);
p_2.addPoint(xb[i],yb[i]);
}
if(Buttonout==2)
{
g.setColor(Color.red);
g.drawPolygon(p_2);
}
//合并
if(Buttonout==3)
{
g.setColor(Color.darkGray);
g.drawPolygon(p_1);
g.drawPolygon(p_2);
}
//心型线 r=a(1+cosθ)
int x3=width/2;
int y3=height/4;
Polygon p=new Polygon();
if(Buttonout==4)
{
for(int a=60;a<=210;a+=30)
{
for(int t=-90;t<270;t++)
p.addPoint((int)(a*(1+Math.cos(t*Math.PI/180-Math.PI/2))*Math.cos(t*Math.PI/180)+x3),(int)(a*(1+Math.cos(t*Math.PI/180-Math.PI/2))*Math.sin(t*Math.PI/180)+y3));
g.setColor(new Color(200,a/2,a));
g.drawPolygon(p);
}
}
//八边形
int x4=width/2;
int y4=height/2;
int radius_4=Math.min(width/3,height/3);
Polygon p_4=new Polygon();
if(Buttonout==5)
{
for(int i=0;i<8;i++)
p_4.addPoint( (int)(x4+Math.cos((22.5+i*45)*Math.PI/180)*radius_4),
(int)(y4-Math.sin((22.5+i*45)*Math.PI/180)*radius_4)
);
g.setColor(Color.black);
g.drawPolygon(p_4);
}
//半圆
int x5a=width/4;
int y5a=height*5/8;
int x5b=width*3/4;
int y5b=height*5/8;
int radius_5=(x5b-x5a)/2;
int startAngle=0;
int arcAngle=180;
if(Buttonout==6)
{
g.setColor(Color.magenta);
//画线