erfensousuo.java
来自「二分搜索算法程序源码,动态演示过程.JAVA源码.」· Java 代码 · 共 271 行
JAVA
271 行
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class erfensousuo
{
/**
* Creates a new instance of <code>Newton</code>.
*/
public erfensousuo() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] arg)
{
new newFrame().init();
}
}
class newFrame extends JFrame
{
public void init()
{
MyPanel hello=new MyPanel();
getContentPane().add(hello);
hello.thread.start();
//以下代码设置JFrame窗体的外观
setSize(400,400);
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable
{
Thread thread;
private int xpoints[];
private double ypoints[];
private double a,b;
private double a1 = 50.0;
private double b1 = 100.0;
private double b2=50.0;
private double a2=-50.0;
private double b3=-50;
private double a3=-100;
private double e = 0.001;
private double A1=Math.PI/2;
private double B1=Math.PI;
private double B2=Math.PI/2;
private double A2=-Math.PI/2;
private double B3=-Math.PI/2;
private double A3=-Math.PI;
private double x,xx,z1,z2,z3,fa,fx;
int hh1=0,hh2=0,hh3=0;
public MyPanel()
{
setSize(500, 500);
xpoints=new int[401];
ypoints=new double[401];
thread=new Thread(this);
}
public void run()
{
a=a1;b=b1;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
while(B1-A1>e) //求第一个值
{
x=(A1+B1)/2;
xx=(a1+b1)/2;
fx=2*Math.sin(x)-x;
fa=2*Math.sin(A1)-A1;
if(fx*fa<0)
{
B1=x;
b=b1=xx;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
}
else
{
A1=x;
a=a1=xx;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
}
}
hh1=1;
z1=(A1+B1)/2;
repaint();
try{
thread.sleep(1000);
}
catch(InterruptedException e)
{
}
a=a2;b=b2;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
while(B2-A2>e) //求第二个值
{
x=(A2+B2)/2;
xx=(a2+b2)/2;
fx=2*Math.sin(x)-x;
fa=2*Math.sin(A2)-A2;
if(fx*fa<=0)
{
B2=x;
b=b2=xx;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
}
else
{
A2=x;
a=a2=xx;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
}
}
hh2=1;
z2=(A2+B2)/2;
repaint();
try{
thread.sleep(1000);
}
catch(InterruptedException e)
{
}
a=a3;b=b3;
repaint();
try{
thread.sleep(1000);
}
catch(InterruptedException e)
{
}
while(B3-A3>e) //求第三个值
{
x=(A3+B3)/2;
xx=(a3+b3)/2;
fx=2*Math.sin(x)-x;
fa=2*Math.sin(A3)-A3;
if(fx*fa<0)
{
B3=x;
b=b3=xx;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
}
else
{
A3=x;
a=a3=xx;
repaint();
try{
thread.sleep(1000);
}catch(InterruptedException e)
{
}
}
}
hh3=1;
z3=(A3+B3)/2;
repaint();
}
public void paintComponent(Graphics g)
{
for(int i=0,x=-200;i<=400;i++)
{
xpoints[i]=x;
ypoints[i]=150*Math.sin(Math.PI*xpoints[i]/100);
x++;
}
g.setColor(Color.white);
g.clearRect(0, 0, 400, 440);
//画坐标轴
g.setColor(Color.red);
g.drawLine(0, 200, 400, 200); //x轴
g.drawLine(200, 400, 200, 0); //y轴
g.translate(200,200);
g.setColor(Color.green);
g.drawLine(200,-473,-200,473);
for(int i=0;i<400;i++)
{
g.drawLine(xpoints[i],-(int)ypoints[i],xpoints[i+1],-(int)ypoints[i+1]);
}
g.setColor(Color.black);
g.drawLine((int)a,-200,(int)a,200);
g.drawLine((int)b,-200,(int)b,200);
if(hh1==1)
{
g.drawString("x1="+z1,-190,-180);
}
if(hh2==1)
{
g.drawString("x2="+z2,-190,-160);
}
if(hh3==1)
{
g.drawString("x3="+z3,-190,-140);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?