Showing posts with label Event Handling. Show all posts
Showing posts with label Event Handling. Show all posts

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:

Java Swing - Traffic lights


/*

Write a java program that simulates a traffic light . The program lets the  user select one of three lights: red, yellow, or green with radio buttons. On selecting a button, an appropriate message with stop or ready or go should appear above the buttons in a selected color . Initially there is no  message shown .

*/

// Swing components
// Traffic light

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

class TrafficLights implements ActionListener
{
     JFrame jf;
     JRadioButtonMenuItem r1, r2, r3;
     JLabel L1;
    
     TrafficLights()
     {
          jf = new JFrame();
         
          jf.setSize(350,200);
          jf.setTitle("Traffic Lights");
         
          FlowLayout FL = new FlowLayout(FlowLayout.CENTER,20,20);
          jf.setLayout(FL);
         
          Font f1 = new Font("Freestyle Script", Font.BOLD, 30);
          Font f2 = new Font("Freestyle Script", Font.BOLD, 20);
          Dimension d1 = new Dimension(250,50);
          Dimension d2 = new Dimension(75,30);

         
          L1 = new JLabel("",SwingConstants.CENTER);
          L1.setFont(f1);
          L1.setPreferredSize(d1);
                  
          r1 = new JRadioButtonMenuItem("Red");
          r2 = new JRadioButtonMenuItem("Yellow");
          r3 = new JRadioButtonMenuItem("Green");
         
          r1.setFont(f2);
          r2.setFont(f2);
          r3.setFont(f2);
          r1.setPreferredSize(d2);
          r2.setPreferredSize(d2);
          r3.setPreferredSize(d2);

          ButtonGroup bg = new ButtonGroup();
          bg.add(r1);
          bg.add(r2);
          bg.add(r3);
                  
          jf.add(L1);
         
          jf.add(r1);
          jf.add(r2);
          jf.add(r3);
         
          r1.addActionListener(this);
          r2.addActionListener(this);
          r3.addActionListener(this);
             
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
    
    
     public void actionPerformed(ActionEvent ae)
     {
          Color c1 = Color.black;
          String str = "";
                  
          if(r1.isSelected())
          {  
              str = "Stop";
              c1 = Color.red;
          }
          if(r2.isSelected())
          {
              str = "Ready";
              c1 = Color.yellow;
          }
          if(r3.isSelected())
          {
              str = "Go";
              c1 = Color.green;
          }
         
             
          L1.setText(str);
          L1.setForeground(c1);   
         
     }
    
     public static void main(String as[])
     {
          new TrafficLights();
     }
}


Output:

Initial window
 
Red Selected









Yellow Selected









Green Selected