示例#1
0
 // färger, se sid 43 i boken
 // grafiska metoder, sid 248 i boken
 public void paintComponent(Graphics g) { // för att vara säker på att
   // "super"-klassen gör sitt
   // anropar vi den metoden, innan
   // vi skriver eller ritar
   super.paintComponent(g);
   g.drawLine(185, 10, 195, 40); // x1,y1 till x2,y2
   g.drawLine(200, 10, 200, 40);
   g.drawLine(215, 10, 205, 40);
   g.setColor(Color.white);
   g.fillOval(50, 30, 300, 150); // x,y,b,h (x,y för ö v h)
   g.setColor(Color.red);
   g.drawArc(100, 100, 200, 50, 180, 180); // x,y,b,h,s,l
   g.setColor(Color.yellow);
   g.fillRect(200, 100, 30, 30);
   g.fill3DRect(150, 50, 30, 50, true); // true upphöjd figur
   g.fill3DRect(250, 50, 30, 50, true);
   // skriv ut en textsträng, samt ange läget i avståndet från
   // övre vänstra hörnet i x-led åt höger och i y-led neråt
   g.drawString("** Tjenare kompis !! **", 20, 20);
   f = new Font("Arial", Font.BOLD, 30);
   setBackground(Color.cyan);
   g.setFont(f);
   g.setColor(new Color(255, 175, 175));
   g.drawString("YEEEEEEEES!!", 100, 250);
 }
示例#2
0
 public void paint(Graphics g) {
   if (f == 1) {
     g.setColor(Color.RED);
     g.drawLine(10, 10, 10, 200);
     g.setColor(Color.green);
     g.fill3DRect(20, 20, 200, 200, true);
   }
 }
示例#3
0
 /** *************************************************************************************** */
 public void drawCell(Graphics g, boolean setPiece) {
   if (color != null) {
     g.setColor(color);
     g.fill3DRect(x, y, CELL_HEIGHT, CELL_WIDTH, true);
   } else {
     g.draw3DRect(x, y, CELL_HEIGHT, CELL_WIDTH, true);
   }
   /*if (p != null)
   {
   	p.drawPiece(g, x, y, setPiece);
   }*/
 }
示例#4
0
  public void paint(Graphics g) {
    Rectangle r = g.getClipBounds();

    showStatus("x: " + r.x + " y: " + r.y + " w: " + r.width + " h: " + r.height);

    for (int i = 0; i < numRects; i++) {
      Point lhc = randomPoint(); // left hand corner
      Dimension size = randomDimension();

      g.setColor(colors[(int) (Math.random() * 10)]);

      if (round) {
        if (fill)
          g.fillRoundRect(
              lhc.x,
              lhc.y,
              size.width,
              size.height,
              (int) (Math.random() * 250),
              (int) (Math.random() * 250));
        else
          g.drawRoundRect(
              lhc.x,
              lhc.y,
              size.width,
              size.height,
              (int) (Math.random() * 250),
              (int) (Math.random() * 250));
      } else if (threeD) {
        g.setColor(Color.lightGray);

        if (fill) g.fill3DRect(lhc.x, lhc.y, size.width, size.height, raise);
        else g.draw3DRect(lhc.x, lhc.y, size.width, size.height, raise);
      } else {
        if (fill) g.fillRect(lhc.x, lhc.y, size.width, size.height);
        else g.drawRect(lhc.x, lhc.y, size.width, size.height);
      }
      raise = raise ? false : true;
    }
  }
示例#5
0
    public void paint(Graphics g) {
      g = this.startPaint(g);
      int width = this.getSize().width;
      int height = this.getSize().height;
      g.clearRect(0, 0, width, height);
      int cell_size, xstart, ystart;
      double panel_aspect_ratio = (double) width / height;
      double grid_aspect_ratio = (double) grid[0].length / grid.length;
      if (panel_aspect_ratio > grid_aspect_ratio) {
        cell_size = (int) ((double) height / grid.length + 0.5);
        xstart = (int) (width / 2 - (grid[0].length / 2.0 * cell_size + 0.5));
        ystart = 0;
      } else {
        cell_size = (int) ((double) width / grid[0].length + 0.5);
        xstart = 0;
        ystart = (int) (height / 2 - (grid.length / 2.0 * cell_size + 0.5));
      }

      if (paint_background) {
        g.setColor(
            BACKGROUND_COLORS[
                (num_rows_deleted / DELETED_ROWS_PER_LEVEL) % BACKGROUND_COLORS.length]);
        g.fillRect(xstart, ystart, COLUMNS * cell_size, ROWS * cell_size);
      }

      for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[0].length; j++) {
          if (grid[i][j] != EMPTY) {
            g.setColor(PIECE_COLORS[grid[i][j]]);
            int x = xstart + j * cell_size;
            int y = ystart + i * cell_size;
            g.fill3DRect(x, y, cell_size, cell_size, true);
          }
        }
      }
      this.endPaint();
    }
示例#6
0
  //	画出坦克的函数
  public void drawTank(int x, int y, Graphics g, int direction, int type) {
    //		判断坦克类型,是敌人的还是我方的坦克,改变坦克的颜色区分
    float lineWidth = 3.0f; // 设置线条为粗线
    switch (type) {
      case 0:
        g.setColor(Color.YELLOW);
        break;

      case 1:
        g.setColor(Color.red);
        break;
    }

    switch (direction) {
      case 0:
        //			1.画出左边履带
        g.fill3DRect(x, y, 5, 30, true);
        //			2.画出右边履带
        g.fill3DRect(x + 20, y, 5, 30, true);
        //			3.画出中间机身
        g.fill3DRect(x + 5, y + 5, 15, 20, false);
        //			4.画出中间圆形
        g.fillOval(x + 5, y + 7, 12, 12);
        //			5.画出炮筒
        ((Graphics2D) g).setStroke(new BasicStroke(lineWidth)); // 设置线条为粗线
        g.drawLine(x + 11, y - 5, x + 11, y + 17);
        break;
      case 1:
        //			1.画出左边履带
        g.fill3DRect(x, y, 30, 5, true);
        //			2.画出右边履带
        g.fill3DRect(x, y + 20, 30, 5, true);
        //			3.画出中间机身
        g.fill3DRect(x + 5, y + 5, 20, 15, false);
        //			4.画出中间圆形
        g.fillOval(x + 7, y + 5, 12, 12);
        //			5.画出炮筒
        ((Graphics2D) g).setStroke(new BasicStroke(lineWidth)); // 设置线条为粗线
        g.drawLine(x + 17, y + 11, x + 32, y + 11);
        break;
      case 2:
        //			1.画出左边履带
        g.fill3DRect(x, y, 5, 30, true);
        //			2.画出右边履带
        g.fill3DRect(x + 20, y, 5, 30, true);
        //			3.画出中间机身
        g.fill3DRect(x + 5, y + 5, 15, 20, false);
        //			4.画出中间圆形
        g.fillOval(x + 5, y + 7, 12, 12);
        //			5.画出炮筒
        ((Graphics2D) g).setStroke(new BasicStroke(lineWidth)); // 设置线条为粗线
        g.drawLine(x + 11, y + 17, x + 11, y + 32);
        break;
      case 3:
        //			1.画出左边履带
        g.fill3DRect(x, y, 30, 5, true);
        //			2.画出右边履带
        g.fill3DRect(x, y + 20, 30, 5, true);
        //			3.画出中间机身
        g.fill3DRect(x + 5, y + 5, 20, 15, false);
        //			4.画出中间圆形
        g.fillOval(x + 7, y + 5, 12, 12);
        //			5.画出炮筒
        ((Graphics2D) g).setStroke(new BasicStroke(lineWidth)); // 设置线条为粗线
        g.drawLine(x + 17, y + 11, x - 5, y + 11);
        break;
    }
  }
示例#7
0
 public void draw(Graphics g) {
   g.setColor(Color.white);
   // zbêdna synchronizacja
   g.fill3DRect(pos, Cwiczenie5_4.SIZE.height - SIZE.height - 1, SIZE.width, SIZE.height, true);
 }
示例#8
0
 public void drawBlock(Graphics g, int r, int c, int mode) {
   Color curr = g.getColor();
   g.setColor(Color.black);
   int inc = 2;
   int xpos = inc + (c * wt), ypos = inc + (r * ht);
   switch (mode) {
     case 1:
       { // drawn on a square with an unclicked
         // bomb
         drawBomb(g, xpos, ypos);
         break;
       }
     case 2:
       { // draws a cell with a number on it
         g.setColor(Color.lightGray);
         g.fillRect(xpos, ypos, wt, ht);
         if (field[r][c] > 0) {
           g.setFont(new Font("Serif", Font.BOLD, 12));
           g.setColor(list[field[r][c] - 1]);
           g.drawString(field[r][c] + "", xpos + 5, ypos + 12);
         }
       }
       break;
     case 3:
       { // drawn on the square where the bomb was clicked
         g.setColor(Color.red);
         g.fillRect(xpos, ypos, wt, ht);
         g.setColor(Color.black);
         drawBomb(g, xpos, ypos);
       }
       break;
     case 4:
       { // draws a plain unrevealed cell
         g.setColor(Color.lightGray);
         g.fill3DRect(xpos, ypos, wt, ht, true);
       }
       break;
     case 5:
       { // a Flag.
         g.setColor(Color.lightGray);
         g.fill3DRect(xpos, ypos, wt, ht, true);
         g.setColor(Color.red);
         g.fillRect(xpos + 4, ypos + 3, 4, 4);
         g.setColor(Color.red.darker());
         g.drawLine(xpos + 8, ypos + 3, xpos + 8, ypos + 6);
         g.drawLine(xpos + 8, ypos + 6, xpos + 5, ypos + 6);
         g.drawLine(xpos + 6, ypos + 5, xpos + 7, ypos + 5);
         g.drawLine(xpos + 7, ypos + 5, xpos + 7, ypos + 4);
         g.setColor(new Color(128, 128, 128));
         g.drawLine(xpos + 5, ypos + 12, xpos + 11, ypos + 12);
         g.drawLine(xpos + 7, ypos + 11, xpos + 9, ypos + 11);
         g.setColor(Color.black);
         g.drawLine(xpos + 8, ypos + 7, xpos + 8, ypos + 11);
         g.drawLine(xpos + 6, ypos + 12, xpos + 10, ypos + 12);
       }
       break;
     case 6:
       { // A red cross - flag over non mine block
         g.setColor(Color.red);
         g.drawLine(xpos + 3, ypos + 3, xpos + 11, ypos + 11);
         g.drawLine(xpos + 4, ypos + 3, xpos + 12, ypos + 11);
         g.drawLine(xpos + 12, ypos + 3, xpos + 4, ypos + 11);
         g.drawLine(xpos + 3, ypos + 11, xpos + 11, ypos + 3);
         g.setColor(Color.red.darker());
         g.drawLine(xpos + 3, ypos + 12, xpos + 4, ypos + 12);
         g.drawLine(xpos + 4, ypos + 12, xpos + 7, ypos + 9);
         g.drawLine(xpos + 8, ypos + 9, xpos + 11, ypos + 12);
         g.drawLine(xpos + 11, ypos + 12, xpos + 12, ypos + 12);
         g.drawLine(xpos + 3, ypos + 4, xpos + 6, ypos + 7);
         g.drawLine(xpos + 12, ypos + 4, xpos + 9, ypos + 7);
       }
       break;
     case 7:
       {
         g.setColor(Color.lightGray);
         g.fillRect(xpos, ypos, wt, ht);
       }
       break;
   }
   g.setColor(curr);
 }
示例#9
0
 public void paintIcon(Component c, Graphics g, int x, int y) {
   Color oldColor = g.getColor();
   g.setColor(color);
   g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true);
   g.setColor(oldColor);
 }