コード例 #1
0
ファイル: PictureBoard.java プロジェクト: wk-fork/xiangqi
  public void paintBoard(Graphics2D g2) {

    double gbwidth = boardGridSize * 8d + lineLoc, gbheight = boardGridSize * 9d + lineLoc;
    g2.setPaint(red);
    g2.draw3DRect((int) lineLoc / 2, (int) lineLoc / 2, (int) gbwidth, (int) gbheight, true);
    g2.draw3DRect(
        (int) lineLoc / 2 + 3, (int) lineLoc / 2 + 3, (int) gbwidth - 6, (int) gbheight - 7, false);
    g2.setPaint(fg);
    for (int i = 0; i < 10; i++) {
      g2.draw(
          new Line2D.Double(
              lineLoc,
              lineLoc + boardGridSize * i,
              lineLoc + boardGridSize * 8,
              lineLoc + boardGridSize * i));
    }
    for (int i = 0; i < 9; i++) {
      g2.draw(
          new Line2D.Double(
              lineLoc + boardGridSize * i,
              lineLoc,
              lineLoc + boardGridSize * i,
              lineLoc + boardGridSize * 4));
      if (i == 0 || i == 8)
        g2.draw(
            new Line2D.Double(
                lineLoc + boardGridSize * i,
                lineLoc + boardGridSize * 4,
                lineLoc + boardGridSize * i,
                lineLoc + boardGridSize * 9));
      else
        g2.draw(
            new Line2D.Double(
                lineLoc + boardGridSize * i,
                lineLoc + boardGridSize * 5,
                lineLoc + boardGridSize * i,
                lineLoc + boardGridSize * 9));
    }
    drawX(lineLoc + boardGridSize * 4, lineLoc + boardGridSize, g2);
    drawX(lineLoc + boardGridSize * 4, lineLoc + boardGridSize * 8, g2);
    // black Pao
    drawLocation(lineLoc + boardGridSize, lineLoc + boardGridSize * 2, center, g2);
    drawLocation(lineLoc + boardGridSize * 7, lineLoc + boardGridSize * 2, center, g2);
    // red Pao
    drawLocation(lineLoc + boardGridSize, lineLoc + boardGridSize * 7, center, g2);
    drawLocation(lineLoc + boardGridSize * 7, lineLoc + boardGridSize * 7, center, g2);
    // black Zu
    drawLocation(lineLoc, lineLoc + boardGridSize * 3, right, g2);
    drawLocation(lineLoc + boardGridSize * 2, lineLoc + boardGridSize * 3, center, g2);
    drawLocation(lineLoc + boardGridSize * 4, lineLoc + boardGridSize * 3, center, g2);
    drawLocation(lineLoc + boardGridSize * 6, lineLoc + boardGridSize * 3, center, g2);
    drawLocation(lineLoc + boardGridSize * 8, lineLoc + boardGridSize * 3, left, g2);
    // red bin
    drawLocation(lineLoc, lineLoc + boardGridSize * 6, right, g2);
    drawLocation(lineLoc + boardGridSize * 2, lineLoc + boardGridSize * 6, center, g2);
    drawLocation(lineLoc + boardGridSize * 4, lineLoc + boardGridSize * 6, center, g2);
    drawLocation(lineLoc + boardGridSize * 6, lineLoc + boardGridSize * 6, center, g2);
    drawLocation(lineLoc + boardGridSize * 8, lineLoc + boardGridSize * 6, left, g2);
    // ----------------------------------------------------------------------
  }
コード例 #2
0
ファイル: PictureBoard.java プロジェクト: wk-fork/xiangqi
 public void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2 = (Graphics2D) g;
   g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   Dimension d = getSize();
   Color fg3D = Color.lightGray;
   g2.setPaint(fg3D);
   g2.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
   g2.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
   g2.setPaint(fg);
   test.setBounds(boardGridSize * 4 + 10, boardGridSize * 5 - 20, 100, 50);
   this.add(test);
   paintBoard(g2);
 }
コード例 #3
0
ファイル: JCanvas.java プロジェクト: xaleth09/Connect4-AI
 // also clip, transform, composite,
 // public boolean isOpaque(){return false;}//theOpaque!=null&&theOpaque;}
 // ---------------------------------------------------------
 private void doPaint(Graphics2D g, int s, Object o) {
   // process an operation from the buffer
   // System.out.println(s);
   Object o1 = null,
       o2 = null,
       o3 = null,
       o4 = null,
       o5 = null,
       o6 = null,
       o7 = null,
       o8 = null,
       o9 = null,
       o10 = null,
       o11 = null;
   if (o instanceof Object[]) {
     Object[] a = (Object[]) o;
     if (a.length > 0) o1 = a[0];
     if (a.length > 1) o2 = a[1];
     if (a.length > 2) o3 = a[2];
     if (a.length > 3) o4 = a[3];
     if (a.length > 4) o5 = a[4];
     if (a.length > 5) o6 = a[5];
     if (a.length > 6) o7 = a[6];
     if (a.length > 7) o8 = a[7];
     if (a.length > 8) o9 = a[8];
     if (a.length > 9) o10 = a[9];
     if (a.length > 10) o11 = a[10];
   }
   switch (s) {
     case clear:
       paintBackground(g, theBackground);
       break;
       // public void addRenderingHints(Map<?,?> hints)
       // {toBuffer("addRenderingHints",hints );}
     case addRenderingHints:
       g.addRenderingHints((Map<?, ?>) o);
       break;
     case clip1:
       g.clip((Shape) o);
       break;
     case draw1:
       g.draw((Shape) o);
       break;
     case draw3DRect:
       g.draw3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5);
       break;
     case drawGlyphVector:
       g.drawGlyphVector((GlyphVector) o1, (Float) o2, (Float) o3);
       break;
     case drawImage1:
       g.drawImage((BufferedImage) o1, (BufferedImageOp) o2, (Integer) o3, (Integer) o4);
       break;
     case drawImage2:
       g.drawImage((Image) o1, (AffineTransform) o2, (ImageObserver) o3);
       break;
     case drawRenderableImage:
       g.drawRenderableImage((RenderableImage) o1, (AffineTransform) o2);
       break;
     case drawRenderedImage:
       g.drawRenderedImage((RenderedImage) o1, (AffineTransform) o2);
       break;
     case drawString1:
       g.drawString((AttributedCharacterIterator) o1, (Float) o2, (Float) o3);
       break;
     case drawString2:
       g.drawString((AttributedCharacterIterator) o1, (Integer) o2, (Integer) o3);
       break;
     case drawString3:
       g.drawString((String) o1, (Float) o2, (Float) o3);
       break;
     case drawString4:
       g.drawString((String) o1, (Integer) o2, (Integer) o3);
       break;
     case fill:
       g.fill((Shape) o);
       break;
     case fill3DRect:
       g.fill3DRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Boolean) o5);
       break;
     case rotate1:
       g.rotate((Double) o);
       break;
     case rotate2:
       g.rotate((Double) o1, (Double) o2, (Double) o3);
       break;
     case scale1:
       g.scale((Double) o1, (Double) o2);
       break;
     case setBackground:
       g.setBackground(
           (Color) o); // paintBackground(g,(Color)o); /*super.setBackground((Color)o) ;*/
       break;
     case setComposite:
       g.setComposite((Composite) o);
       break;
     case setPaint:
       g.setPaint((Paint) o);
       break;
     case setRenderingHint:
       g.setRenderingHint((RenderingHints.Key) o1, o2);
       break;
     case setRenderingHints:
       g.setRenderingHints((Map<?, ?>) o);
       break;
     case setStroke:
       g.setStroke((Stroke) o);
       break;
     case setTransform:
       g.setTransform(makeTransform(o));
       break;
     case shear:
       g.shear((Double) o1, (Double) o2);
       break;
     case transform1:
       g.transform(makeTransform(o));
       break;
     case translate1:
       g.translate((Double) o1, (Double) o2);
       break;
     case translate2:
       g.translate((Integer) o1, (Integer) o2);
       break;
     case clearRect:
       g.clearRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case copyArea:
       g.copyArea(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case drawArc:
       g.drawArc(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case drawBytes:
       g.drawBytes((byte[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5);
       break;
     case drawChars:
       g.drawChars((char[]) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5);
       break;
     case drawImage4:
       g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (Color) o4, (ImageObserver) o5);
       break;
     case drawImage5:
       g.drawImage((Image) o1, (Integer) o2, (Integer) o3, (ImageObserver) o4);
       break;
     case drawImage6:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Color) o6,
           (ImageObserver) o7);
       break;
     case drawImage7:
       g.drawImage(
           (Image) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (ImageObserver) o6);
       break;
     case drawImage8:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Integer) o6,
           (Integer) o7,
           (Integer) o8,
           (Integer) o9,
           (Color) o10,
           (ImageObserver) o11);
       break;
     case drawImage9:
       g.drawImage(
           (Image) o1,
           (Integer) o2,
           (Integer) o3,
           (Integer) o4,
           (Integer) o5,
           (Integer) o6,
           (Integer) o7,
           (Integer) o8,
           (Integer) o9,
           (ImageObserver) o10);
       break;
     case drawLine:
       g.drawLine((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawOval:
       g.drawOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawPolygon1:
       g.drawPolygon((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case drawPolygon2:
       g.drawPolygon((Polygon) o);
       break;
     case drawPolyline:
       g.drawPolyline((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case drawRect:
       g.drawRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case drawRoundRect:
       g.drawRoundRect(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case fillArc:
       g.fillArc(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case fillOval:
       g.fillOval((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
       // {toBuffer("fillPolygon",mkArg(xPoints,  yPoints, nPoints) );}
     case fillPolygon1:
       g.fillPolygon((int[]) o1, (int[]) o2, (Integer) o3);
       break;
     case fillPolygon2:
       g.fillPolygon((Polygon) o);
       break;
     case fillRect:
       g.fillRect((Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4);
       break;
     case fillRoundRect:
       g.fillRoundRect(
           (Integer) o1, (Integer) o2, (Integer) o3, (Integer) o4, (Integer) o5, (Integer) o6);
       break;
     case setClip1:
       g.setClip((Shape) o);
       break;
     case setColor:
       g.setColor((Color) o);
       break;
     case setFont:
       g.setFont((Font) o);
       break;
     case setPaintMode:
       g.setPaintMode();
       break;
     case setXORMode:
       g.setXORMode((Color) o);
       break;
     case opaque:
       super.setOpaque((Boolean) o);
       break;
     case drawOutline: // g.drawString((String)o1, (Integer)o2, (Integer)o3) ;break;
       {
         FontRenderContext frc = g.getFontRenderContext();
         TextLayout tl = new TextLayout((String) o1, g.getFont(), frc);
         Shape s1 = tl.getOutline(null);
         AffineTransform af = g.getTransform();
         g.translate((Integer) o2, (Integer) o3);
         g.draw(s1);
         g.setTransform(af);
       }
       ;
       break;
     default:
       System.out.println("Unknown image operation " + s);
   }
 }
コード例 #4
0
ファイル: Board.java プロジェクト: br-Zhang/Sample-Programs
  /*
   * (non-Javadoc)
   * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
   *
   * g2: used to paint JPanel
   * return: void
   *
   * List of Local Variables:
   * g: Graphics2D object used to paint onto panel
   * skyBlue: Color object used to paint
   * navyBlue: Color object used to paint
   * azure: Color object used to paint
   * purple: Color object used to paint
   * red: Color object used to paint
   * thinStroke: Stroke object used for painting
   * mediumStroke: Stroke object used for painting
   * times: Font object used for drawString
   * heading: Font object used for drawString
   * newAI: AIRecurse object used to find best move
   * x: int variable to hold x co-ordinate of best move
   * y: int variable to hold y co-ordinate of best move
   *
   * Explanation: procedural method to paint onto panel
   */
  public void paintComponent(Graphics g2) {
    // needed to custom paint
    super.paintComponent(g2);

    // cast Graphics to Graphics2D
    // more functions ie 3DRect and some other stuff
    Graphics2D g = (Graphics2D) g2;

    // create colors/strokes/fonts to use
    Color skyBlue = new Color(135, 206, 235);
    Color navyBlue = new Color(0, 0, 128);
    Color azure = new Color(240, 255, 255);
    Color purple = new Color(199, 21, 133);
    Color red = new Color(255, 69, 0);

    Stroke thinStroke = new BasicStroke(3);
    Stroke mediumStroke = new BasicStroke(5);
    Font times = new Font("Times New Roman", Font.BOLD, 60);
    Font heading = new Font("Times New Roman", Font.BOLD, 40); // font for Turn/Victory/Tie Headings

    // draw border
    g.setColor(navyBlue);
    g.setStroke(thinStroke);
    g.drawRect(20, 20, 750, 525);
    g.draw3DRect(20, 20, 750, 525, false);

    // draw player Board
    g.setFont(heading);
    if (player == 1) {
      g.setColor(red);
      if (playerOne != null) {
        if (playerOne.length() > 10) { // take first letters
          g.drawString("P1: " + playerOne.substring(0, 10), 50, 75);
        } else {
          g.drawString("P1: " + playerOne, 50, 75);
        }
      }
    } else {
      g.setColor(purple);
      if (playerOne != null) {
        if (playerOne.length() > 10) {
          g.drawString("P1: " + playerOne.substring(0, 10), 50, 75);
        } else {
          g.drawString("P1: " + playerOne, 50, 75);
        }
      }
    }
    if (player == 2) {
      g.setColor(red);
      if (playerTwo != null) {
        if (playerTwo.length() > 10) {
          g.drawString("P2: " + playerTwo.substring(0, 10), 450, 75);
        } else {
          g.drawString("P2: " + playerTwo, 450, 75);
        }
      }
    } else {
      g.setColor(purple);
      if (playerTwo != null) {
        if (playerTwo.length() > 10) {
          g.drawString("P2: " + playerTwo.substring(0, 10), 450, 75);
        } else {
          g.drawString("P2: " + playerTwo, 450, 75);
        }
      }
    }

    // draw score Board
    g.setColor(skyBlue);
    g.setStroke(thinStroke);
    g.drawRect(50, 235, 145, 75);
    g.draw3DRect(50, 235, 145, 75, false);
    g.setColor(navyBlue);
    g.drawString("P1: " + pointsP1, 57, 285);
    g.setColor(skyBlue);
    g.drawRect(50, 330, 145, 75);
    g.draw3DRect(50, 330, 145, 75, false);
    g.setColor(navyBlue);
    g.drawString("P2: " + pointsP2, 57, 380);

    // draw board
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        g.setColor(skyBlue);
        g.fillRect(225 + 100 * i, 125 + 100 * j, 100, 100);
        g.setStroke(mediumStroke);
        g.setColor(navyBlue);
        g.drawRect(225 + 100 * i, 125 + 100 * j, 100, 100);
        g.draw3DRect(225 + 100 * i, 125 + 100 * j, 100, 100, true);
      }
    }

    // draw buttons box
    g.setColor(purple);
    g.setStroke(thinStroke);
    g.drawRoundRect(560, 125, 100, 265, 25, 25);

    // draw Victory/Tie/Message Box
    g.setColor(skyBlue);
    g.setStroke(thinStroke);
    g.drawRect(50, 140, 145, 75);
    g.draw3DRect(50, 140, 145, 75, false);
    if (checkWin(board) == 1) { // check for player 1 win
      g.setFont(heading); // set font
      g.setColor(navyBlue); // set color
      g.drawString("P1 Win", 57, 190); // victory message
      if (gameOver == false) { // only add one time (and not 129841298410324812341234 per win)
        pointsP1 += 100; // add points
      }
      repaint(); // repaint
      gameOver = true; // set boolean flag to true
    } else if (checkWin(board) == 2) { // check for player 2 win
      g.setFont(heading);
      g.setColor(navyBlue);
      g.drawString("P2 Win", 57, 190);
      if (gameOver == false) {
        pointsP2 += 100;
      }
      repaint();
      gameOver = true;
    } else if (checkTie(board) > 0) { // check for tie
      g.setFont(heading);
      g.setColor(navyBlue);
      g.drawString("Tie!", 57, 190);
      if (gameOver == false) {
        pointsP1 += 25;
        pointsP2 += 25;
      }
      repaint();
      gameOver = true;
    } else { // non finished game
      g.setFont(heading);
      g.setColor(navyBlue);
      if (ifAI1 == false
          && ifAI2 == false) { // show whether its Human VS Human / Human VS AI / AI VS AI
        g.drawString("H vs H", 57, 190);
      } else if (ifAI1 == false && ifAI2 == true) {
        g.drawString("H vs AI", 57, 190);
      } else if (ifAI1 == true && ifAI2 == false) {
        g.drawString("H vs AI", 57, 190);
      } else {
        g.drawString("AI vs AI", 57, 190);
      }
    }

    // draw pieces (X/O)
    g.setFont(times);
    g.setColor(azure);
    for (int y = 0; y < 3; y++) {
      for (int x = 0; x < 3; x++) {
        if (board[y][x] == 1) {
          g.drawString("X", 250 + 100 * x, 200 + 100 * y);
        }
        if (board[y][x] == 2) {
          g.drawString("O", 250 + 100 * x, 200 + 100 * y);
        }
      }
    }

    if (player == 1 && ifAI1 == true || player == 2 && ifAI2 == true) {
      /*
       * Algorithm 1: Initial
       * Straight forward, Play The Game And Code It Like A Player Style
       * Essentially we rank certain moves as move favorable and less favorable
       * 3 in a row is most favorable,
       * Two 2 in a row is next most favorable (guarantees a 3 in a row in 1 move)
       * The idea was simply to program it, like I would play Tic Tac Toe (and I never lose!)
       *

           	AI newAI = new AI();
           	int[] coOrd = newAI.findMove(board, player);
           	int y = coOrd[0];
           	int x = coOrd[1];

           	if (x>-1 && y>-1){
           		board[y][x]= player;
           		if (player==1){ //change player turn
       				player++;
       			}
       			else{
       				player--;
       			}
               	repaint();
           	}
      */

      /*
       * Algorithm 2: Recursive Method
       * So the idea is, instead of playing to win, we can just play to not lose and
       * take wins if we see them
       * The "recursive method" basically ranks "terminal states" ie. win/loss/tie
       * Win is positive, tie = 0, loss is negative
       * And we recursively go through every possible move
       *
       * When tested, this was slower than my first Algorithm.
       * However, since this is a recursion test
       */

      AIRecurse newAI = new AIRecurse();
      if (newAI.checkFirstMove(board)) {
        // Basically, against ideal play, any move is the same really
        // However, through experience from playing tic tac toe in younger grades, I know corners is
        // optimal starting position
        // Doing some mathematics of all possible end game results, show corners leave least room
        // for error for opponent
        // Also I randomize, because watching the same game over and over again is boring
        // Corners are 0,0 2,0 0,2 2,2
        int x = 2 * (int) Math.round((Math.random()));
        int y = 2 * (int) Math.round((Math.random()));
        if (x > -1 && y > -1 && gameOver == false) {
          board[y][x] = player;
          if (player == 1) { // change player turn
            player++;
          } else {
            player--;
          }
          repaint();
        }
      } else {
        int[] coOrd = newAI.findMove(board, player); // int[] coOrd to hold co-ordinates of move
        int y = coOrd[0]; // two variables to hold coordinates
        int x = coOrd[1];
        if (x > -1 && y > -1 && gameOver == false) { // make sure x/y are > -1
          board[y][x] = player; // change board
          if (player == 1) { // change player turn
            player++;
          } else {
            player--;
          }
          repaint(); // re-paint with updated state
        }
      }
    }
  }