رسم نمودار به صورت گرافیکی با تعیین طول بازه
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class rasme_nomodar extends JFrame{
private static final long serialVersionUID = 1L;
private JTextArea zz;
private JTextField z,xx;
JLabel m,n;
private JButton btn;
private Boolean boo=false;
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.WHITE);
g.drawLine(400,25,400,597);
g.drawLine(0, 300, 800, 300);
if(boo==true){
for(int i=0;i<=(int)strtodou(z.getText());i++){
String zz="("+xx.getText()+")";
int s=(int) parant(zz,i);
int ss=(int)parant(zz,i+1);
g.setColor(Color.red);
g.drawLine(i*2+401, 301-s,401+(( i+1)*2), 301-ss);
}
for(int i=-(int)strtodou(z.getText());i<=0;i++){
String zz="("+xx.getText()+")";
int s=(int) parant(zz,i);
int ss=(int)parant(zz,i+1);
g.setColor(Color.blue);
g.drawLine(i*2+401, 301-s,401+(( i+1)*2), 301-ss);
}
}
}
public rasme_nomodar(){
this.setTitle("Nomadar");
this.setBounds(100 , 50 , 800 , 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
this.getContentPane().setBackground(Color.BLACK);
this.setResizable(false);
this.setLayout(null);
this.setVisible(true);
zz=new JTextArea();
zz.setText("");
zz.setBounds(5 , 0 , 105 , 20);
zz.setBackground(Color.white);
this.getContentPane().add(zz);
z=new JTextField();
z.setBounds(5,55, 100, 20);
z.setForeground(Color.green);
z.setText("Enter Mahdode");
z.setEnabled(false);
z.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
z.setEnabled(true);
z.setText(null);
}
});
this.getContentPane().add(z);
xx=new JTextField();
xx.setBounds(5,30, 100, 20);
xx.setForeground(Color.red);
xx.setText("Enter X");
xx.setEnabled(false);
xx.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseClicked(MouseEvent arg0) {
xx.setEnabled(true);
xx.setText(null);
}
});
this.getContentPane().add(xx);
btn=new JButton();
btn.setText("DRAW");
btn.setBounds(115, 0, 100, 20);
this.getContentPane().add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
press(e);
}
});
this.addMouseListener(new MouseListener() {
public void mouseReleased(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseClicked(MouseEvent arg0) {
}
});
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
mousemove(e);
}
@Override
public void mouseDragged(MouseEvent arg0) {
}
});
}
public void press(ActionEvent e){
this.repaint();
boo=true;
paint(null);
boolean iscontinue=true;
if (z.getText().equals(""))
{
z.setBackground(Color.red);
iscontinue=false;
}
else
z.setBackground(Color.WHITE);
if(xx.getText().equals("")){
xx.setBackground(Color.red);
iscontinue=false;
}
else
xx.setBackground(Color.WHITE);
if (!iscontinue)
{
JOptionPane.showMessageDialog(null , "Please Enter Red Fields");
return;
}
}
public static void main(String[] args)
{
JFrame.setDefaultLookAndFeelDecorated(true);
new rasme_nomodar();
}
public void mousemove(MouseEvent e){
int i=e.getX()-400;
int ii=300-e.getY();
zz.setText("Moved to ("+i+","+ii+")");
}
public static double strtodou(String e)
{
int i = 0,x = 1, d = 1;
int a = 0;
if(e.charAt(i)=='-')
{
d = -1;
i++;
}
if(e.charAt(i)=='+')
i++;
double r = 0;
while(i<e.length())
{
r = r*10+(e.charAt(i++)-'0');
if(i==e.length())
break;
if(x>1)
x*=10;
if(e.charAt(i)=='.')
{
i++;
x*=10;
}
if(x>1000000)
{
a ++;
break;
}
}
if(a>0)
x/=10;
return (r/(x*1.0))*d;
}//end of strtodou
public int mt=1;
////////////////////////////////////Math/////////////////////////////////////
public double exp(double x){
double s=1;
for(int i=0;i<=1000;i++){
double k=1;
for(int j=1;j<=i;j++){
k*=(x*1.0/j);
}
s+=k;
}
return s;
}
//***********************************//
public double sin(double x){
int a=-1;
x=Math.toRadians(x);
double s=x;
for(int i=3;i<=1000;i+=2){
double k=1;
for(int j=1;j<=i;j++){
k*=(x*1.0/j);
}
s+=k*a;
a*=-1;
}
return s;
}
//***********************************//
public double cos(double x){
int a=-1;
x=Math.toRadians(x);
double s=1;
for(int i=2;i<=1000;i+=2){
double k=1;
for(int j=1;j<=i;j++){
k*=(x*1.0/j);
}
s+=k*a;
a*=-1;
}
return s;
}
//***********************************//
/////////////////////////////////////////////////////////////////////////////
public double toDouble(String s){
double n=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
int p=1;
boolean as = false;
int t=10;
n=s.charAt(i)-'0';
while(i+p<s.length()){
if((s.charAt(i+p)>='0'&&s.charAt(i+p)<='9')){
if(!as){n=n*10+s.charAt(i+p)-'0';}
if(as){n=n+(s.charAt(i+p)-'0')*1.0/t;t*=10;}
p++;
}
else if(s.charAt(i+p)=='.'){
as=true;p++;
}
else break;
}
i+=p-1;
continue;
}
}
if(s.charAt(0)=='-')n=-n;
return n;
}
public double toarr(String s,double x)
{
double a[]=new double[s.length()];
char b[] = new char [s.length()];
int j=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)>='0'&&s.charAt(i)<='9'){
int p=1;
boolean as = false;
boolean na = false;
int t=10;
double n=s.charAt(i)-'0';
if(i>0)
if(s.charAt(i-1)=='-'){
if(i-2>0){
if(!(s.charAt(i-2)>='0'&&s.charAt(i-2)<='9')&&!(s.charAt(i-2)=='x')){na=true;}
}
else if(i-2==-1)
na=true;
}
while(i+p<s.length()){
if((s.charAt(i+p)>='0'&&s.charAt(i+p)<='9')){
if(!as)n=n*10+s.charAt(i+p)-'0';
if(as){n=n+(s.charAt(i+p)-'0')*1.0/t;t*=10;}
p++;
}
else if(s.charAt(i+p)=='.'){
as=true;p++;
}
else break;
}
i+=p-1;
a[j]=n;
b[j]='_';
if(na){a[j]*=-1;shifta(a,j-1,1);shiftb(b,j-1,1);j--;}
j++;
continue;
}
if(s.charAt(i)=='+'){
b[j++]='+';
continue;
}
if(s.charAt(i)=='x'){
b[j++]='x';
continue;
}
if(s.charAt(i)=='^'){
b[j++]='^';
continue;
}
if(s.charAt(i)=='-'){
b[j++]='-';
continue;
}
if(s.charAt(i)=='*'){
b[j++]='*';
continue;
}
if(s.charAt(i)=='/'){
b[j++]='/';
continue;
}
if(s.charAt(i)=='s'){
if(i+2<s.length())
if(s.charAt(i+1)=='i'&&s.charAt(i+2)=='n')
{b[j++]='s';i+=2;continue;}
}
if(s.charAt(i)=='c'){
if(i+2<s.length())
if(s.charAt(i+1)=='o'&&s.charAt(i+2)=='s')
{b[j++]='c';i+=2;continue;}
}
if(s.charAt(i)=='t'){
if(i+2<s.length())
if(s.charAt(i+1)=='a'&&s.charAt(i+2)=='n')
{b[j++]='t';i+=2;continue;}
}
if(s.charAt(i)=='c'){
if(i+2<s.length())
if(s.charAt(i+1)=='o'&&s.charAt(i+2)=='t')
{b[j++]='g';i+=2;continue;}
}
if(s.charAt(i)=='l'){
if(i+2<s.length())
if(s.charAt(i+1)=='o'&&s.charAt(i+2)=='g')
{b[j++]='l';i+=2;continue;}
}
if(s.charAt(i)=='s'){
if(i+2<s.length())
if(s.charAt(i+1)=='q'&&s.charAt(i+2)=='r')
{b[j++]='j';i+=2;continue;}
}
if(s.charAt(i)=='e'){
if(i+2<s.length())
if(s.charAt(i+1)=='x'&&s.charAt(i+2)=='p')
{b[j++]='e';i+=2;continue;}
}
}
double r=calc(j,a,b,x);
return r;
}
public void shifta(double a[] ,int p,int l){
for(int i=p;i<a.length-l;i++)
{
if(i+l>=a.length)break;
a[i]=a[i+l];
}
}
public void shiftb(char b[],int p,int l){
for(int i=p;i<b.length-l;i++)
{
if(i+l>=b.length)break;
b[i]=b[i+l];
}
}
public double calc(int j,double a[],char b[],double x){
for(int i=0;i<b.length;i++)
if(b[i]=='x'){
b[i]='_';
a[i]=x;
}
for(int i=0;i<b.length;i++)
if(b[i]=='^'){
if(a[i+1]%2==0&&mt%2==1)
mt=(int)a[i+1];
}
while(j>1){
for(int i=0;i<j-1;i++){
if(b[i]=='^'){
double t=a[i-1];
for(int ii=2;ii<=a[i+1];ii++)a[i-1]*=t;
shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
}
}
for(int i=0;i<j;i++){
if(b[i]=='s'){
a[i]=sin(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
}
if(b[i]=='c'){
a[i]=cos(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
}
if(b[i]=='t'){
a[i+1]=Math.toRadians(a[i+1]);
a[i]=Math.tan(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
}
if(b[i]=='l'){
a[i]=Math.log(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
}
if(b[i]=='e'){
a[i]=exp(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
}
if(b[i]=='j'){
a[i]=Math.sqrt(a[i+1]);shifta(a,i+1,1);shiftb(b,i,1);j--;continue;
}
}
for(int i=0;i<j-1;i++){
if(b[i]=='*'){
a[i-1]=a[i-1]*a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
}
if(b[i]=='/'){
a[i-1]=a[i-1]/a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
}
}
for(int i=0;i<j-1;i++){
if(b[i]=='+'){
a[i-1]=a[i-1]+a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
}
if(b[i]=='-'){
a[i-1]=a[i-1]-a[i+1];shifta(a,i,2);shiftb(b,i,2);j-=2;continue;
}
}
}
return a[0];
}
public double parant(String s,double x){
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)=='('){
int j=1;
String sp ="";
while(s.charAt(i+j)!=')'){
sp=sp+s.charAt(i+j);
j++;
}
double k=toarr(sp,x);
String ss="";
for(int ii=0;ii<i;ii++) ss=ss+s.charAt(ii);
ss=ss+k;
for(int ii=i+j+1;ii<s.length();ii++) ss=ss+s.charAt(ii);
s=ss;
i=s.length()-1;
}
}
double sn=toDouble(s);
return sn;
}
public void Find(String s){
System.out.println("Te'dad Risheha :"+mt);
double a=0,aa=0;
double b=0,bb=0;
double k=1,kk=1;
double i=0,ii=0;
boolean d=false;
int n=0;
while(true){
a=parant(s,i);
aa=parant(s,0);
b=parant(s,ii);
bb=parant(s,0);
if(a==0){System.out.println("Rishe: "+i);d=true;n++;}
if(b==0&&i!=0){System.out.println("Rishe: "+ii);d=true;n++;}
if(mt%2==1&&d)break;
else if(n==mt)break;
if(a*aa<0){System.out.println("Rishe: ["+0+","+i+"]");k=k/10;i-=(k*10);}
if(bb*b<0){System.out.println("Rishe: ["+ii+","+0+"]");kk=kk/10;ii+=(kk*10);}
i+=k;
ii-=kk;
}
}
}