📄 graph.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class graph extends JFrame implements ActionListener
{
private Container cp;
private JButton draw,cancel,run,back,forward,remove,exit;JLabel lbl;
private JTextField tfx1,tfx2;JTable table;
private ScrollPane pn;JScrollPane pTable;draw d;
private JComboBox coFunc,coHistory;
private textAlysis ts;
private String[][] strCoff={{"Coefficient A",""},{"Coefficient B",""},
{"Coefficient C",""},{"Coefficient D",""},
{"Coefficient E",""}};
private String tableTitle[]={"Coefficient","Value"};
private int numberFunc=1,numbers=2,intHistory=0;
private Vector history=new Vector();
public graph()
{
super("Draw graph");
cp=getContentPane();cp.setBackground(Color.darkGray);
cp.setLayout(null);
lbl=new JLabel("Draw Graphs");
lbl.setBounds(100,2,250,50);
lbl.setFont(new Font("TimesRoman",Font.BOLD,30));
lbl.setForeground(new Color(14,215,125));cp.add(lbl);
lbl=new JLabel("nhudinhthuan@yahoo.com");
lbl.setBounds(10,70,250,20);
lbl.setFont(new Font("TimesRoman",Font.BOLD,18));
lbl.setForeground(new Color(14,177,233));cp.add(lbl);
lbl=new JLabel("Enter underbound(x1):");
lbl.setBounds(20,320,130,20);
lbl.setForeground(new Color(145,255,154));cp.add(lbl);
tfx1=new JTextField();tfx1.setText("0");
tfx1.setBounds(150,320,50,20);cp.add(tfx1);
lbl=new JLabel("Enter upperbound(x2):");
lbl.setBounds(20,350,130,20);
lbl.setForeground(new Color(145,255,154));cp.add(lbl);
tfx2=new JTextField();tfx2.setText("0");
tfx2.setBounds(150,350,50,20);cp.add(tfx2);
draw=new JButton("Draw Graph");
draw.addActionListener(this);
draw.setBackground(new Color(127,122,213));
draw.setForeground(Color.green);
draw.setBounds(300,500,120,20);cp.add(draw);
cancel=new JButton("Cancel");
cancel.addActionListener(this);
cancel.setBackground(new Color(127,122,213));
cancel.setForeground(Color.green);
cancel.setBounds(430,500,120,20);cp.add(cancel);
run=new JButton("Stop Text");
run.addActionListener(this);
run.setBackground(new Color(127,122,213));
run.setForeground(Color.green);
run.setBounds(560,500,120,20);cp.add(run);
back=new JButton("Back");
back.addActionListener(this);
back.setBackground(new Color(127,122,213));
back.setForeground(Color.green);
back.setBounds(20,450,100,20);cp.add(back);
forward=new JButton("Forward");
forward.addActionListener(this);
forward.setBackground(new Color(127,122,213));
forward.setForeground(Color.green);
forward.setBounds(130,450,100,20);cp.add(forward);
remove=new JButton("Remove");
remove.addActionListener(this);
remove.setBackground(new Color(127,122,213));
remove.setForeground(Color.green);
remove.setBounds(20,480,100,20);cp.add(remove);
exit=new JButton("Exit");
exit.addActionListener(this);
exit.setBackground(new Color(127,122,213));
exit.setForeground(Color.green);
exit.setBounds(130,480,100,20);cp.add(exit);
pn=new ScrollPane();
pn.setBounds(250,110,530,370);
d=new draw();
pn.add(d);cp.add(pn);
lbl=new JLabel("Enter Coefficient:");
lbl.setBounds(10,180,250,20);
lbl.setForeground(new Color(177,125,225));cp.add(lbl);
pTable=new JScrollPane();
pTable.setBounds(30,200,200,100);cp.add(pTable);
ts=new textAlysis();
ts.setBounds(330,10,420,85);cp.add(ts);
createCoff(2);createFunction();createCoHistory();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt){System.exit(0);}
});
}
public void actionPerformed(ActionEvent evt)
{
String str=evt.getActionCommand();
if(evt.getSource()==draw){paintGraph();}
if(evt.getSource()==cancel) {cancel();}
if(str.equals("Stop Text"))
{run.setText("Run Text"); ts.stop(); }
if(str.equals("Run Text"))
{ts.setBackground(Color.black);
run.setText("Stop Text");ts.start();}
if(evt.getSource()==back){back(true);}
if(evt.getSource()==forward){forward();}
if(evt.getSource()==remove){remove();}
if(evt.getSource()==exit){System.exit(0);}
}
//tao danh sach cac he so can nhap
public void createCoff(int numberCurrent)
{
this.numbers=numberCurrent;
String currentCoff[][]=new String[numbers][2];
for(int i=0;i<numbers;i++)
{
for(int j=0;j<2;j++)
{currentCoff[i][j]=strCoff[i][j];}
}
table=new JTable(currentCoff,tableTitle);
table.setForeground(new Color(0,36,92));
table.setBackground(new Color(227,255,243));
table.setSelectionBackground(Color.darkGray);
table.setSelectionForeground(Color.green);
pTable.setViewportView(table);
}
//tao danh sach cac ham
public void createFunction()
{
String strFunc[]=d.getNameFunc();
coFunc=new JComboBox(strFunc);
coFunc.setBounds(10,150,230,20);
coFunc.setBackground(new Color(24,88,213));
coFunc.setForeground(Color.yellow);
coFunc.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent evt)
{
numberFunc=coFunc.getSelectedIndex()+1;
createCoff(d.getNumberCoff(numberFunc));
}
});
lbl=new JLabel("Select Function:");
lbl.setBounds(10,130,100,20);
lbl.setForeground(new Color(135,240,240));
cp.add(coFunc);cp.add(lbl);
}
//tao danh sach cac ham da ve
public void createCoHistory()
{
coHistory=new JComboBox();
coHistory.setBounds(10,410,230,20);
coHistory.setBackground(new Color(22,182,23));
coHistory.setForeground(Color.yellow);
coHistory.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent evt)
{
intHistory=coHistory.getSelectedIndex()+1;
back(false);
}
});
lbl=new JLabel("Functions were drawn:");
lbl.setBounds(10,380,200,20);
lbl.setForeground(new Color(140,245,20));
cp.add(coHistory);cp.add(lbl);
}
//ve chuong trinh
public void paintGraph()
{
table.setDragEnabled(false);table.selectAll();
Double heSo[]=new Double[numbers];double com[]=new double[numbers];
for(int i=0;i<numbers;i++)
{
try
{
heSo[i]=new Double(String.valueOf(table.getValueAt(i,1)));
com[i]=heSo[i].doubleValue();
}
catch(Exception e)
{
heSo[i]=new Double("0");com[i]=heSo[i].doubleValue();
table.setValueAt("0",i,1);
}
}
try
{
Double x1=new Double(tfx1.getText());
Double x2=new Double(tfx2.getText());
if(check(x1.doubleValue(),x2.doubleValue()))
{
d.calculate(numberFunc,com,x1.doubleValue(),x2.doubleValue());
int numberFuncCurrent=coFunc.getSelectedIndex();
history(numberFuncCurrent,com,x1.doubleValue(),x2.doubleValue());
}
}catch(Exception e)
{
System.out.println("loi tai day ne"+e);
tfx1.setText("0");tfx2.setText("0");
}
table.setDragEnabled(true);
}
public void history(int numberFunc,double com[],double x1,double x2)
{
String value=" ";boolean check=false;
value+=String.valueOf(x1)+" ";
value+=String.valueOf(x2)+" ";
value+=String.valueOf(numberFunc)+" ";
for(int i=0;i<com.length;i++)
{value+=String.valueOf(com[i])+" ";}
for(int i=0;i<history.size();i++)
{
if(value.equals(String.valueOf(history.elementAt(i))))
{check=true;}
}
if(!check)
{
history.addElement(value);
coHistory.addItem(replaceStr(numberFunc,com,x1,x2));
intHistory=history.size();
coHistory.setSelectedIndex(intHistory-1);
back.setEnabled(true);forward.setEnabled(false);
}
}
public void remove()
{
intHistory=coHistory.getSelectedIndex();
history.removeElementAt(intHistory);
coHistory.removeItemAt(intHistory);
intHistory=intHistory--;
}
public String replaceStr(int numberFunc,double com[],double x1,double x2)
{
String strCoHis=String.valueOf(coFunc.getItemAt(numberFunc));
if(strCoHis.indexOf("A")>-1&&(numbers>0))
{strCoHis=strCoHis.replaceFirst("A",String.valueOf(com[0]));}
if(strCoHis.indexOf("B")>-1&&(numbers>1))
{strCoHis=strCoHis.replaceFirst("B",String.valueOf(com[1]));}
if(strCoHis.indexOf("C")>-1&&(numbers>2))
{strCoHis=strCoHis.replaceFirst("C",String.valueOf(com[2]));}
if(strCoHis.indexOf("D")>-1&&(numbers>3))
{strCoHis=strCoHis.replaceFirst("D",String.valueOf(com[3]));}
if((strCoHis.indexOf("E")>-1)&&(numbers>4))
{strCoHis=strCoHis.replaceFirst("E",String.valueOf(com[4]));}
strCoHis+=" / ["+String.valueOf(x1)+","+String.valueOf(x2)+"]";
return strCoHis;
}
public void back(boolean press)
{
if(intHistory>0)
{
intHistory--;
String value=String.valueOf(history.elementAt(intHistory));
String items[]=value.split(" ");
coFunc.setSelectedIndex(Integer.parseInt(items[3]));
tfx1.setText(items[1]);
tfx2.setText(items[2]);
for(int i=4;i<items.length;i++)
{table.setValueAt(items[i],i-4,1);}
forward.setEnabled(true);
if(press){coHistory.setSelectedIndex(intHistory);}
}
else{back.setEnabled(false);forward.setEnabled(true);}
}
public void forward()
{
if(intHistory<history.size()-1)
{
intHistory++;
String value=String.valueOf(history.elementAt(intHistory));
String items[]=value.split(" ");
coFunc.setSelectedIndex(Integer.parseInt(items[3]));
tfx1.setText(items[1]);
tfx2.setText(items[2]);
for(int i=4;i<items.length;i++)
{table.setValueAt(items[i],i-4,1);}
back.setEnabled(true);
coHistory.setSelectedIndex(intHistory);
}
else{forward.setEnabled(false);back.setEnabled(true);}
}
public boolean check(double x1,double x2)
{
boolean value=true;
if(x1>=x2)
{
value=false;tfx1.setText(String.valueOf(x2));
tfx2.setText(String.valueOf(x1));
}
if((numberFunc==6)&&(x1<0))
{value=false;tfx1.setText(String.valueOf(Math.abs(x1)));}
if((numberFunc==6)&&(x2<0))
{value=false;tfx2.setText(String.valueOf(Math.abs(x2)));}
if((numberFunc==6)&&(x1==0))
{value=false;tfx1.setText(String.valueOf(Math.abs(1)));}
if((numberFunc==6)&&(x2==0))
{value=false;tfx2.setText(String.valueOf(Math.abs(1)));}
return value;
}
public void cancel()
{
for(int i=0;i<numbers;i++)
{table.setValueAt("",i,1);}
tfx1.setText("");tfx2.setText("");
}
public static void main(String arg[])
{
graph g=new graph();
g.setSize(800,570);
g.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -