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

📄 huangjin.txt

📁 黄金分割算法
💻 TXT
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class demo2 
{
   public static void main(String[] arg)
   {
      new sinTest().init();
   }
}

class sinTest extends JFrame
{
    public void init()  
    {
        Container con=getContentPane();
		con.add(new MyPanel());
		setBounds(350,250,350,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

class MyPanel extends JPanel implements Runnable
{
	
		double a,b,e;
		double x1,x2;
		double f1,f2;
	public MyPanel() 
    {
        setSize(500, 500);
		new Thread(this).start();
	}

	public void run()  
    {
		a=-(3.14/2);
		b=(3.14/2);
		e=0.0000001;
		x1=a+(b-a)*0.382;
		x2=a+(b-a)*0.618;
		f1=Math.cos(x1);
		f2=Math.cos(x2);
		while (b-a>e)
		{
			if (f2>f1)
			{
				a = x1;
                x1 = x2;
                f1 = f2;
                x2 =a+b-x1;
                f2 = Math.cos(x2);
			}
			else
			{
				b = x2;
                x2 = x1;
                f2 = f1;
                x1 =a+b-x2;
                f1 = Math.cos(x1);
			}
			repaint();
			try
			{
				Thread.sleep(1000);
			}
			catch (Exception f)
			{
			}
		}
	}
	
	public void paintComponent(Graphics g)    
    {
		//清屏
		//g.setColor(Color.white);              
        //g.clearRect(0, 0, 200, 200);  


		//画坐标轴
        g.setColor(Color.red);     
        g.drawLine(0, 150,350,150);   //x轴
        g.drawLine(175,0,175,300);  //y轴


        g.setColor(Color.green);     
        for(int i = -50;i<=50;i++)
        {
			g.drawLine(i+175,(int)((-Math.cos(i*3.14/100))*50+150),i+175,(int)(-(Math.cos(i*3.14/100))*50+150));
        }

		g.setColor(Color.black);
		g.drawLine((int)((x1*100/3.14)+175),150,(int)((x1*100/3.14)+175),(int)(-(Math.cos(x1))*50+150));
		for (int i=-50;i<=x1*100/3.14;i++)
		{
			g.drawLine(i+175,(int)((-Math.cos(i*3.14/100))*50+150),i+175,(int)(-(Math.cos(i*3.14/100))*50+150));
		}
		g.drawLine((int)((x2*100/3.14)+175),150,(int)((x2*100/3.14)+175),(int)(-(Math.cos(x2))*50+150));
		for (int i=50;i>x2*100/3.14;i--)
		{
			g.drawLine(i+175,(int)((-Math.cos(i*3.14/100))*50+150),i+175,(int)(-(Math.cos(i*3.14/100))*50+150));
		}
	}
}

⌨️ 快捷键说明

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