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

📄 tuxing.java

📁 简单的画图程序和添加图片程序 虽人不是很复杂
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class tuxing //主类
{
	public static void main(String[] args)
	{
		ButtonFrame frame=new ButtonFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.show();
	}
}
class ButtonFrame extends JFrame//框架
{
 public	 ButtonFrame()
 {
	 setTitle("画图演示");
	 setSize(WIDTH,HEIGHT);
	 setLocation(400,400);
	 ButtonPanel panel=new ButtonPanel();
	 panel.setBackground(Color.pink);//将背景颜色设为粉红色
	 Container contentpane=getContentPane();//得到内容窗格
	 contentpane.add(panel);//将面板组件加入内容窗格
 }
 public static final int WIDTH=400;
 public static final int HEIGHT=400;
}
class ButtonPanel extends JPanel implements ActionListener//用于绘图的面板组件,同时实现ActionListener接口
{
	public ButtonPanel()
	{
		button1=new JButton("正八边型");//创建新按钮
		button2=new JButton("星型线");
		button3=new JButton("图片");
		add(button1);//将新按钮添加到面板组件中
		add(button2);
		add(button3);
		button1.addActionListener(this);//注册监听器,三个共享同一个监听器对象;
		button2.addActionListener(this);
		button3.addActionListener(this);
	}
	public void paintComponent(Graphics g)
	{
		 super.paintComponent(g);
		 if(ButtonText=="正八边型")//判断是否是事件源button1发生
		 {Polygon p= new Polygon();//构造多边形对象p,可以将点装入p中
		    int  red,green,blue;
			int x1=150;//八边形的中心
			int y1=200;
			int radius=100;//八边形的内径
			red=(int)(Math.random()*255);//随机生成颜色
			green=(int)(Math.random()*255);   
			blue=(int)(Math.random()*255);
			for(int i=0;i<8;i++)//产生八边型的八个顶点,并放入p中
			{ 
			 p.addPoint((int)(x1+radius*Math.cos(i*2*Math.PI/8)),
				        (int)(y1+radius*Math.sin(i*2*Math.PI/8))); 
			 }
			g.setColor(new   Color(red,green,blue));
	        g.drawPolygon(p);
	        g.drawString("您点击的是:"+ButtonText,x1-radius/2,y1);//将文字显示在八边形的中心位置
	      }
		 if(ButtonText=="星型线")//判断是否是事件源button2发生
		 {  int   red,green,blue;
			int radius=100;//星型线函数的常数a,x=a*cos(t)^3 ,  y=a*sin(t)^3,  0<=t<=2*pi
			//将心形线的四条弧分别画出,有画弧的方法画出的对称曲线
			/*red=(int)(Math.random()*255);   
			green=(int)(Math.random()*255);   
			blue=(int)(Math.random()*255);
		    g.setColor(new   Color(red,green,blue));//每画一条弧就随机产生更新一种颜色
		g.drawArc(x1,y1,2*radius,2*radius,-90,90);
		    red=(int)(Math.random()*255);   
		    green=(int)(Math.random()*255);   
		    blue=(int)(Math.random()*255);
		    g.setColor(new   Color(red,green,blue));
		g.drawArc(x1+2*radius,y1,2*radius,2*radius,180,90);
		   red=(int)(Math.random()*255);   
		   green=(int)(Math.random()*255);   
		   blue=(int)(Math.random()*255);
		   g.setColor(new   Color(red,green,blue));
	    g.drawArc(x1,y1+2*radius,2*radius,2*radius,0,90);
	       red=(int)(Math.random()*255);   
		   green=(int)(Math.random()*255);   
		   blue=(int)(Math.random()*255);
		   g.setColor(new   Color(red,green,blue));
		g.drawArc(x1+2*radius,y1+2*radius,2*radius,2*radius,90,90); 
		g.drawString("您点击的是:"+ButtonText,x1+radius,y1);//控制文字输出的位置*/
		int x,y;
		for(float i=0;i<=720;i++)
		   {   //将曲线上每个原点的颜色都随机改变
			red=(int)(Math.random()*255);   
			green=(int)(Math.random()*255);   
		    blue=(int)(Math.random()*255);
		    g.setColor(new   Color(red,green,blue));
			 x=(int)(radius*Math.cos(i/180*Math.PI)*Math.cos(i/180*Math.PI)*Math.cos(i/180*Math.PI));
		    y=(int)(radius*Math.sin(i/180*Math.PI)*Math.sin(i/180*Math.PI)*Math.sin(i/180*Math.PI));
		    g.fillOval(x+200,y+200,2,2);//将原点设在(200,200)处	,也可以是其他 当然不能使曲线超过面板。
		  }
		g.drawString("您点击的是:"+ButtonText,150,99);//控制文字输出的位置
		}
		if(ButtonText=="图片")
		{    //Toolkit tk=Toolkit.getDefaultToolkit();
			Image image=Toolkit.getDefaultToolkit().getImage("tupian1.gif");
			MediaTracker tracker=new MediaTracker(this);
			tracker.addImage(image,1);
			try
			{
			tracker.waitForID(1);	
			}
			catch(InterruptedException exception)
		     {}
			//g.drawString("您点击的是:"+ButtonText,150,99)
			g.drawImage(image,20,20,null);
		}
		}
	
		
	public void actionPerformed(ActionEvent event)//实现接口ActionListener唯一的paintComponent方法
	{
		Object source=event.getSource();//通过EventObject类的getSource方法来得到判断是那个事件源发生
		if(source==button1)//判断那个按钮按下
		ButtonText="正八边型";//相应的操作
		if(source==button2)
		ButtonText="星型线";
		if(source==button3)
		ButtonText="图片";
		repaint();
	}
	private JButton button1;
	private JButton button2;
	private JButton button3;
	public String ButtonText=" ";
	}

⌨️ 快捷键说明

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