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

📄 adapt.java

📁 数值方法上机实验内容. 自适应算法用图象实现效果.
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.lang.*;

class Page1 extends Frame
{
	Button enter;
	TextArea showArea;
	
	public Page1()
	{
		super("自适应技术");
		setSize(800,600);
		setLocation(0,0);
		setResizable(false);
		Color s=new Color(114,194,244);
        setBackground(s);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});
		
		Panel p2=new Panel();
		enter=new Button("enter");
		p2.add(enter,"Center");
		add(p2,"South");
		enter.addActionListener(new Adapt());
	}
	
    public void paint(Graphics g)
	{	
		Color s=Color.blue.darker().darker();
		g.setColor(s);
		g.setFont(new Font("M",2,64));
		g.drawString("     自 适 应 技 术",80,216);
		g.setFont(new Font("M",2,34));
		g.drawString("by:陈雪姗",380+50,416);
		
	}  				
}

public class Adapt extends Frame implements ActionListener
{
	static Page1 page1;
	Button clear,_Sin,adapt1,adapt2;	
	float x;                
	int M=1000;                //scale
	float Nx=100;
	float Ny=200;
	float y[]=new float[800*M];
	TextField show;
	int drawFlag=-1;
	int item=0;
	
	public Adapt()
	{
		super("自适应技术");
		setSize(900,600);
		setLocation(0,0);
		setResizable(false);

		Color s=new Color(114,194,244);
		setBackground(s);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
		});

		Panel p1=new Panel();
		
		show=new TextField("",16);
		show.setEditable(false);	

        clear=new Button("清除");

		_Sin=new Button("SinX/X曲线");
		adapt1=new Button("自适应技术");
		
		adapt2=new Button("改进的自适应");
			
		p1.add(clear);
		
		p1.add(_Sin);
		p1.add(new Label("         "));
		
		
		p1.add(adapt1);
		
		p1.add(adapt2);
		p1.add(new Label("         "));
		p1.add(show);
		
		p1.setBackground(Color.white);	
		add(p1,"North");
		
		
		adapt1.addActionListener(this);
		adapt2.addActionListener(this);
        clear.addActionListener(this);
		
		_Sin.addActionListener(this);
		
	}
	float f(double i)
	{
		return (float)(-1*Ny*(Math.sin((i-100)/Nx)/((i-100)/Nx))+400.0);
		
	}
	
	public void paint(Graphics g)
	{
		if(drawFlag==-1)
		{
			g.clearRect(0,0,900,700);
			g.setColor(Color.black);
			g.fillOval(95,395,10,10);
			g.drawLine(100-50,400,800,400);
			g.drawLine(100,600,100,100);
			g.setFont(new Font("M",1,24));
			g.drawString("/\\",100-7,105);   
			g.drawString(">",800,408);  g.drawString("X",800,405+20);
			g.drawString("Y",100+10,105);
			g.drawString("0",100-2,415+5); g.drawLine(100,400,100,395);//x=0
            g.drawString("PI",100+314-2,415+5); g.drawLine(100+314,400,100+314,395);//x=90
			g.drawString("2PI",100+628-2,415+5); g.drawLine(728,400,728,395);//x=180
					
		}
		else if(drawFlag==1)
		{   
			
            float a=100,b=728;
			float x1,x2,f1,f2;
			double e=3;
			show.setBackground(Color.yellow);
			show.setText("画SinX/X曲线中...");   		
			for(int i=100*M;i<728*M;i++)
			{
				g.setColor(Color.black);
				y[i]=(float)(-1*Ny*(Math.sin((i-100*M)/M/Nx)/((i-100*M)/M/Nx))+400.0);
				
				
				g.fillOval(Math.round(i/M),Math.round(y[i]),3,3);
			}
            show.setBackground(Color.yellow.darker());
			show.setText("画完!");   
		}
		else if(drawFlag==2)
		{   
			int a=100,b=728,n=1,e=500;
			double t0,t1,h;
			show.setBackground(Color.yellow);
			show.setText("自适应积分进行中...");   
			g.setColor(Color.green.darker());
			g.drawLine(a,400,a,Math.round(f(a+1)));
			g.drawLine(b,400,b,Math.round(f(b)));

			h=b-a;
			t0=h/2*(f(a+1)+f(b));
			t1=t0/2+h/2*f(a+h/2);
			
			while(Math.abs(t1-t0)>e)
			{   
				h=h/2;
				n=2*n;
				for(int i=1;i<n;i++)
				{
					float j;
					j=(float)(a+h*i);
					g.drawLine(Math.round(j),400,Math.round(j),Math.round(f(j)));
				}
				t0=t1;
				t1=0;
				for(int k=0;k<n;k++) t1=t1+f((a+(k-0.5)*h));
				t1=1/2*t0+h/2*t1;
				try{ Thread.sleep(800);}
				catch(Exception ex) { }
			}
		
            
             show.setBackground(Color.yellow.darker());
			 show.setText("自适应积分完毕!");
		}
		else if(drawFlag==3)
		
		{
			int a=100+1,b=728,I=0;
			double t2,t1,h,u,v,w,S=0,e=0.1*100;
			double St[]=new double[1000];
			show.setBackground(Color.yellow);
			show.setText("改进自适应进行中...");   
			g.setColor(Color.orange.darker());
			g.drawLine(a,400,a,Math.round(f(a+1)));
			g.drawLine(b,400,b,Math.round(f(b)));

			e=e/(b-a);
			u=a;
			v=b;
			
			h=v-u;
			t1=(f(u)+f(v))*h/2;
		    w=(u+v)/2;
		    t2=t1/2+f(w)*h/2;
			
			while(true)
			{
				
				g.drawLine((int)Math.round(w),400,(int)Math.round(w),Math.round(f(w)));
                try{ Thread.sleep(20);}
				catch(Exception ex) { }
				
				if(Math.abs(t2-t1)<h*e)  
					{
					  S=S+t2;
				      if(I==0) break;
				      else {
					        u=v;
					        v=St[I];I--;	
				           }
				    }
				else
				{
					I++;
				    St[I]=v;
                    v=w; 	
				   
				}
                h=v-u;
			    t1=(f(u)+f(v))*h/2;
		        w=(u+v)/2;
		        t2=t1/2+f(w)*h/2;
			
			}

			
           show.setBackground(Color.yellow.darker());
		   show.setText("改进自适应积分完毕!");
		    	 
		}
		
		
	}
	
	public void update(Graphics g)
	{
		paint(g);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==page1.enter)
		{
			new Adapt().setVisible(true);
			page1.setVisible(false);
		}
		if(e.getSource()==clear)
		{
			drawFlag=-1;
			repaint();
		}
		else if(e.getSource()==_Sin)
		{
			drawFlag=1;
			repaint();
		}
		else if(e.getSource()==adapt1)
		{
			drawFlag=2;
			repaint();
		}
		else if(e.getSource()==adapt2)
		{
			drawFlag=3;
			repaint();
		}
		
		
		
	

	}
	
	public static void main(String arg[])
	{
		page1=new Page1();
		page1.setVisible(true);
		
	}
	public void mouseDragged(MouseEvent e) {}
	
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {}
	public void mouseReleased(MouseEvent e) {}
}

⌨️ 快捷键说明

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