Question

In: Computer Science

Develop a simple GUI-based Java program that may be used to control a lighting system

Develop a simple GUI-based Java program that may be used to control a lighting system

Solutions

Expert Solution

/* THIS IS A ELECTRIC BALL CONTROL PROGRAM. USING GUI BUTTON WE CAN SWITCH ON OR OFF THE BALL. ONE SLIDER IS AVAILABLE TO CONTROL CONTRAST LABEL */
/* STRUCTURE OF PROGRAM

PARENT FRAME CONSISTING OF TWO PANEL (panelTop AND panelButtom) WITH FlowLayout.
IN TOP PANEL ONE LABLE HAS ADDED TO SHOW THE IMAGE OF BALL IN ICON FORMAT
HERE WE HAVE TAKEN TWO IMAGE FOR BALL (b1.png and b2.png), ONE FOR OFF STATE AND ANOTHER FOR OFF STATE

IN BUTTOM PANEL , TWO OTHER PANELS(P1,P2) ARE ADDED.
TO PANEL ONE, BUTTON SWITCH (ON/OFF) ADDED.
TO PANEL TWO, SLIDER WITH LABEL ADDED.

INITALIZE THE LABEL WITH OFF STATE BALL

ONE CUSTOM EVENT HANDELER CLASS EventHandler HAS BEEN DEFINED WITH ACTIONLISTER,CHANGELISTER

EVENT HANDELER REGISTERED TO BUTTON (ON/OFF)
EVENT HANDELER REGISTERED TO JSLIDER

PLEASE VIEW SCREENSHOT OF CODE FOR BETTER UNDERSTANDING OF STRCUTURE

*/

import java.awt.*;   // for gridlayout
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.util.Hashtable;

public class Ball {

   JButton bl,b2;
   JPanel panelTop,panelButtom;
   JFrame frame ;
   JLabel label,label2;
float scaleFactor=1.0f;//change scaleFactor to change contrast
/*keep the value scaleFactor = 1.0f; as for a normal image*/
RescaleOp rescale;
JSlider slider;
int Switch=0;

   Ball()
   {
       // main window
       JFrame.setDefaultLookAndFeelDecorated(true);
       frame = new JFrame("Control a Lighting System");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      panelTop = new JPanel();
      panelButtom = new JPanel();

      frame.setLayout(new FlowLayout());
       frame.add(panelTop);
   frame.add(panelButtom);

       /*       PANEL 1 LAYOUT            */
       label=new JLabel();
       label.setBounds(50,50,300,300);
   showImg("b1.png",1.0f); // initialize with off state ball
   panelTop.add(label);

       /*       PANEL 2 LAYOUT            */

       panelButtom.setLayout(new GridLayout(2,1));
       JPanel p1=new JPanel();
       JPanel p2=new JPanel();

       panelButtom.add(p1);
       panelButtom.add(p2);

       bl=new JButton(" Swith On ");
   p1.add(bl);

       label2=new JLabel("Control Contrast");
      p2.add(label2);

   slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);

       //Turn on labels at major tick marks.
       slider.setMajorTickSpacing(10);
       slider.setMinorTickSpacing(1);
       slider.setPaintTicks(true); // ALLOW RO SHOW TICK BARS ON SLIDERS
       slider.setPaintLabels(true); // ALLOW RO SHOW LABELS ON SLIDERS
       p2.add(slider);

       /*SLIDDER WITH LABEL ON 0 POSITION AND 100 POSITION */
       Hashtable<Integer, JLabel> labels = new Hashtable<>();
       labels.put(0, new JLabel("Max"));
       labels.put(100, new JLabel("Low"));
       slider.setLabelTable(labels);


      /*       REGISTERING EVENT HANDELER (CUSTOME EVENT HANDELER)           */

   ActionListener myHandlerButton = new EventHandler();
        bl.addActionListener(myHandlerButton); /// Binding the custom event handler

       ChangeListener myHandleSlide = new EventHandler();
       slider.addChangeListener(myHandleSlide);


       /*SETTING OF PARENT FRAME TO DISPPLAY */
       frame.pack();
       frame.setVisible(true);
       frame.setSize(400,500);
}
   /* CUSTOM EVENT HANDELER CLASS */
    class EventHandler implements ActionListener,ChangeListener
    {
   public void actionPerformed(ActionEvent e) // WHEN BUTTON CLICK OCCURED
   {
           if(e.getSource() == bl)
           {
               if(Switch==0)
               {
                   Switch=1;
                   showImg("b2.png",1.0f); // call showImg function to show off image
                   bl.setText(" Switch Off "); // change label of button

               }else
               {
                   Switch=0;
                   showImg("b1.png",1.0f); // call showImg function to show off image
                   bl.setText(" Switch On "); // change label of button

               }
           }
   }
   public void stateChanged(ChangeEvent event) // WHEN JSLIDER VALUE CHANGED
   {
           if(Switch==0)
               /*WHEN BALB IF OFF , AND TRY TO CONTROL CONTRAST*/
              JOptionPane.showMessageDialog(panelButtom, "Please switch on ball","ERROR", JOptionPane.ERROR_MESSAGE);
          else
          {
               int value = slider.getValue(); // getting value form slider
               double f=value/100.0 + 1 ; // WE NORMAL SCALE FACTOR IN FORM OF 1.0f
               showImg("b2.png",(float)f); // call showImg function to show on image with desired contrast
           }
   }

   } // END OF EventHandler CLASS

   void showImg(String img,float sFactor )
   {
       try
       {
           BufferedImage image = ImageIO.read(new File(img)); //READING THE FILE IMAGE FROM STORAGE

           // Setting up the scaling and
   // the offset parameters for processing
   // WE ARE CONTROLING scaleFactor FROM SLIDER VALUE TO CONTROL CONTRAST
           rescale = new RescaleOp(sFactor,0.0f, null); // RescaleOp(float scaleFactor, float offset, RenderingHints hints)

           // applying the parameters on the image
   // by using filter() method,
           image=rescale.filter(image,null);
           ImageIcon icon = new ImageIcon(image);
           label.setIcon(icon);   //SET IMAGE TO LABEL IN BUTTON PANEL
       }
       catch(IOException ex)
       {
           System.out.println(ex);
       }

   }

   public static void main(String[] arguments)
   {
       new Ball();

   }
}

IMAGES OF BALL

b1.png

b2.png

SCREEN SHOT OF CODE

SCREEN SHOT OF OUTPUT


Related Solutions

URGENT JAVA Develop a Java computer program to simulate access control to a system network. The...
URGENT JAVA Develop a Java computer program to simulate access control to a system network. The username and password authentication mechanism is used as an access control mechanism in this project. The program needs to authenticate the user with their username and hashed password stored in Access Manager User (AMU) file. To create an Access Manager User file, you need to develop a java program that takes username and password as input and then generates a file containing the username,...
in a gui ' in java write a program that draws equal a simple fence with...
in a gui ' in java write a program that draws equal a simple fence with vertical, spaced slats backed by two boards. Behind the fence show a simple house support Make sure the in the und. house is visible between the slats in the fence.
Give an example of a program in java that creates a GUI with at least one...
Give an example of a program in java that creates a GUI with at least one button and several textfields. Some of the textfields should be for input and others for output. Make the output textfields uneditable. When the button is clicked, the input fields should be read, some calculation performed and the result displayed in the output textfield(s).
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
In Java Create a simple GUI that gets the exam score and shows the corresponding letter...
In Java Create a simple GUI that gets the exam score and shows the corresponding letter grade by clicking on the “Convert Score to Letter Grade” button.
Develop a very simple Tic Tac Toe gaming software in Java. Ifyou want, you may...
Develop a very simple Tic Tac Toe gaming software in Java. If you want, you may take some code (with proper reference to the source from where it is taken - i.e., hackerrank or github) from an existing game implementation to extend/improve the functionalities. To develop a good quality software, you would like to follow a particular software engineering process or more than one process (waterfall, incremental, agile etc.) and choose an appropriate programming language.
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
Use the below info to create a java program A GUI interface to ensure a user...
Use the below info to create a java program A GUI interface to ensure a user is old enough to play a game. Properly formatted prompts to input name, address, phone number, and age. Remember that name, address, phone number, etc. can be broken out in additional fields. Refer to the tutorial from this week’s Reading Assignment Multiple vs. Single Field Capture for Phone Number Form Input for help with this. Instructions to ensure that the information is displayed back...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following...
Using a (GUI interface), write a Java program that simulates an ATM machine with the following options menu: "Welcome" 1. Deposit to account 2. Withdraw 3. Exit
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT