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

📄 testmydraw.java

📁 金旭亮的java教案
💻 JAVA
字号:

// Java core packages
import java.awt.*;
import java.awt.event.*;
   
// Java extension packages
import javax.swing.*;

public class TestMyDraw extends JFrame implements ActionListener
{
	
	static private MyShape shapes[]; //形状引用变量数组
	private Container container;
	private JButton okDraw;
	private JButton clearScreen;
	
	static final int LEFT=50;
	static final int TOP=450;
	static final int WIDTH=400;
	static final int HEIGHT=500;
	public TestMyDraw()
	{
		super("我的绘图程序");
		// set up GUI
        container = getContentPane();
        container.setLayout( new FlowLayout() );
		
		okDraw=new JButton("开始绘图");
		okDraw.addActionListener(this);
		container.add(okDraw);
		
		clearScreen=new JButton("清除屏幕");
		clearScreen.addActionListener(this);
		container.add(clearScreen);
		
       	//给窗口增关闭功能
		WindowListener w=new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		};
		this.addWindowListener(w);
		
	
	}
	public void paint(Graphics g)
	{
		super.paint(g);

	}
	public void actionPerformed(ActionEvent e)
	{
		
		if(e.getSource()==clearScreen)
		{
			repaint();
			return;
		}
		
		shapes=new MyShape[5];
		int left,top,right,buttom;
		
		left=(int)Math.round(Math.random()*WIDTH);
		top=(int)Math.round(Math.random()*HEIGHT);
		right=(int)Math.round(Math.random()*WIDTH);
		buttom=(int)Math.round(Math.random()*HEIGHT);
		
		MyLine line=new MyLine(left,top,right,buttom);
		shapes[0]=line;
		
		left=(int)Math.round(Math.random()*WIDTH);
		top=(int)Math.round(Math.random()*HEIGHT);
		right=(int)Math.round(Math.random()*WIDTH);
		buttom=(int)Math.round(Math.random()*HEIGHT);
		
		MyOval oval=new MyOval(left,top,right,buttom);
		shapes[1]=oval;
		
		left=(int)Math.round(Math.random()*WIDTH);
		top=(int)Math.round(Math.random()*HEIGHT);
		right=(int)Math.round(Math.random()*WIDTH);
		buttom=(int)Math.round(Math.random()*HEIGHT);
		
		MyRect rect=new MyRect(left,top,right,buttom);
		shapes[2]=rect;

		left=(int)Math.round(Math.random()*WIDTH);
		top=(int)Math.round(Math.random()*HEIGHT);
		right=(int)Math.round(Math.random()*WIDTH);
		buttom=(int)Math.round(Math.random()*HEIGHT);
		
		MyOval oval1=new MyOval(left,top,right,buttom);
		shapes[3]=oval1;
		
		left=(int)Math.round(Math.random()*WIDTH);
		top=(int)Math.round(Math.random()*HEIGHT);
		right=(int)Math.round(Math.random()*WIDTH);
		buttom=(int)Math.round(Math.random()*HEIGHT);
		
		MyString str1=new MyString(left,top,right,buttom);
		str1.str="你好,这是多态的妙用";
		
		shapes[4]=str1;		
		
		int i,len;
		len=shapes.length;
		if(len!=0)
		{
			for(i=0;i<len;i++)
				shapes[i].Draw(this.getGraphics());
			
		}
				
	}
	
	public static void main( String args[] )
	{
		TestMyDraw window=new TestMyDraw();
		
		window.setSize(WIDTH,HEIGHT);
		window.show();
	
	}

}

⌨️ 快捷键说明

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