gpt4 book ai didi

Java 简单图形小程序

转载 作者:行者123 更新时间:2023-12-01 12:04:05 26 4
gpt4 key购买 nike

我正在尝试用 Java 制作一个简单的功能抽屉。我正在使用 ScriptEngine API 从字符串解析方程式,但绘图时速度非常慢。还有另一种方法可以做同样的事情吗?这是代码:

private String v;
@Override
public void init(){
setSize(600,600);
v = JOptionPane.showInputDialog("Input function:");
}
@Override
public void paint(Graphics g){
drawQuadrants(g);
drawEquation(g);
}
private void drawEquation(Graphics g) {
g.setColor(Color.BLUE);
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("JavaScript");
v = v.replace("sin", "Math.sin")
.replace("cos", "Math.cos")
.replace("sen", "Math.sin")
.replace("tan", "Math.tan")
.replace("tg", "Math.tan")
.replace("log", "Math.log")
.replace("Log(x)","(Math.log(x)/Math.LN10)");
for(double x0 = -10;x0<=10;x0+=0.001){
engine.put("x", x0);
try {
double y0 = (Double)engine.eval(v);
drawPoint(g,x0,-y0);
} catch (HeadlessException | ScriptException e) {
e.printStackTrace();
}
}
}
private void drawQuadrants(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(0, 300, 600, 300);
g.drawLine(300, 0, 300, 600);
g.setFont(new Font("Arial",Font.BOLD,15));
g.drawString("x", 580, 320);
g.drawString("y", 280, 20);
for(int l = 0;l<=600;l+=30){
g.drawLine(l, 297, l, 303);
}
for(int l = 0;l<=600;l+=30){
g.drawLine(297, l, 303, l);
}
}
private void drawPoint(Graphics g, double x0, double y0) {
int newx0 = (int)map((float)x0, (float)-10, (float)10, (float)0.0, (float)600.0);
int newy0 = (int)map((float)y0, (float)-10, (float)10, (float)0.0, (float)600.0);
g.drawOval(newx0, newy0, 1, 1);
}
public static final float map(float value, float start1, float stop1, float start2, float stop2)
{
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
}

最佳答案

你可以随时尝试我的代码,它简单快速且优雅。它还可以使用外部解析器绘制任何图形。

    import javax.swing.*;
import java.awt.*;
import java.util.Scanner;
import net.objecthunter.exp4j.*;

class math extends JFrame
{
public static void main(String args[])
{
math m=new math();
m.setVisible(true);
m.setLocationRelativeTo(null);


}

public void paintallies(Graphics G1,double sf)
{int i;
Graphics2D g21=(Graphics2D) G1;
g21.setColor(Color.GREEN);
for(i=0;i<=600;i=(int) (i+sf))
{
g21.drawLine(i,0,i,600);
g21.drawLine(0,i,600,i);

}

}
public void paintaxes(Graphics G1)
{
Graphics2D g21=(Graphics2D) G1;
g21.setColor(Color.BLACK);
g21.drawLine(300,0,300,600);//y axis

g21.drawLine(0,300,600,300); //x axis

}


public void paint(Graphics G)
{
int i;
double j,k;

Scanner s=new Scanner(System.in);
System.out.println("Enter input");
String input=s.nextLine();
System.out.println("Enter scale factor");
double sf=s.nextDouble();
double sff=300/sf;
double kf=sff;
double count=0;






Graphics g2=(Graphics) G;
paintallies(G,sf);
paintaxes(G);

g2.translate(300,300);
do
{
kf=kf-(1/sf);
count++;

}while(kf>=0);
double counts=2*count;


Color c=Color.RED;
g2.setColor(c.darker());

double yarr[]=new double[(int)counts];
double xarr[]=new double[(int)counts];



Expression E=new ExpressionBuilder(input).variables("x").build();


j=-sff; k=-sff;
for(i=0;i<counts;i++)
{

xarr[i]=j;
j=j+(1/sf);


E.setVariable("x",k);
yarr[i]=E.evaluate();
k=k+(1/sf);

xarr[i]=sf*xarr[i];
yarr[i]=-sf*yarr[i];

}

for(i=0;i<counts;i++)
{
if(i==counts-1)
{
break;
}
else
{
g2.drawLine((int)xarr[i],(int)yarr[i],(int)xarr[i+1],(int)yarr[i+1]);

}
}


}






math()
{
super("Grapher");
setSize(600,600);
setResizable(true);



}

}

关于Java 简单图形小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27778854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com