Created
October 6, 2016 06:37
-
-
Save joakiti/185da852352bf59e4fdb9903c38618e5 to your computer and use it in GitHub Desktop.
Øvelse i swing.
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
// Der er 4 klasser indtil videre. | |
// Dette er den første klasse, som skal bruges til at initialisere alle "scenarierne" baseret på de valg man tager: | |
import java.util.HashMap; | |
/** | |
* Lav en beskrivelse af klassen Scenarios her. | |
* | |
* @author (dit navn her) | |
* @version (versions nummer eller dato her) | |
*/ | |
public class Scenarios | |
{ | |
private String scenario; | |
private HashMap<String, Integer> allScenarios; | |
public Scenarios(String scenario) { | |
this.scenario = scenario; | |
allScenarios = new HashMap<>(); | |
} | |
public String getScenario() { | |
return scenario; | |
} | |
} | |
// Her er interfacen, som bliver brugt til at starte selveste programmet, og indeholder de vigtigeste informationer. | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.io.PrintStream; | |
public class ButtonInterface | |
{ | |
JFrame frame = new JFrame(); | |
JButton b1 = new JButton("One"); | |
JButton b2 = new JButton("Two"); | |
private Scenarios Scenario; | |
public ButtonInterface(int wW, int wH) { | |
frame.setLayout(null); | |
frame.setSize(wW, wH); | |
JTextArea textArea = new JTextArea(); | |
textArea.setSize(wW,wH/2); | |
textArea.setBounds(0, wH/2, wW, wH/2); | |
textArea.setLineWrap(true); | |
textArea.setEditable(false); | |
textArea.setVisible(true); | |
b1.setBounds(0, 0, (wW/2), wH/2); | |
b1.setVisible(true); | |
b1.setText("Go Right"); | |
b2.setBounds((wW/2), 0, (wW/2), wH/2); | |
b2.setVisible(true); | |
b2.setText("Go Left"); | |
frame.add(b1); | |
frame.add(b2); | |
frame.add(textArea); | |
System.setOut(new PrintStream(new TextAreaOutputStream(textArea))); | |
frame.setVisible(true); | |
System.out.println("You find yourself abandoned in the woods. Stormy weather burdens the sky."); | |
System.out.println("You come to a fork, what do you do?"); | |
b1.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed (ActionEvent e) | |
{ | |
myImportantAction(b1); // her | |
} | |
}); | |
b2.addActionListener(new ActionListener() | |
{ | |
public void actionPerformed (ActionEvent e) | |
{ | |
myImportantAction(b2); // her | |
} | |
}); | |
} | |
private void myImportantAction(JButton b) | |
{ | |
b.setText("Clicked"); | |
} | |
} | |
// TextAreaOutputStream bliver brugt af button interface til at oversætte System.out.println TIL JTEXTAREA! AMAZING! HOW DOES IT WORK? | |
// | |
// I dont know.. But it works? | |
import java.awt.BorderLayout; | |
import java.awt.Container; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.io.PrintStream; | |
import javax.swing.JFrame; | |
import javax.swing.JScrollPane; | |
import javax.swing.JTextArea; | |
import javax.swing.SwingUtilities; | |
public class TextAreaOutputStream extends OutputStream | |
{ | |
private final JTextArea textArea; | |
private final StringBuilder sb = new StringBuilder(); | |
public TextAreaOutputStream(final JTextArea textArea) | |
{ | |
this.textArea = textArea; | |
} | |
@Override | |
public void write(int b) throws IOException | |
{ | |
if (b == '\r') | |
return; | |
if (b == '\n') | |
{ | |
final String text = sb.toString() + "\n"; | |
SwingUtilities.invokeLater(new Runnable() | |
{ | |
public void run() | |
{ | |
textArea.append(text); | |
} | |
}); | |
sb.setLength(0); | |
} | |
sb.append((char) b); | |
} | |
} | |
// Her er så den sidste klasse, som skal benytte sig af klasserne ButtonInterface og Scenarios. Indtil videre initialisere den blot ButtoInterface. | |
public class GameStart | |
{ | |
private ButtonInterface gameInterface; | |
public GameStart(int wW, int wH) { | |
gameInterface = new ButtonInterface(wW, wH); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment