/* * n_graph4.java * * Created on 29 Δεκέμβριος 2003, 10:07 πμ */ /** * * @author gauss * @version */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class n_graph4 extends java.applet.Applet implements ActionListener { TextField textField1 = new TextField(); //input textField TextField textField2 = new TextField(); //output textField Label label1 = new Label(); Label label2 = new Label(); Label label3 = new Label(); Button button1 = new Button(); public void init() { setLayout(null); //free layout setSize(600,300); //size of this applet add(textField1); textField1.setForeground(Color.blue); textField1.setFont(new Font("Dialog", Font.BOLD, 15)); textField1.setBounds(100,50,150,30); add(textField2); textField2.setForeground(Color.green); textField2.setFont(new Font("Dialog", Font.BOLD, 15)); textField2.setBounds(100,120,150,30); add(label1); label1.setText(" πληθος"); label1.setFont(new Font("Dialog", Font.BOLD, 12)); label1.setBounds(20,50,70,40); add(label2); label2.setText("αθροισμα"); label2.setFont(new Font("Dialog", Font.BOLD, 12)); label2.setBounds(20,120,70,40); add(label3); label3.setBackground(Color.lightGray); label3.setForeground(Color.blue); label3.setText("υπολογισμος του αθροισματος 1+2+...+n"); label3.setFont(new Font("Dialog", Font.BOLD, 12)); label3.setBounds(50,10,250,30); add(button1); button1.setForeground(Color.blue); button1.setLabel("υπολογισε"); button1.setBackground(java.awt.Color.magenta); button1.setFont(new Font("Dialog", Font.BOLD, 12)); button1.setBounds(100,200,120,30); button1.addActionListener(this); } public void actionPerformed(ActionEvent event) { Object object = event.getSource(); String str; int n,sum; if (object == button1){ // get text from textfield str=textField1.getText(); // convert string to integer n=Integer.parseInt(str); sum=calculate(n); // convert integer to string str=Integer.toString(sum); //set text to textfield textField2.setText(str); } } public int calculate(int n){ int i; // index int a; // i-th term int s=0; // sum of the sequence for(i=1;i<=n;i++){ a=i; //a(i)=i s=s+a; // S(i+1)=S(i)+a(i) } return s; } }