summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/view/impl/StonePainter.java
blob: e536e32db1f6a06ad1a78b5e4bdbc1a0f2ce30fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package jrummikub.view.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneColor;

class StonePainter {
  private static final float ASPECT_RATIO = 0.75f;
  private static final float DEFAULT_WIDTH = 40;
  private static final float CIRCLE_WIDTH = 0.5f;
  
  private static final Color BACKGROUND_COLOR = new Color(0.9f, 0.9f, 0.6f);
  
  public static final float BOARD_SCALE = 75.0f*ASPECT_RATIO/DEFAULT_WIDTH;
  
  
  private static Color getColor(StoneColor color) {
    switch(color) {
      case BLACK:
        return Color.BLACK;
      case BLUE:
        return Color.BLUE;
      case ORANGE:
        return new Color(1.0f, 0.6f, 0);
      case RED:
        return Color.RED;
    }
    
    return null;
  }
  
  public static void paintStone(Graphics g, Stone stone, Position p, float scale) {
    if (g instanceof Graphics2D) {
      ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
          RenderingHints.VALUE_ANTIALIAS_ON);
    }
    
    int width = (int)(DEFAULT_WIDTH*scale);
    int height = (int)(DEFAULT_WIDTH*scale/ASPECT_RATIO);
    
    int x = (int)(p.getX()*width);
    int y = (int)(p.getY()*height);
    
    // Paint background
    g.setColor(BACKGROUND_COLOR);
    g.fillRect(x, y, width, height);
    
    // Paint bevel border
    g.setColor(BACKGROUND_COLOR.brighter().brighter());
    g.fillRect(x, y, 1, height);
    g.setColor(BACKGROUND_COLOR.brighter());
    g.fillRect(x+1, y+1, 1, height-2);

    g.setColor(BACKGROUND_COLOR.brighter().brighter());
    g.fillRect(x, y, width, 1);
    g.setColor(BACKGROUND_COLOR.brighter());
    g.fillRect(x+1, y+1, width-2, 1);
    
    g.setColor(BACKGROUND_COLOR.darker().darker());
    g.fillRect(x+width-1, y, 1, height);
    g.setColor(BACKGROUND_COLOR.darker());
    g.fillRect(x+width-2, y+1, 1, height-2);
    
    g.setColor(BACKGROUND_COLOR.darker().darker());
    g.fillRect(x, y+height-1, width, 1);
    g.setColor(BACKGROUND_COLOR.darker());
    g.fillRect(x+1, y+height-2, width-2, 1);
    
    // Paint number
    g.setFont(new Font("SansSerif", Font.BOLD, height/4));
    FontMetrics fm = g.getFontMetrics();
    String value = Integer.toString(stone.getValue()); 
    Rectangle2D stringRect = fm.getStringBounds(value, g);
    
    g.setColor(getColor(stone.getColor()).darker());
    g.drawString(value, (int)(x+width/2-stringRect.getWidth()/2)+1, y+height/4+(fm.getAscent()-fm.getDescent())/2+1);
    g.setColor(getColor(stone.getColor()));
    g.drawString(value, (int)(x+width/2-stringRect.getWidth()/2), y+height/4+(fm.getAscent()-fm.getDescent())/2);
    
    // Paint circle
    g.setColor(BACKGROUND_COLOR.darker());
    g.drawArc((int)(x+width/2-width*CIRCLE_WIDTH/2), (int)(y+height*0.65f-width*CIRCLE_WIDTH/2), (int)(width*CIRCLE_WIDTH), (int)(width*CIRCLE_WIDTH), 50, 170);
    
    g.setColor(BACKGROUND_COLOR.brighter());
    g.drawArc((int)(x+width/2-width*CIRCLE_WIDTH/2), (int)(y+height*0.65f-width*CIRCLE_WIDTH/2), (int)(width*CIRCLE_WIDTH), (int)(width*CIRCLE_WIDTH), -130, 170);
  }
}