예제 #1
0
  public void paintComponent(Graphics g) throws RuntimeException {
    int squarewidth = getSquareWidth();
    int squareheight = getSquareHeight();

    int hoffset = getHorizontalOffset();
    int voffset = getVerticalOffset();

    for (int x = 0; x < grid.maxX(); x++) {
      for (int y = 0; y < grid.maxY(); y++) {
        Cell tmp = grid.grabObjectAt(x, y);

        if (tmp == null) {
          g.setColor(Color.gray.darker());
        } else {
          g.setColor(tmp.getColor());
        }

        g.fillRect(
            hoffset + x * squarewidth,
            voffset + ((grid.maxY() - y) * squareheight),
            squarewidth - 1,
            squareheight - 1);
      }
    }
  }
  /** Method declaration Adjust this method for large strings...ie multi megabtypes. */
  void execute() {

    String sCmd = null;

    if (4096 <= ifHuge.length()) {
      sCmd = ifHuge;
    } else {
      sCmd = txtCommand.getText();
    }

    if (sCmd.startsWith("-->>>TEST<<<--")) {
      testPerformance();

      return;
    }

    String g[] = new String[1];

    lTime = System.currentTimeMillis();

    try {
      sStatement.execute(sCmd);

      lTime = System.currentTimeMillis() - lTime;

      int r = sStatement.getUpdateCount();

      if (r == -1) {
        formatResultSet(sStatement.getResultSet());
      } else {
        g[0] = "update count";

        gResult.setHead(g);

        g[0] = String.valueOf(r);

        gResult.addRow(g);
      }

      addToRecent(txtCommand.getText());
    } catch (SQLException e) {
      lTime = System.currentTimeMillis() - lTime;
      g[0] = "SQL Error";

      gResult.setHead(g);

      String s = e.getMessage();

      s += " / Error Code: " + e.getErrorCode();
      s += " / State: " + e.getSQLState();
      g[0] = s;

      gResult.addRow(g);
    }

    updateResult();
    System.gc();
  }
예제 #3
0
 public void startScan(String mode) {
   pathfindStart = System.currentTimeMillis();
   if (!gridSet) {
     grid.clearScan();
     tracing = false;
     if (grid.scanStart(mode)) {
       gridSet = true;
     }
   }
 }
  /** Update the status of the GUI based on any changes in the game object. */
  private void updateStatus() {
    if (game.donePlacingShips()) {
      // Kill the ship highlight and disable the vertical/horizontal buttons.
      humanGrid.setHoverShape(null);
      hButton.setEnabled(false);
      vButton.setEnabled(false);
    } else {
      int len = game.placingLength();
      boolean horiz = game.placingHorizontal();
      humanGrid.setHoverShape(new Dimension(horiz ? len : 1, horiz ? 1 : len));
    }

    message.setText(game.getStatus());
    repaint();
  }
예제 #5
0
  @Override
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D gfx = (Graphics2D) g;
    gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Clear screen
    gfx.setColor(Constants.BACKGROUND_COLOR);
    gfx.fillRect(0, 0, getWidth(), getHeight());
    // Render next frame
    grid.draw(gfx);
    // Trace path line
    if (tracing) {
      gfx.setColor(Constants.PATH_COLOR);
      gfx.setStroke(new BasicStroke(2));
      for (int i = 1; i < pathLine.size(); i++) {
        Coordinate p = pathLine.get(i - 1);
        Coordinate n = pathLine.get(i);
        gfx.drawLine(
            (Constants.TILESIZE + Constants.MARGIN) * p.x
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN,
            (Constants.TILESIZE + Constants.MARGIN) * p.y
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN,
            (Constants.TILESIZE + Constants.MARGIN) * n.x
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN,
            (Constants.TILESIZE + Constants.MARGIN) * n.y
                + (Constants.TILESIZE / 2)
                + Constants.MARGIN);
      }
    }
  }
예제 #6
0
파일: Knight.java 프로젝트: Ceasar/JChess
 /**
  * Generates the Knight's moveset by looping clockwise. First take two steps in a certain
  * direction to create a baseLocation, and then go to the right and left of the baseLocation to
  * check for possible moves.
  */
 public ArrayList<Location> getValidMoveLocations() {
   Grid<Actor> gr = getGrid();
   if (gr == null) {
     return null;
   }
   ArrayList<Location> result = new ArrayList<Location>();
   Location possibleLocation1, possibleLocation2;
   for (int i = 0; i < Location.FULL_CIRCLE; i += Location.RIGHT) {
     Location baseLocation = getLocation();
     for (int counter = 0; counter < 2; counter++) {
       baseLocation = baseLocation.getAdjacentLocation(i);
     }
     possibleLocation1 = baseLocation.getAdjacentLocation(i + Location.LEFT);
     if (gr.isValid(possibleLocation1)) {
       result.add(possibleLocation1);
       if (gr.get(possibleLocation1) instanceof Piece
           && gr.get(possibleLocation1).getColor().equals(getColor())) {
         result.remove(possibleLocation1);
       }
     }
     possibleLocation2 = baseLocation.getAdjacentLocation(i + Location.RIGHT);
     if (gr.isValid(possibleLocation2)) {
       result.add(possibleLocation2);
       if (gr.get(possibleLocation2) instanceof Piece
           && gr.get(possibleLocation2).getColor().equals(getColor())) {
         result.remove(possibleLocation2);
       }
     }
   }
   return result;
 }
예제 #7
0
  protected int getSquareHeight() {
    int sheight = getRealHeight() / (grid.maxY() + 1);

    if (sheight > 0) {
      return sheight;
    } else {
      return 1;
    }
  }
예제 #8
0
  protected int getSquareWidth() {
    int swidth = getRealWidth() / (grid.maxX() + 1);

    if (swidth > 0) {
      return swidth;
    } else {
      return 1;
    }
  }
  /**
   * Method declaration
   *
   * @param r
   */
  void formatResultSet(ResultSet r) {

    if (r == null) {
      String g[] = new String[1];

      g[0] = "Result";

      gResult.setHead(g);

      g[0] = "(empty)";

      gResult.addRow(g);

      return;
    }

    try {
      ResultSetMetaData m = r.getMetaData();
      int col = m.getColumnCount();
      String h[] = new String[col];

      for (int i = 1; i <= col; i++) {
        h[i - 1] = m.getColumnLabel(i);
      }

      gResult.setHead(h);

      while (r.next()) {
        for (int i = 1; i <= col; i++) {
          h[i - 1] = r.getString(i);

          if (r.wasNull()) {
            h[i - 1] = "(null)";
          }
        }

        gResult.addRow(h);
      }

      r.close();
    } catch (SQLException e) {
    }
  }
예제 #10
0
    private void _remove() {
      this.parent.height--;
      if (prevRow == null) {
        parent.firstRow = nextRow;
      } else {
        this.prevRow.nextRow = nextRow;
      }
      if (nextRow == null) {
        parent.lastRow = prevRow;
      } else {
        this.nextRow.prevRow = prevRow;
      }

      this.firstCell = null;
      this.lastCell = null;
      this.prevRow = null;
      this.nextRow = null;
      this.parent = null;
    }
  /** Method declaration */
  private void initGUI() {

    Panel pQuery = new Panel();
    Panel pCommand = new Panel();

    pResult = new Panel();

    pQuery.setLayout(new BorderLayout());
    pCommand.setLayout(new BorderLayout());
    pResult.setLayout(new BorderLayout());

    Font fFont = new Font("Dialog", Font.PLAIN, 12);

    txtCommand = new TextArea(5, 40);

    txtCommand.addKeyListener(this);

    txtResult = new TextArea(20, 40);

    txtCommand.setFont(fFont);
    txtResult.setFont(new Font("Courier", Font.PLAIN, 12));

    butExecute = new Button("Execute");
    butClear = new Button("Clear");

    butExecute.addActionListener(this);
    butClear.addActionListener(this);
    pCommand.add("East", butExecute);
    pCommand.add("West", butClear);
    pCommand.add("Center", txtCommand);

    gResult = new Grid();

    setLayout(new BorderLayout());
    pResult.add("Center", gResult);
    pQuery.add("North", pCommand);
    pQuery.add("Center", pResult);
    fMain.add("Center", pQuery);

    tTree = new Tree();

    // (ulrivo): screen with less than 640 width
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    if (d.width >= 640) {
      tTree.setMinimumSize(new Dimension(200, 100));
    } else {
      tTree.setMinimumSize(new Dimension(80, 100));
    }

    gResult.setMinimumSize(new Dimension(200, 300));
    fMain.add("West", tTree);
    doLayout();
    fMain.pack();
  }
예제 #12
0
 public Row<T> insertRowAbove() {
   Row<T> newRow = new Row<T>(parent, prevRow, this);
   if (prevRow == null) {
     parent.firstRow = newRow;
   } else {
     getPrevRow().nextRow = newRow;
   }
   parent.height++;
   prevRow = newRow;
   return newRow;
 }
예제 #13
0
 public Row<T> insertRowBeneath() {
   Row<T> newRow = new Row<T>(parent, this, nextRow);
   if (nextRow == null) {
     parent.lastRow = newRow;
   } else {
     getNextRow().prevRow = newRow;
   }
   parent.height++;
   nextRow = newRow;
   return newRow;
 }
 public void actionPerformed(ActionEvent e) {
   if (e.getSource().equals(save)) new GridSaver(grid, fileName());
   if (e.getSource().equals(load)) {
     GridLoader gl = new GridLoader(grid, fileName());
     try {
       grid.setMap(gl.read());
     } catch (IOException ioex) {
       System.out.println("File load Failed ioex");
     }
   }
   parent.getParent().refresh();
 }
  /** Method declaration */
  void updateResult() {

    if (iResult == 0) {

      // in case 'help' has removed the grid
      if (bHelp) {
        pResult.removeAll();
        pResult.add("Center", gResult);
        pResult.doLayout();

        bHelp = false;
      }

      gResult.update();
      gResult.repaint();
    } else {
      showResultInText();
    }

    txtCommand.selectAll();
    txtCommand.requestFocus();
  }
예제 #16
0
파일: Knight.java 프로젝트: Ceasar/JChess
 /** Helper method in order to check for the legality of certain moves made by a King Piece. */
 public ArrayList<Location> getAttackZones() {
   Grid<Actor> gr = getGrid();
   if (gr == null) {
     return null;
   }
   ArrayList<Location> result = new ArrayList<Location>();
   Location possibleLocation1, possibleLocation2;
   for (int i = 0; i < Location.FULL_CIRCLE; i += Location.RIGHT) {
     Location baseLocation = getLocation();
     for (int counter = 0; counter < 2; counter++) {
       baseLocation = baseLocation.getAdjacentLocation(i);
     }
     possibleLocation1 = baseLocation.getAdjacentLocation(i + Location.LEFT);
     if (gr.isValid(possibleLocation1)) {
       result.add(possibleLocation1);
     }
     possibleLocation2 = baseLocation.getAdjacentLocation(i + Location.RIGHT);
     if (gr.isValid(possibleLocation2)) {
       result.add(possibleLocation2);
     }
   }
   return result;
 }
예제 #17
0
    public boolean tryInterleaveWith(Row<T> other) {
      // System.out.println("Try to interleave " + this);
      // System.out.print("             with " + other);
      if (!isInterleaveableWith(other)) {
        // System.out.println(": failed");
        return false;
      }

      Iterator<Cell<T>> oIt = other.iterator();
      for (Cell<T> c : this) {
        Cell<T> oC = oIt.next();
        if (c.isFilled()) {
          if (oC.prevCell == null) {
            oC.parent.firstCell = c;
          } else {
            oC.prevCell.nextCell = c;
          }
          if (oC.nextCell == null) {
            oC.parent.lastCell = c;
          } else {
            oC.nextCell.prevCell = c;
          }

          c.prevCell = oC.prevCell;
          c.nextCell = oC.nextCell;
          c.parent = oC.parent;
          oC.nextCell = null;
          oC.prevCell = null;
          oC.parent = null;
        } else if (c.isUnpackable()) {
          oC.setPackable(false);
        }
      }
      this._remove();
      // System.out.println(": done");
      return true;
    }
예제 #18
0
  public void run() {
    long start, end, sleepTime;

    while (running) {
      start = System.currentTimeMillis();
      if (gridSet) {
        grid.scanStep();
      }
      repaint();
      end = System.currentTimeMillis();
      // Sleep to match FPS limit
      sleepTime = (1000 / framerate) - (end - start);
      if (sleepTime > 0) {
        try {
          Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
          thread.interrupt();
        }
      }
    }
  }
예제 #19
0
  public Cell getObjectAt(int x, int y) {
    int grid_x = (x - getHorizontalOffset()) / getSquareWidth();
    int grid_y = Math.abs(((y - getVerticalOffset()) / getSquareHeight()) - grid.maxY());

    return grid.getObjectAt(grid_x, grid_y);
  }
  /** Method declaration */
  void showResultInText() {

    String col[] = gResult.getHead();
    int width = col.length;
    int size[] = new int[width];
    Vector data = gResult.getData();
    String row[];
    int height = data.size();

    for (int i = 0; i < width; i++) {
      size[i] = col[i].length();
    }

    for (int i = 0; i < height; i++) {
      row = (String[]) data.elementAt(i);

      for (int j = 0; j < width; j++) {
        int l = row[j].length();

        if (l > size[j]) {
          size[j] = l;
        }
      }
    }

    StringBuffer b = new StringBuffer();

    for (int i = 0; i < width; i++) {
      b.append(col[i]);

      for (int l = col[i].length(); l <= size[i]; l++) {
        b.append(' ');
      }
    }

    b.append(NL);

    for (int i = 0; i < width; i++) {
      for (int l = 0; l < size[i]; l++) {
        b.append('-');
      }

      b.append(' ');
    }

    b.append(NL);

    for (int i = 0; i < height; i++) {
      row = (String[]) data.elementAt(i);

      for (int j = 0; j < width; j++) {
        b.append(row[j]);

        for (int l = row[j].length(); l <= size[j]; l++) {
          b.append(' ');
        }
      }

      b.append(NL);
    }

    b.append(NL + height + " row(s) in " + lTime + " ms");
    txtResult.setText(b.toString());
  }
  /** Method declaration */
  void testPerformance() {

    String all = txtCommand.getText();
    StringBuffer b = new StringBuffer();
    long total = 0;

    for (int i = 0; i < all.length(); i++) {
      char c = all.charAt(i);

      if (c != '\n') {
        b.append(c);
      }
    }

    all = b.toString();

    String g[] = new String[4];

    g[0] = "ms";
    g[1] = "count";
    g[2] = "sql";
    g[3] = "error";

    gResult.setHead(g);

    int max = 1;

    lTime = System.currentTimeMillis() - lTime;

    while (!all.equals("")) {
      int i = all.indexOf(';');
      String sql;

      if (i != -1) {
        sql = all.substring(0, i);
        all = all.substring(i + 1);
      } else {
        sql = all;
        all = "";
      }

      if (sql.startsWith("--#")) {
        max = Integer.parseInt(sql.substring(3));

        continue;
      } else if (sql.startsWith("--")) {
        continue;
      }

      g[2] = sql;

      long l = 0;

      try {
        l = DatabaseManagerCommon.testStatement(sStatement, sql, max);
        total += l;
        g[0] = String.valueOf(l);
        g[1] = String.valueOf(max);
        g[3] = "";
      } catch (SQLException e) {
        g[0] = g[1] = "n/a";
        g[3] = e.toString();
      }

      gResult.addRow(g);
      System.out.println(l + " ms : " + sql);
    }

    g[0] = "" + total;
    g[1] = "total";
    g[2] = "";

    gResult.addRow(g);

    lTime = System.currentTimeMillis() - lTime;

    updateResult();
  }
예제 #22
0
 protected int getHorizontalOffset() {
   int offset = (getRealWidth() - (getSquareWidth() * (grid.maxX()))) / 2;
   return offset;
 }
예제 #23
0
 public void clear() {
   if (!gridSet) {
     grid.clearAll();
     tracing = false;
   }
 }
예제 #24
0
  private void majgrid1(Player player) {
    gridtest = new Grid();
    gridtest = player.getGridJoueur();

    ShipType type = ShipType.AIRCRAFT_CARRIER;
    Ship ship = new Ship(type);
    Orientation orient = Orientation.HORIZONTAL;
    for (int i = 0; i < 10; i++) // ligne
    {
      for (int j = 0; j < 10; j++) // colonne
      {
        if (gridtest.getCell(i, j) instanceof OceanCell) {
          tabbout1[i][j].setIcon(iconeau);
        }
        //                else if(gridtest.getCell(i, j) instanceof TouchOceanCell)
        //                {
        //                    tabbout1[i][j].setIcon(iconeautouch);
        //                }
        //                else if(gridtest.getCell(i, j) instanceof TouchOceanCell)
        //                {
        //                    tabbout1[i][j].setIcon(icondebristouch);
        //                }
        else if (gridtest.getCell(i, j) instanceof ShipCell) {
          ship = ((ShipCell) gridtest.getCell(i, j)).getShip();
          int size = ship.getSize();
          orient = ((ShipCell) gridtest.getCell(i, j)).getOrient();

          if (orient.isHorizontal()) {
            if (ship.getSize() == 2) {
              tabbout1[i][j].setIcon(iconsub1h);
              tabbout1[i][j + 1].setIcon(iconsub2h);
              tabbout1[i][j + 2].setIcon(iconsub3h);
              j = j + 2;

            } else if (ship.getSize() == 3) // battleship
            {
              tabbout1[i][j].setIcon(iconbattle1h);
              tabbout1[i][j + 1].setIcon(iconbattle2h);
              tabbout1[i][j + 2].setIcon(iconbattle3h);
              tabbout1[i][j + 3].setIcon(iconbattle4h);
              j = j + 3;
            } else {
              tabbout1[i][j].setIcon(iconair1h);
              tabbout1[i][j + 1].setIcon(iconair2h);
              tabbout1[i][j + 2].setIcon(iconair3h);
              tabbout1[i][j + 3].setIcon(iconair4h);
              tabbout1[i][j + 4].setIcon(iconair5h);
              j = j + 4;
            }
          }
        }
      }

      for (int i2 = 0; i2 < 10; i2++) // ligne
      {
        for (int j2 = 0; j2 < 10; j2++) // colonne
        {
          if (gridtest.getCell(i2, j2) instanceof ShipCell) {
            ship = ((ShipCell) gridtest.getCell(i2, j2)).getShip();
            orient = ((ShipCell) gridtest.getCell(i2, j2)).getOrient();

            if (!orient.isHorizontal()) {
              if (ship.getSize() == 2) {
                tabbout1[i2][j2].setIcon(iconsub1v);
                tabbout1[i2 + 1][j2].setIcon(iconsub2v);
                tabbout1[i2 + 2][j2].setIcon(iconsub3v);
                i2 = i2 + 2;
              } else if (ship.getSize() == 3) // battleship
              {
                tabbout1[i2][j2].setIcon(iconbattle1v);
                tabbout1[i2 + 1][j2].setIcon(iconbattle2v);
                tabbout1[i2 + 2][j2].setIcon(iconbattle3v);
                tabbout1[i2 + 3][j2].setIcon(iconbattle4v);
                i2 = i2 + 3;
              } else {
                tabbout1[i2][j2].setIcon(iconair1v);
                tabbout1[i2 + 1][j2].setIcon(iconair2v);
                tabbout1[i2 + 2][j2].setIcon(iconair3v);
                tabbout1[i2 + 3][j2].setIcon(iconair4v);
                tabbout1[i2 + 4][j2].setIcon(iconair5v);
                i2 = i2 + 4;
              }
            }
          }
        }
      }
    }
  }
예제 #25
0
 protected int getVerticalOffset() {
   int offset = (getRealHeight() - (getSquareHeight() * (grid.maxY() + 2))) / 2;
   return offset;
 }