Created
June 2, 2011 13:17
-
-
Save CvutFelStudentAccount/1004400 to your computer and use it in GitHub Desktop.
Voting (Java)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * To change this template, choose Tools | Templates | |
| * and open the template in the editor. | |
| */ | |
| package Voting; | |
| import java.awt.BorderLayout; | |
| import java.awt.Color; | |
| import java.awt.Graphics; | |
| import java.awt.Graphics2D; | |
| import java.awt.event.ActionEvent; | |
| import java.awt.event.ActionListener; | |
| import javax.swing.JButton; | |
| import javax.swing.JFrame; | |
| import javax.swing.JLabel; | |
| import javax.swing.JPanel; | |
| /** | |
| * | |
| * @author Petr | |
| */ | |
| public class Voting extends JFrame { | |
| JPanel buttonPanel; | |
| VotingPanel votingPanel; | |
| public Voting() { | |
| this.setSize(500, 500); | |
| this.setTitle("Voting"); | |
| votingPanel = new VotingPanel(); | |
| createButtonPanel(); | |
| this.getContentPane().add(buttonPanel, BorderLayout.SOUTH); | |
| this.getContentPane().add(votingPanel, BorderLayout.CENTER); | |
| this.setResizable(false); | |
| this.setDefaultCloseOperation(EXIT_ON_CLOSE); | |
| } | |
| void createButtonPanel() { | |
| buttonPanel = new JPanel(); | |
| buttonPanel.setBackground(Color.BLACK); | |
| JButton b1 = new JButton("Holeček"); | |
| b1.setBounds(20, 20, 50, 50); | |
| JButton b2 = new JButton("Hošek"); | |
| b1.addActionListener(new B1Listener()); | |
| b2.addActionListener(new B2Listener()); | |
| buttonPanel.add(b1); | |
| buttonPanel.add(b2); | |
| } | |
| class B1Listener implements ActionListener { | |
| public void actionPerformed(ActionEvent e) { | |
| votingPanel.addFirstVote(); | |
| } | |
| } | |
| class B2Listener implements ActionListener { | |
| public void actionPerformed(ActionEvent e) { | |
| votingPanel.addSecondVote(); | |
| } | |
| } | |
| public static void main(String[] args) { | |
| Voting v = new Voting(); | |
| v.setVisible(true); | |
| } | |
| } | |
| class VotingPanel extends JPanel { | |
| JLabel firstLabel; | |
| JLabel secondLabel; | |
| static int firstVoteCount = 0; | |
| static int secondVoteCount = 0; | |
| final int voteRectangleWidth = 100; | |
| final int firstRectXPosition = 100; | |
| final int firstRectYPosition = 400; | |
| final int secondRectXPosition = 300; | |
| final int secondRectYPosition = 400; | |
| public VotingPanel() { | |
| this.setBackground(Color.LIGHT_GRAY); | |
| this.setLayout(null); | |
| firstLabel = new JLabel("Holeček"); | |
| firstLabel.setBounds(125, 400, 100, 50); | |
| this.add(firstLabel); | |
| secondLabel = new JLabel("Hošek"); | |
| secondLabel.setBounds(335, 400, 100, 50); | |
| this.add(secondLabel); | |
| } | |
| @Override | |
| public void paint(Graphics g) { | |
| Graphics2D g2 = (Graphics2D) g; | |
| super.paint(g2); | |
| if (firstVoteCount != 0) { | |
| g2.setColor(Color.BLACK); | |
| g2.fillRect(firstRectXPosition - 5, firstRectYPosition - 10 * firstVoteCount - 5, voteRectangleWidth + 10, 10 * firstVoteCount + 10); | |
| g2.setColor(Color.GREEN); | |
| g2.fillRect(firstRectXPosition, firstRectYPosition - 10 * firstVoteCount, voteRectangleWidth, 10 * firstVoteCount); | |
| } | |
| if (secondVoteCount != 0) { | |
| g2.setColor(Color.BLACK); | |
| g2.fillRect(secondRectXPosition - 5, secondRectYPosition - 10 * secondVoteCount - 5, voteRectangleWidth + 10, 10 * secondVoteCount + 10); | |
| g2.setColor(Color.RED); | |
| g2.fillRect(secondRectXPosition, secondRectYPosition - 10 * secondVoteCount, voteRectangleWidth, 10 * secondVoteCount); | |
| } | |
| } | |
| void addFirstVote() { | |
| this.firstVoteCount++; | |
| repaint(); | |
| } | |
| void addSecondVote() { | |
| this.secondVoteCount++; | |
| repaint(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment