public class GUI extends JFrame implements ActionListener {
private static final long serialVersionUID = -7619621152921101049L;
public static final int STANDARD_HEIGHT = 900, STANDARD_WIDTH = 1200;
public static final String STANDARD_TITLE = "Rimwar";
public JButton builds;
public ArrayList<JButton> allButtons;
public Zeichenflaeche canvas = null;
public JPanel untereReihe = null;
public JPanel buildsMenu;
public JScrollPane canvasScrollPaneLeft = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
public JPanel canvasPanelLeft = new JPanel();
// übrigens: JA, die Methode wird (bei der Erstellung von GUI) aufgerufen
// ich erspare euch nur die ganzen Konstruktoren
public void init(String title, int width, int height) {
setTitle(title);
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
canvasScrollPaneLeft.setViewportView(canvasPanelLeft);
canvasScrollPaneLeft.setFocusable(false);
canvasPanelLeft.setFocusable(false);
canvas = new Zeichenflaeche();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setFocusable(true);
canvas.setLayout(new BorderLayout());
canvas.add(canvasScrollPaneLeft, BorderLayout.WEST);
canvasScrollPaneLeft.setVisible(false);
add(canvas, BorderLayout.CENTER);
allButtons = new ArrayList<JButton>();
builds = new JButton("Build things");
builds.setActionCommand("build");
allButtons.add(builds);
buildsMenu = new JPanel(new GridLayout(0, 3));
//buildsMenu.setBackground(new Color(0, 0, 0, 0));
buildsMenu.setBackground(Color.BLUE);
buildsMenu.add(new JButton("1")).setFocusable(false);
buildsMenu.add(new JButton("2")).setFocusable(false);
buildsMenu.add(new JButton("3")).setFocusable(false);
buildsMenu.add(new JButton("4")).setFocusable(false);
buildsMenu.add(new JButton("5")).setFocusable(false);
buildsMenu.add(new JButton("6")).setFocusable(false);
buildsMenu.add(new JButton("7")).setFocusable(false);
buildsMenu.add(new JButton("8")).setFocusable(false);
buildsMenu.add(new JButton("9")).setFocusable(false);
buildsMenu.add(new JButton("10")).setFocusable(false);
initMenu(buildsMenu);
initButtons();
pack();
}
private void initMenu(JComponent menu) {
menu.setVisible(false);
Dimension d = menu.getLayout().minimumLayoutSize(menu);
menu.setPreferredSize(d);
menu.setSize(d);
menu.setFocusable(false);
}
private void initButtons() {
final int buttonHeight = 50;
untereReihe = new JPanel();
untereReihe.setBackground(new Color(0, 0, 0, 0));
untereReihe.setPreferredSize(new Dimension(canvas.getPreferredSize().width, buttonHeight));
untereReihe.setLayout(new GridLayout(1, 0));
final Color c = new Color(0, 0, 150, 125);
for (int i=0; i<allButtons.size(); i++) {
allButtons.get(i).setFocusable(false);
allButtons.get(i).setBackground(c);
allButtons.get(i).setForeground(Color.white);
allButtons.get(i).addActionListener(this);
untereReihe.add(allButtons.get(i));
}
canvas.add(untereReihe, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent e) {
if("build".equals(e.getActionCommand())) {
toggleLeftMenu(buildsMenu);
/*
if(buildsMenu.isVisible()) {
buildsMenu.setVisible(false);
} else {
buildsMenu.setVisible(true);
Dimension d = new Dimension(buildsMenu.getPreferredSize().width+3, canvas.getPreferredSize().height - this.untereReihe.getPreferredSize().height);
canvasScrollPaneLeft.setPreferredSize(d);
canvasScrollPaneLeft.setSize(d);
canvasScrollPaneLeft.setViewportView(buildsMenu);
canvasScrollPaneLeft.repaint();
buildsMenu.repaint();
this.repaint();
}
//*/
}
}
private void toggleLeftMenu(JComponent menu) {
if(menu.isVisible()) {
menu.setVisible(false);
canvasPanelLeft.removeAll();
canvasScrollPaneLeft.setVisible(false);
} else {
menu.setVisible(true);
canvasPanelLeft.removeAll();
canvasPanelLeft.add(menu);
canvasPanelLeft.setPreferredSize(menu.getPreferredSize());
canvasPanelLeft.validate();
canvasPanelLeft.setVisible(true);
canvasScrollPaneLeft.setPreferredSize(new Dimension(canvasPanelLeft.getPreferredSize().width+3, 800));
canvasScrollPaneLeft.setViewportView(canvasPanelLeft);
canvasScrollPaneLeft.validate();
canvasScrollPaneLeft.setVisible(true);
canvasScrollPaneLeft.repaint();
}
}
}