Example #1
0
  public void paint(Graphics g) {
    if (!gameComplete) {
      if (!initialPaintComplete) {
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, MaxX, MaxY);
        initialPaintComplete = true;
      }

      Graphics blockGraphics = blockBuffer.getGraphics();
      Graphics padGraphics = padBuffer.getGraphics();
      Graphics ballGraphics = ballBuffer.getGraphics();
      Graphics ballPreviousGraphics = ballBufferPrevious.getGraphics();

      ballPreviousGraphics.setColor(Color.WHITE);
      ballPreviousGraphics.fillRect(0, 0, ballDiameter, ballDiameter);
      g.drawImage(ballBufferPrevious, ballPreviousX, ballPreviousY, null);

      ballGraphics.setColor(Color.BLUE);
      ballGraphics.fillOval(0, 0, ballDiameter, ballDiameter); // whole line white
      g.drawImage(ballBuffer, ballX, ballY, null);

      ballGraphics.setColor(Color.RED);
      ballGraphics.fillOval(4, 4, ballDiameter - 8, ballDiameter - 8); // whole line white
      g.drawImage(ballBuffer, ballX, ballY, null);

      padGraphics.setColor(Color.WHITE);
      padGraphics.fillRect(0, 0, MaxX, blockHeight); // whole line white
      padGraphics.setColor(Color.BLACK);
      padLeft = padx - padLength / 2;

      if (padLeft >= 0 && padx + padLength / 2 < MaxX) {
        padGraphics.fillRoundRect(padLeft, 0, padLength, padHeight, padHeight, padHeight);
      }
      if (padLeft < 0) {
        padGraphics.fillRoundRect(0, 0, padLength, padHeight, padHeight, padHeight);
      } else if (padx + padLength / 2 >= MaxX) {
        padGraphics.fillRoundRect(MaxX - padLength, 0, padLength, padHeight, padHeight, padHeight);
      }
      g.drawImage(padBuffer, 0, padTop, null);
      // g.drawString(msg, 50, 50);
      // Drawing Blocks

      Iterator<Entry<String, Color>> it = blockMap.entrySet().iterator();
      blockGraphics.setColor(Color.WHITE);
      blockGraphics.fillRect(0, 0, blockSetWidth, blockSetHeight); // whole line white
      while (it.hasNext()) {
        Map.Entry<String, Color> pairs = (Map.Entry<String, Color>) it.next();
        String coordinate = pairs.getKey();
        blockGraphics.setColor(pairs.getValue());
        blockGraphics.fillRect(
            new Pair(coordinate).first, new Pair(coordinate).second, blockLength, blockHeight);
      }
      if (repaintBlocks) {
        g.drawImage(blockBuffer, blockStartX, blockStartY, null);
        if (--count < 0) repaintBlocks = false;
      }
    }
  }
 @Override
 public void paintComponent(Graphics g) {
   if (selected) {
     g.setColor(selColor);
     g.fillRoundRect(0, 0, getWidth(), getHeight(), getWidth() / 10, getHeight() / 10);
     label.setForeground(Color.YELLOW);
   } else label.setForeground(Color.WHITE);
   super.paintComponent(g);
 }