Ejemplo n.º 1
0
  void init() {
    int innerWidth = width - border * 2;

    /** Sketch */
    Panel sketchPanel = new Panel();
    sketchPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
    sketchPanel.setBackground(new Color(panelBG));
    sketchPanel.setPreferredSize(new Dimension(innerWidth, 270));

    CheckboxGroup sketchToolsGroup = new CheckboxGroup();
    Checkbox lineTool = new Checkbox("Line", sketchToolsGroup, true);
    lineTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.setCurrentTool(Sketch.Tool.LINE);
          }
        });
    Checkbox polygonTool = new Checkbox("Polygon", sketchToolsGroup, false);
    polygonTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.setCurrentTool(Sketch.Tool.POLYGON);
          }
        });
    Checkbox selectTool = new Checkbox("Select", sketchToolsGroup, false);
    selectTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.setCurrentTool(Sketch.Tool.SELECT);
          }
        });
    Checkbox moveTool = new Checkbox("Move", sketchToolsGroup, false);
    moveTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.setCurrentTool(Sketch.Tool.MOVE);
          }
        });

    strokeColor = new ColorWell();
    strokeColor.addChangeListener(
        new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            maker.sketch.setStrokeColor(strokeColor.getColor().getRGB());
          }
        });
    fillColor = new ColorWell();
    fillColor.addChangeListener(
        new ChangeListener() {
          @Override
          public void stateChanged(ChangeEvent e) {
            maker.sketch.setFillColor(fillColor.getColor().getRGB());
          }
        });

    snapH = new Checkbox("Snap Horizontal (H)");
    snapH.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.snapToHorz = snapH.getState();
          }
        });
    snapV = new Checkbox("Snap Vertical (V)");
    snapV.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.snapToVert = snapV.getState();
          }
        });
    drawCurves = new Checkbox("Draw Curves (C)");
    drawCurves.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.drawCurves = drawCurves.getState();
          }
        });
    invertCurve = new Checkbox("Invert Curve (I)");
    invertCurve.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.invertCurve = invertCurve.getState();
          }
        });

    Button clearSketch = new Button("Clear");
    clearSketch.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.sketch.clear();
            maker.redraw();
          }
        });

    showSketchLines = new Checkbox("Grid Lines");
    showSketchLines.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.sketch.drawSnapGrid = showSketchLines.getState();
            maker.redraw();
          }
        });

    Button snapGridDecrement = new Button("Grid -");
    snapGridDecrement.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.sketch.snapGridDecrement();
            maker.redraw();
          }
        });
    Button snapGridIncrement = new Button("Grid +");
    snapGridIncrement.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.sketch.snapGridIncrement();
            maker.redraw();
          }
        });

    sketchPanel.add(new Label("Sketch"));
    sketchPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    sketchPanel.add(new Label("Tools"));
    sketchPanel.add(lineTool);
    sketchPanel.add(polygonTool);
    sketchPanel.add(selectTool);
    sketchPanel.add(moveTool);
    sketchPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    sketchPanel.add(new Label("Stroke"));
    sketchPanel.add(strokeColor);
    sketchPanel.add(new Label("Fill"));
    sketchPanel.add(fillColor);
    sketchPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    sketchPanel.add(snapH);
    sketchPanel.add(snapV);
    sketchPanel.add(drawCurves);
    sketchPanel.add(invertCurve);
    sketchPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    sketchPanel.add(clearSketch);
    sketchPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    sketchPanel.add(snapGridDecrement);
    sketchPanel.add(snapGridIncrement);
    sketchPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    sketchPanel.add(showSketchLines);

    /** Palette */
    Panel palettePanel = new Panel();
    palettePanel.setLayout(new FlowLayout(FlowLayout.LEADING));
    palettePanel.setBackground(new Color(panelBG));
    palettePanel.setPreferredSize(new Dimension(innerWidth, 110));

    Button addToPalette = new Button("Add Sketch Tile");
    addToPalette.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.palette.saveTileToPalette();
            maker.redraw();
          }
        });

    Button editTile = new Button("Edit Tile");
    editTile.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.palette.loadPaletteTileToSketch();
            maker.redraw();
          }
        });

    Button deleteTile = new Button("Delete Tile(s)");
    deleteTile.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.palette.deletePaletteTile();
            maker.redraw();
          }
        });

    Button clearPalette = new Button("Remove All");
    clearPalette.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.palette.clear();
            maker.redraw();
          }
        });

    palettePanel.add(new Label("Palette"));
    palettePanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    palettePanel.add(addToPalette);
    palettePanel.add(editTile);
    palettePanel.add(deleteTile);
    palettePanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    palettePanel.add(clearPalette);

    /** Grid */
    Panel gridPanel = new Panel();
    gridPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
    gridPanel.setBackground(new Color(panelBG));
    gridPanel.setPreferredSize(new Dimension(innerWidth, 270));

    CheckboxGroup toolsGroup = new CheckboxGroup();
    Checkbox paintTool = new Checkbox("Paint", toolsGroup, true);
    paintTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.getCurrentLayout().currentTool = Layout.Tool.PAINT;
          }
        });
    Checkbox lockTool = new Checkbox("Lock", toolsGroup, false);
    lockTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.getCurrentLayout().currentTool = Layout.Tool.LOCK;
          }
        });
    Checkbox gridSelectTool = new Checkbox("Select", toolsGroup, false);
    gridSelectTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.getCurrentLayout().currentTool = Layout.Tool.SELECT;
          }
        });
    Checkbox gridMoveTool = new Checkbox("Move", toolsGroup, false);
    gridMoveTool.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.getCurrentLayout().currentTool = Layout.Tool.MOVE;
          }
        });

    final Choice layoutChoice = new Choice();
    layoutChoice.add("Seamless Progressive");
    layoutChoice.add("Seamless Random");
    layoutChoice.add("Pure Random");

    layoutChoice.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            String l = layoutChoice.getSelectedItem();
            if (l == "Seamless Progressive") {
              maker.getCurrentLayout().currentLayout = Layout.LayoutType.SEAM_PROG;
            } else if (l == "Seamless Random") {
              maker.getCurrentLayout().currentLayout = Layout.LayoutType.SEAM_RND;
            } else if (l == "Pure Random") {
              maker.getCurrentLayout().currentLayout = Layout.LayoutType.RND;
            }
          }
        });

    Scrollbar percentageFull = new Scrollbar(Scrollbar.HORIZONTAL, 100, 20, 0, 120);
    percentageFull.setPreferredSize(new Dimension(innerWidth / 2, 20));
    percentageFull.addAdjustmentListener(
        new AdjustmentListener() {
          @Override
          public void adjustmentValueChanged(AdjustmentEvent e) {
            float amount = (float) e.getValue() / 100;
            maker.getCurrentLayout().percentageFull = amount;
          }
        });

    final Checkbox transformI = new Checkbox("I", true);
    transformI.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.orientationsToUse.put(
                TruchetMaker.Orientation.I.toString(), transformI.getState());
          }
        });
    final Checkbox transform90 = new Checkbox("90", true);
    transform90.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.orientationsToUse.put(
                TruchetMaker.Orientation.R90.toString(), transform90.getState());
          }
        });
    final Checkbox transform180 = new Checkbox("180", true);
    transform180.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.orientationsToUse.put(
                TruchetMaker.Orientation.R180.toString(), transform180.getState());
          }
        });
    final Checkbox transform270 = new Checkbox("270", true);
    transform270.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.orientationsToUse.put(
                TruchetMaker.Orientation.R270.toString(), transform270.getState());
          }
        });
    final Checkbox transformH = new Checkbox("H", true);
    transformH.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.orientationsToUse.put(
                TruchetMaker.Orientation.MH.toString(), transformH.getState());
          }
        });
    final Checkbox transformV = new Checkbox("V", true);
    transformV.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.orientationsToUse.put(
                TruchetMaker.Orientation.MV.toString(), transformV.getState());
          }
        });

    Button doLayout = new Button("Do Layout");
    doLayout.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            for (Layout l : maker.layouts) {
              l.doLayout();
            }
          }
        });

    Button clearGrid = new Button("Clear");
    clearGrid.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.getCurrentLayout().clear();
            maker.redraw();
          }
        });

    Button incrementGrid = new Button("Grid +");
    incrementGrid.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.getCurrentLayout().incrementGridSize();
            maker.redraw();
          }
        });
    Button decrementGrid = new Button("Grid -");
    decrementGrid.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.getCurrentLayout().decrementGridSize();
            maker.redraw();
          }
        });

    final Checkbox showGridLines = new Checkbox("Grid Lines");
    //		showGridLines.setState(maker.grid.drawGridLines); // not available at this point
    showGridLines.setState(true);
    showGridLines.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.getCurrentLayout().drawGridLines = showGridLines.getState();
            maker.redraw();
          }
        });

    showStateOverlay = new Checkbox("Show States");
    showStateOverlay.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            maker.getCurrentLayout().overlayVisible(showStateOverlay.getState());
            maker.redraw();
          }
        });

    gridPanel.add(new Label("Grid"));
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(new Label("Tools"));
    gridPanel.add(paintTool);
    gridPanel.add(lockTool);
    gridPanel.add(gridSelectTool);
    gridPanel.add(gridMoveTool);
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(new Label("Layout"));
    gridPanel.add(layoutChoice);
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(new Label("Empty"));
    gridPanel.add(percentageFull);
    gridPanel.add(new Label("Full"));
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(transformI);
    gridPanel.add(transform90);
    gridPanel.add(transform180);
    gridPanel.add(transform270);
    gridPanel.add(transformH);
    gridPanel.add(transformV);
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(doLayout);
    gridPanel.add(clearGrid);
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(decrementGrid);
    gridPanel.add(incrementGrid);
    gridPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    gridPanel.add(showGridLines);
    gridPanel.add(showStateOverlay);

    add(sketchPanel);
    add(palettePanel);
    add(gridPanel);

    Panel layoutsPanel = new Panel();
    layoutsPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
    layoutsPanel.setBackground(new Color(panelBG));
    layoutsPanel.setPreferredSize(new Dimension(innerWidth, 70));

    final Choice layouts = new Choice();
    layouts.setPreferredSize(new Dimension(100, 20));
    layouts.add("Grid 1");
    layouts.addItemListener(
        new ItemListener() {
          @Override
          public void itemStateChanged(ItemEvent e) {
            int i = layouts.getSelectedIndex();
            maker.setCurrentLayout(maker.layouts.get(i));
            maker.redraw();
          }
        });

    Button addLayout = new Button("Add Grid");
    addLayout.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.addLayout();
            layouts.add("Grid " + maker.layouts.size());
            maker.redraw();
          }
        });
    Button removeLayout = new Button("Remove Grid");
    removeLayout.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            maker.removeLayout();
            layouts.removeAll();
            for (int i = 0; i < maker.layouts.size(); i++) {
              layouts.add("Grid " + (i + 1));
              maker.redraw();
            }
          }
        });

    layoutsPanel.add(addLayout);
    layoutsPanel.add(removeLayout);
    layoutsPanel.add(Box.createRigidArea(new Dimension(innerWidth, 0)));
    layoutsPanel.add(layouts);
    add(layoutsPanel);

    addKeyListener(
        new KeyListener() {
          @Override
          public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) {
              case KeyEvent.VK_C:
                maker.sketch.drawCurves = !maker.sketch.drawCurves;
                drawCurves.setState(maker.sketch.drawCurves);
                break;
              case KeyEvent.VK_I:
                maker.sketch.invertCurve = !maker.sketch.invertCurve;
                invertCurve.setState(maker.sketch.invertCurve);
                break;
              case KeyEvent.VK_H:
                maker.sketch.snapToHorz = !maker.sketch.snapToHorz;
                snapH.setState(maker.sketch.snapToHorz);
                break;
              case KeyEvent.VK_V:
                maker.sketch.snapToVert = !maker.sketch.snapToVert;
                snapV.setState(maker.sketch.snapToVert);
                break;
            }
          }

          @Override
          public void keyTyped(KeyEvent e) {
            // TODO Auto-generated method stub
          }

          @Override
          public void keyReleased(KeyEvent e) {
            // TODO Auto-generated method stub
          }
        });

    addWindowListener(
        new WindowListener() {

          @Override
          public void windowClosing(WindowEvent e) {
            //				dispose();
            //				System.exit(0);
          }

          @Override
          public void windowOpened(WindowEvent e) {
            // TODO Auto-generated method stub
          }

          @Override
          public void windowClosed(WindowEvent e) {
            // TODO Auto-generated method stub
          }

          @Override
          public void windowIconified(WindowEvent e) {
            // TODO Auto-generated method stub
          }

          @Override
          public void windowDeiconified(WindowEvent e) {
            // TODO Auto-generated method stub
          }

          @Override
          public void windowActivated(WindowEvent e) {
            // TODO Auto-generated method stub
          }

          @Override
          public void windowDeactivated(WindowEvent e) {
            // TODO Auto-generated method stub
          }
        });
  }
  public MokumFrame() {
    setTitle("Heuristieken 2014 - Mokum Airlines! - Group 4 \"De Vliegende Hollanders\"");
    setLayout(new BorderLayout());
    setSize(1024, 768);

    frame = this;

    Dienstregeling dienstregeling = maakDienstregeling();

    schaal = new Scrollbar();
    schaal.setMaximum(100);
    schaal.setMinimum(1);
    schaal.setOrientation(Scrollbar.HORIZONTAL);
    schaal.setValue(45);
    schaal.setPreferredSize(new Dimension(250, 20));
    redrawButton = new Button("Redraw!");
    left = new BasicArrowButton(BasicArrowButton.WEST);
    right = new BasicArrowButton(BasicArrowButton.EAST);

    traffic = new GlobalTraffic(dienstregeling);
    dienst = new DienstregelingCanvas(dienstregeling);

    schaal.addAdjustmentListener(
        new AdjustmentListener() {
          // Houdt bij of er aan de scrollbar gezet is of niet
          public void adjustmentValueChanged(AdjustmentEvent e) {
            double scaleFactor = (double) schaal.getValue() / 100;
            dienst.setScaleFactor(scaleFactor);
            frame.redraw(200);
          }
        });
    redrawButton.addActionListener(
        new ActionListener() {
          // Houdt bij of er op de knop gedrukt is
          public void actionPerformed(ActionEvent evt) {
            if (evt.getSource() == redrawButton) {
              Dienstregeling dienstregeling = maakDienstregeling();
              traffic.setDienstregeling(dienstregeling);
              dienst.setDienstregeling(dienstregeling);
              frame.redraw(200);
            }
          }
        });

    left.addActionListener(
        new ActionListener() {
          // Houdt bij of er op de knop gedrukt is
          public void actionPerformed(ActionEvent evt) {
            if (evt.getSource() == left) {
              int val = schaal.getValue();
              schaal.setValue(val - 1);
              double scaleFactor = (double) schaal.getValue() / 100;
              dienst.setScaleFactor(scaleFactor);
              frame.redraw(200);
            }
          }
        });

    right.addActionListener(
        new ActionListener() {
          // Houdt bij of er op de knop gedrukt is
          public void actionPerformed(ActionEvent evt) {
            if (evt.getSource() == right) {
              int val = schaal.getValue();
              schaal.setValue(val + 1);
              double scaleFactor = (double) schaal.getValue() / 100;
              dienst.setScaleFactor(scaleFactor);
              frame.redraw(200);
            }
          }
        });

    Panel buttonPane = new Panel();
    buttonPane.setLayout(new FlowLayout());
    buttonPane.add(left);
    buttonPane.add(schaal);
    buttonPane.add(right);
    buttonPane.add(redrawButton);

    buttonPane.setPreferredSize(new Dimension(1024, 40));

    this.add(buttonPane, BorderLayout.SOUTH);

    JPanel topPanel = new JPanel();
    topPanel.add(traffic, BorderLayout.CENTER);
    traffic.setLocation(frame.getWidth() / 2, 0);

    this.add(topPanel, BorderLayout.NORTH);
    this.add(dienst, BorderLayout.CENTER);

    // Exit the program when the window is closed
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Close the window when escape is pressed
    this.addKeyListener(
        new KeyAdapter() {
          @Override
          public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
              System.exit(0);
            }
          }
        });

    mouseX = 0;
    mouseY = 0;

    this.addMouseMotionListener(
        new MouseMotionAdapter() {

          @Override
          public void mouseMoved(MouseEvent e) {
            super.mouseMoved(e);
            mouseX = (e.getX());
            mouseY = (e.getY()) - 25; // offset the 25px top bar

            mouseposition.setText(String.format("Mouse: %d, %d", mouseX, mouseY));
          }
        });

    mouseposition = new JLabel("Mouse:");
    buttonPane.add(mouseposition);

    // Resize the window
    pack();

    setVisible(true);
    frame.redraw(200);
  }