Last active
August 29, 2015 14:11
-
-
Save Robotto/c77d3d3d8bc4aab0f17a to your computer and use it in GitHub Desktop.
Prog2_U6_A1
This file contains 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
Jeg indser at det er super åndssvagt at putte mine textboxes ind i containers, bare for at få layout til at se rigtigt ud, for det er super besværligt at få fat i dem og dermed deres indhold, når det skal bruges senere... men det virker, så jeg gider ikke optimere det yderligere... :) |
This file contains 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
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class BinomialApplet extends JApplet { | |
//init the two binomial algorithms: | |
private Binomial1 algo1 = new Binomial1(); | |
private Binomial2 algo2 = new Binomial2(); | |
private Integer n=null,k=null; | |
private ButtonGroup g = new ButtonGroup(); //group of mutually exclusive radio button | |
private Container txtFieldContainer = new Container(); //graphical container for the n and k input fields | |
private Container cp = getContentPane(); //graphical container for the entire applet. | |
//text fields are added to a container (in this case called txtFieldContainer) | |
private void addTextField(int columns, String label, Container pane) | |
{ | |
JTextField field = new JTextField(columns); | |
field.setName(label); //for ease of identification when pulling fields from group later... | |
//field.setAlignmentX(Component.CENTER_ALIGNMENT); | |
JLabel lbl=new JLabel(label); | |
pane.add(field); | |
pane.add(lbl); | |
} | |
//radio buttons are added to a container (in this case called radioContainer), they are also added to a group called g | |
private void addRadioBtn(String label, Container pane, boolean isActive) | |
{ | |
JRadioButton rb = new JRadioButton(label, isActive); | |
//rb.setAlignmentX(Component.CENTER_ALIGNMENT); | |
rb.setActionCommand(label); //stored as a string... used a sort of a label to ID which button in the group is active | |
g.add(rb); | |
pane.add(rb); | |
} | |
//extracts the textfields from the txtFieldContainer... it's silly, I know.. | |
private boolean parseNK(){ | |
for (Component c : txtFieldContainer.getComponents()) | |
{ | |
if (c instanceof JTextField) | |
{ | |
String name=((JTextField)c).getName(); | |
String contents=((JTextField) c).getText(); | |
//validate that input is actually parseable as integers: | |
try{ | |
if (name.equals("n")) n = new Integer(contents); | |
else if(name.equals("k")) k = new Integer(contents); | |
} | |
catch(NumberFormatException err) { | |
JOptionPane.showMessageDialog(cp,"Input for " + name + " er ikke et heltal!",err.getMessage(),JOptionPane.ERROR_MESSAGE); | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
//checks that the inputs obeys all the rules, and tells the user about each breach. | |
private boolean validateInput() | |
{ | |
boolean returnVal=true; | |
if(n>60){ JOptionPane.showMessageDialog(cp,"n må ikke være større end 60!","n er for stor!",JOptionPane.ERROR_MESSAGE); returnVal=false; } | |
if(n<0){ JOptionPane.showMessageDialog(cp,"n må ikke være mindre end 0!","n er for lille!",JOptionPane.ERROR_MESSAGE); returnVal=false; } | |
if(k<0){ JOptionPane.showMessageDialog(cp,"k må ikke være mindre end 0!","k er for lille!",JOptionPane.ERROR_MESSAGE); returnVal=false; } | |
if(k>n){ JOptionPane.showMessageDialog(cp,"k må ikke være større end n!","k er for stor!",JOptionPane.ERROR_MESSAGE); returnVal=false; } | |
return returnVal; | |
} | |
private ActionListener al = new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
if(parseNK()==false) return; | |
if(validateInput()==false) return; | |
//actully do the thing: | |
String resultat=""; | |
String action=g.getSelection().getActionCommand(); //get the actionCommand of the currently selected radiobutton in the radiogroup | |
if(action.equals("Algoritme 1")) resultat+=algo1.binomial(n,k); | |
else if(action.equals("Algoritme 2")) resultat+=algo2.binomial(n,k); | |
JOptionPane.showMessageDialog(cp,"Resultat for n=" + n +" og k=" + k + ":\n\n" + resultat ,"Resultatvindue",JOptionPane.PLAIN_MESSAGE); | |
} | |
}; | |
public void init() { | |
Button calculate = new Button("Calculate!"); | |
calculate.addActionListener(al); //this is where the magic happens | |
//Radio button stuff: | |
Container radioContainer = new Container(); | |
radioContainer.setLayout(new FlowLayout()); | |
addRadioBtn("Algoritme 1",radioContainer,true); | |
addRadioBtn("Algoritme 2",radioContainer,false); | |
//Text field stuff: | |
txtFieldContainer.setLayout(new FlowLayout()); | |
addTextField(2,"n",txtFieldContainer); | |
addTextField(2,"k",txtFieldContainer); | |
cp.setLayout(new BoxLayout(cp, BoxLayout.Y_AXIS)); | |
cp.add(txtFieldContainer); | |
cp.add(radioContainer); | |
cp.add(calculate); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment