How To Build U.S.A Voting System Applet On Java Eclipse

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to create a checkbox
  • You will learn how to create a applet on java with eclipse
  • You will learn difference between checkboxgroup and checkbox

Requirements

  • Eclipse
  • Java
  • Knowledge Of Coding

Difficulty

  • Basic

Tutorial Contents

Hello,in this tutorial we will create a new applet.This applet will be the United States Of America's Voting system.We will improve it step by step in the future.Let's begin...

First i define two of my libraries that i need for to create an applet.

import java.awt.*;
import java.applet.Applet;

Then i define my class and started program with using "init" method.This method is can be only called once in applet and it basically starts program.I used suppresswarnings because i haven't used "votes" class in anywhere.

 @SuppressWarnings("serial")
public class Votes extends Applet
{
     
    public void init()
    {

In this step i want my background to be white.After writing down "Color." you can select any color you want from the menu that will appear.

setBackground(Color.white);

Now i am defining my labels,label is used to put text on applet.I define three label to ask users citizen id,then i ask user to choose their state and finally user will choose the party they want to vote for.

       Label lbId=new Label("Please Write Your Citizen Id");
        Label lbState=new Label("Please Select Your State");
        Label lbParty=new Label("Please Select The Party You Want To Vote For ");

Now i will use "textfield" to write down my text.Basically after writing the code below "Please Write Your Citizen Id" message will appear on the top of the applet.

       TextField txtId=new TextField(); 

1.png

Now in this step i am defining 50 checkboxs which is all United States State's.If you use "checkbox" user will be able to choose all the checkboxes unless you add while loop on it.
" Checkbox cbAL=new Checkbox("AL");" As you can see in this line i used "cb" which means checkbox if you want to define a checkbox this is how you do it and in the final line you write down inside the "" what ever you want to write.

 Checkbox cbAL=new Checkbox("AL");
        Checkbox cbAK=new Checkbox("AK");
        Checkbox cbAZ=new Checkbox("AZ");
        Checkbox cbCA=new Checkbox("CA");
        Checkbox cbCO=new Checkbox("CO");
        Checkbox cbCT=new Checkbox("CT");
        Checkbox cbDE=new Checkbox("DE");
        Checkbox cbFL=new Checkbox("FL");
        Checkbox cbGA=new Checkbox("GA");
        Checkbox cbHI=new Checkbox("HI");
        Checkbox cbID=new Checkbox("ID");
        Checkbox cbIL=new Checkbox("IL");
        Checkbox cbIN=new Checkbox("IN");
        Checkbox cbIA=new Checkbox("IA");
        Checkbox cbKS=new Checkbox("KS");
        Checkbox cbKY=new Checkbox("KY");
        Checkbox cbLA=new Checkbox("LA");
        Checkbox cbME=new Checkbox("ME");
        Checkbox cbMD=new Checkbox("MD");
        Checkbox cbMA=new Checkbox("MA");
        Checkbox cbMI=new Checkbox("MI");
        Checkbox cbMN=new Checkbox("MN");
        Checkbox cbMS=new Checkbox("MS");
        Checkbox cbMO=new Checkbox("MO");
        Checkbox cbMT=new Checkbox("MT");
        Checkbox cbNE=new Checkbox("NE");
        Checkbox cbNV=new Checkbox("NV");
        Checkbox cbNH=new Checkbox("NH");
        Checkbox cbNJ=new Checkbox("NJ");
        Checkbox cbNM=new Checkbox("NM");
        Checkbox cbNY=new Checkbox("NY");
        Checkbox cbNC=new Checkbox("NC");
        Checkbox cbND=new Checkbox("ND");
        Checkbox cbOH=new Checkbox("OH");
        Checkbox cbOK=new Checkbox("OK");
        Checkbox cbOR=new Checkbox("OR");
        Checkbox cbPA=new Checkbox("PA");
        Checkbox cbRI=new Checkbox("RI");
        Checkbox cbSC=new Checkbox("SC");
        Checkbox cbSD=new Checkbox("SD");
        Checkbox cbTN=new Checkbox("TN");
        Checkbox cbTX=new Checkbox("TX");
        Checkbox cbUT=new Checkbox("UT");
        Checkbox cbVT=new Checkbox("VT");
        Checkbox cbVA=new Checkbox("VA");
        Checkbox cbWA=new Checkbox("WA");
        Checkbox cbWV=new Checkbox("WV");
        Checkbox cbWI=new Checkbox("WI");
        Checkbox cbWY=new Checkbox("WY");
       

2.png

Now we will make user can choose only one party because you can only vote to one party.To do that we need to use "checkboxgroup" method and define "cbparty" in it.When we do that user will have to choose one of the party.

        CheckboxGroup cbParty=new CheckboxGroup();

Now in the final step of defining,i define a button to make final step on voting;Vote button.

     Button btnVote=new Button("Click To Vote");  

3.png

In this step we will add all the variables we defined above on the applet.To do this we will use "add" method and if the variable is label we have to use for example "add(lbId)" the "lb" means label.Then "add(txtId)" by the way "txt" means text and as you can see we just added our text on to the applet.

We will do the same thing for the checkboxs.We already defined our checkboxs at above so now we will only use "add" to add our checkboxs on the applet.

    add(lbId);
        add(txtId);
        add(lbState);
        add(cbAL);
        add(cbAK);
        add(cbAZ);
        add(cbCA);
        add(cbCO);
        add(cbCT);
        add(cbDE);
        add(cbFL);
        add(cbGA);
        add(cbHI);
        add(cbID);
        add(cbIL);
        add(cbIN);
        add(cbIA);
        add(cbKS);
        add(cbKY);
        add(cbLA);
        add(cbME);
        add(cbMD);
        add(cbMA);
        add(cbMI);
        add(cbMN);
        add(cbMS);
        add(cbMO);
        add(cbMT);
        add(cbNE);
        add(cbNV);
        add(cbNH);
        add(cbNJ);
        add(cbNM);
        add(cbNY);
        add(cbNC);
        add(cbND);
        add(cbOH);
        add(cbOK);
        add(cbOR);
        add(cbPA);
        add(cbRI);
        add(cbSC);
        add(cbSD);
        add(cbTN);
        add(cbTX);
        add(cbUT);
        add(cbVT);
        add(cbVA);
        add(cbWA);
        add(cbWV);
        add(cbWI);
        add(cbWY);   

4.png

I especially gave a space between these lines because it is important to figure out the difference between checkbox group and checkbox.In this step i will use "add" again then since there are two parties in U.S.A which is democratic and republican user must choose only one of them and to do that we will use "add" and create a new checkbox inside the add method then we will use "true/false" to make user choose only one.

Also used"add(btnvote)" to add Vote button on the applet.

  add(lbParty);
        add(new Checkbox("Democratic Party  ",cbParty,true));
        add(new Checkbox("Republican Party  ",cbParty,false));     
       add(btnVote);

To set the applets visuality we will use "setlayout" and select our column and row numbers.Since we had 56 rows i made applet size 56,35.You can change it and see what happens when you make it bigger and smaller.

 setLayout(new GridLayout(56,35));
       

In the final step i am selecting my font types for my headings.

Font myFont = new Font("Serif",Font.BOLD,18);
        lbParty.setFont(myFont);
        lbId.setFont(myFont);
        lbState.setFont(myFont);
        Font myFonttwo = new Font("Serif",Font.BOLD,20);
        btnVote.setFont(myFonttwo);
       

5.png

Whole Code

       import java.awt.*;
import java.applet.Applet;

@SuppressWarnings("serial")
public class Votes extends Applet
{
     
    public void init()
    {
        setBackground(Color.white);

        Label lbId=new Label("Please Write Your Citizen Id");
        Label lbState=new Label("Please Select Your State");
        Label lbParty=new Label("Please Select The Party You Want To Vote For ");
        
        TextField txtId=new TextField();       
        
        
        Checkbox cbAL=new Checkbox("AL");
        Checkbox cbAK=new Checkbox("AK");
        Checkbox cbAZ=new Checkbox("AZ");
        Checkbox cbCA=new Checkbox("CA");
        Checkbox cbCO=new Checkbox("CO");
        Checkbox cbCT=new Checkbox("CT");
        Checkbox cbDE=new Checkbox("DE");
        Checkbox cbFL=new Checkbox("FL");
        Checkbox cbGA=new Checkbox("GA");
        Checkbox cbHI=new Checkbox("HI");
        Checkbox cbID=new Checkbox("ID");
        Checkbox cbIL=new Checkbox("IL");
        Checkbox cbIN=new Checkbox("IN");
        Checkbox cbIA=new Checkbox("IA");
        Checkbox cbKS=new Checkbox("KS");
        Checkbox cbKY=new Checkbox("KY");
        Checkbox cbLA=new Checkbox("LA");
        Checkbox cbME=new Checkbox("ME");
        Checkbox cbMD=new Checkbox("MD");
        Checkbox cbMA=new Checkbox("MA");
        Checkbox cbMI=new Checkbox("MI");
        Checkbox cbMN=new Checkbox("MN");
        Checkbox cbMS=new Checkbox("MS");
        Checkbox cbMO=new Checkbox("MO");
        Checkbox cbMT=new Checkbox("MT");
        Checkbox cbNE=new Checkbox("NE");
        Checkbox cbNV=new Checkbox("NV");
        Checkbox cbNH=new Checkbox("NH");
        Checkbox cbNJ=new Checkbox("NJ");
        Checkbox cbNM=new Checkbox("NM");
        Checkbox cbNY=new Checkbox("NY");
        Checkbox cbNC=new Checkbox("NC");
        Checkbox cbND=new Checkbox("ND");
        Checkbox cbOH=new Checkbox("OH");
        Checkbox cbOK=new Checkbox("OK");
        Checkbox cbOR=new Checkbox("OR");
        Checkbox cbPA=new Checkbox("PA");
        Checkbox cbRI=new Checkbox("RI");
        Checkbox cbSC=new Checkbox("SC");
        Checkbox cbSD=new Checkbox("SD");
        Checkbox cbTN=new Checkbox("TN");
        Checkbox cbTX=new Checkbox("TX");
        Checkbox cbUT=new Checkbox("UT");
        Checkbox cbVT=new Checkbox("VT");
        Checkbox cbVA=new Checkbox("VA");
        Checkbox cbWA=new Checkbox("WA");
        Checkbox cbWV=new Checkbox("WV");
        Checkbox cbWI=new Checkbox("WI");
        Checkbox cbWY=new Checkbox("WY");
       

        CheckboxGroup cbParty=new CheckboxGroup();

        Button btnVote=new Button("Click To Vote");

        add(lbId);
        add(txtId);
        add(lbState);
        add(cbAL);
        add(cbAK);
        add(cbAZ);
        add(cbCA);
        add(cbCO);
        add(cbCT);
        add(cbDE);
        add(cbFL);
        add(cbGA);
        add(cbHI);
        add(cbID);
        add(cbIL);
        add(cbIN);
        add(cbIA);
        add(cbKS);
        add(cbKY);
        add(cbLA);
        add(cbME);
        add(cbMD);
        add(cbMA);
        add(cbMI);
        add(cbMN);
        add(cbMS);
        add(cbMO);
        add(cbMT);
        add(cbNE);
        add(cbNV);
        add(cbNH);
        add(cbNJ);
        add(cbNM);
        add(cbNY);
        add(cbNC);
        add(cbND);
        add(cbOH);
        add(cbOK);
        add(cbOR);
        add(cbPA);
        add(cbRI);
        add(cbSC);
        add(cbSD);
        add(cbTN);
        add(cbTX);
        add(cbUT);
        add(cbVT);
        add(cbVA);
        add(cbWA);
        add(cbWV);
        add(cbWI);
        add(cbWY);
        
        
        add(lbParty);
        add(new Checkbox("Democratic Party  ",cbParty,true));
        add(new Checkbox("Republican Party  ",cbParty,false));
        add(btnVote);
        setLayout(new GridLayout(56,35));
        Font myFont = new Font("Serif",Font.BOLD,18);
        lbParty.setFont(myFont);
        lbId.setFont(myFont);
        lbState.setFont(myFont);
        Font myFonttwo = new Font("Serif",Font.BOLD,20);
        btnVote.setFont(myFonttwo);     
    }
}

In next tutorial we will keep improving the applet.In the final step hopefully we will add system on a website.

In next tutorial we will keep improving the applet.In the final step hopefully we will add system on a website.

Output

6.png

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @milliar I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Congratulations @milliar! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @milliar! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!