Showing posts with label Swing. Show all posts
Showing posts with label Swing. Show all posts

Swing components - event driven programming - calculator

// Swing components
// calculator
// event driven programming 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class calculator implements ActionListener
{
     double n1, n2, result;
     JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
     JButton add,sub,mul,div,eq,dot;
     JTextField tf;
     JFrame jf;
    
     JPanel p1, p2;
    
     String s1="";
     double Num1=0;
     double Num2=0;
     double res=0;
     String opn="";
    
     calculator()
     {
          jf = new JFrame();
          jf.setSize(250,200);
          jf.setTitle("Calculator");
         
         
          tf = new JTextField("0");
          tf.setEditable(false);
          tf.setHorizontalAlignment(JTextField.RIGHT);
          jf.add(tf, BorderLayout.NORTH);
         
          GridLayout GL = new GridLayout(4,4);
          p2 = new JPanel();
          p2.setLayout(GL);
         
          b1 = new JButton("1");
          b2 = new JButton("2");
          b3 = new JButton("3");
          b4 = new JButton("4");
          b5 = new JButton("5");
          b6 = new JButton("6");
          b7 = new JButton("7");
          b8 = new JButton("8");
          b9 = new JButton("9");
          b0 = new JButton("0");
          add = new JButton("+");
          sub = new JButton("-");
          mul = new JButton("*");
          div = new JButton("/");
          eq = new JButton("=");
          dot = new JButton(".");
             
          p2.add(b9);
          p2.add(b8);
          p2.add(b7);
          p2.add(div);
          p2.add(b6);
          p2.add(b5);
          p2.add(b4);
          p2.add(mul);
          p2.add(b3);
          p2.add(b2);
          p2.add(b1);
          p2.add(sub);
          p2.add(b0);
          p2.add(dot);
          p2.add(eq);
          p2.add(add);
         
          jf.add(p2, BorderLayout.CENTER);
         
          b0.addActionListener(this);
          b1.addActionListener(this);
          b2.addActionListener(this);
          b3.addActionListener(this);
          b4.addActionListener(this);
          b5.addActionListener(this);
          b6.addActionListener(this);
          b7.addActionListener(this);
          b8.addActionListener(this);
          b9.addActionListener(this);
          add.addActionListener(this);
          sub.addActionListener(this);
          mul.addActionListener(this);
          div.addActionListener(this);
          eq.addActionListener(this);
          dot.addActionListener(this);
         
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
    
     public void actionPerformed(ActionEvent ae)
     {
          String str = ae.getActionCommand();
         
          if((str.equals("+"))||(str.equals("-"))||(str.equals("*"))||(str.equals("/")))
          {
              Num1 = Double.parseDouble(s1);
              s1="";
              tf.setText("");
              opn=str;
          }
          else if(str.equals("="))
          {
              Num2 = Double.parseDouble(s1);
              s1="";
             
              char op = opn.charAt(0);
              switch(op)
              {
                   case '+':  res = Num1+Num2;
                                  break;
                   case '-':   res = Num1-Num2;
                                  break;
                   case '*':  res = Num1*Num2;
                                  break;
                   case '/':   res = Num1/Num2;
                                  break;                                
              }
             
              tf.setText(String.valueOf(res));
          }
          else
          {
              s1=s1+str;
              tf.setText(s1);
          }
     }
    
     public static void main(String as[])
     {
          new calculator();
     }
}

java Swing - orbit


/*
Write a java program to plot the path of small circle moving around the circumference of a larger circle.
*/
         
// Java Graphics
// Orbit


import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.geom.*;

import java.lang.Math.*;

public class Orbit extends Frame
{
     double degree=0;
     int x1, y1;
    
     public void paint(Graphics g)
     {  
          g.fillOval(200,200,100,100);
     g.drawOval(50,50,400,400);
    
          degree=(degree+10)%360;
          x1 = (int)(200*java.lang.Math.cos((degree*2.0*3.142)/360.0))+250;
          y1 = (int)(200*java.lang.Math.sin((degree*2.0*3.142)/360.0))+250;

          g.drawOval(x1-15,y1-15,30,30);
             
          try
          {
              Thread.sleep(100);
          }
          catch(Exception e)
          {}
             
          repaint();
     }

     public static void main(String as[])
     {
          Orbit obj = new Orbit();
         
          obj.setSize(new Dimension(500,500));
          obj.setTitle("Orbit");
                       
          obj.setVisible(true);       
     }
}

Output:



Java Swing - distance converter


/*
Create a new Java GUI application to convert miles to kilometers when pressing the "Convert!" button. Note that you need to implement the ActionListener interface and override the actionPerformed() method.  Note that 1 mile is equal to 1.609 kilometers.
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Converter implements ActionListener
{
     JFrame jf;
     JLabel L1, L2;
     JTextField tf1, tf2;
     JButton b1;
    
     Converter()
     {
          jf = new JFrame();
         
          jf.setSize(350,200);
          jf.setTitle("Converter");
         
          FlowLayout FL = new FlowLayout(FlowLayout.CENTER,20,20);
          jf.setLayout(FL);
         
          Font f1 = new Font("Freestyle Script", Font.BOLD, 20);
          Font f2 = new Font("Freestyle Script", Font.BOLD, 15);
          Dimension d1 = new Dimension(100,30);
          Dimension d2 = new Dimension(75,30);

          L1 = new JLabel("Miles : ", SwingConstants.RIGHT);
          L2 = new JLabel("Kilometers : ",SwingConstants.RIGHT);
          L1.setFont(f1);
          L1.setPreferredSize(d1);
          L2.setFont(f1);
          L2.setPreferredSize(d1);
         
          tf1=new JTextField("",15);
          tf2=new JTextField("",15);
         
          tf1.setFont(f1);
          tf2.setFont(f1);
         
                  
          b1 = new JButton("Convert!");
          b1.setFont(f2);
          b1.setPreferredSize(d2);
             
          jf.add(L1);
         
          jf.add(tf1);
          jf.add(L2);
          jf.add(tf2);
          jf.add(b1);
         
          b1.addActionListener(this);
                       
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
    
    
     public void actionPerformed(ActionEvent ae)
     {
          double m = Double.parseDouble(tf1.getText());
          double km = m*1.609;
         
          tf2.setText(Double.toString(km));
     }
    
     public static void main(String as[])
     {
          new Converter();
     }
}

Output: