Make board resizable
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@49 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
778a868b9b
commit
41f0d2f6b1
5 changed files with 36 additions and 12 deletions
Binary file not shown.
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 22 KiB |
|
@ -5,6 +5,7 @@ import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.RenderingHints;
|
import java.awt.RenderingHints;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -17,34 +18,53 @@ import jrummikub.view.IBoard;
|
||||||
|
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public class Board extends StonePanel implements IBoard {
|
public class Board extends StonePanel implements IBoard {
|
||||||
private final static ImageIcon BACKGROUND = new ImageIcon(Board.class.getResource("/jrummikub/resource/wood.png"));
|
private final static BufferedImage BACKGROUND;
|
||||||
private final static float DEFAULT_SCALE = StonePainter.BOARD_SCALE;
|
static {
|
||||||
|
ImageIcon image = new ImageIcon(
|
||||||
|
Board.class.getResource("/jrummikub/resource/wood.png"));
|
||||||
|
BACKGROUND = new BufferedImage(image.getIconWidth(), image.getIconHeight(),
|
||||||
|
BufferedImage.TYPE_INT_RGB);
|
||||||
|
|
||||||
|
image.paintIcon(null, BACKGROUND.createGraphics(), 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
private Map<Stone, Position> stones = Collections.emptyMap();
|
private Map<Stone, Position> stones = Collections.emptyMap();
|
||||||
private Collection<Stone> selectedStones = Collections.emptyList();
|
private Collection<Stone> selectedStones = Collections.emptyList();
|
||||||
|
|
||||||
Board() {
|
Board() {
|
||||||
super(DEFAULT_SCALE);
|
|
||||||
|
|
||||||
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
|
setBorder(new CustomBorder(Color.DARK_GRAY, 0, 1, 0, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private BufferedImage getScaledBackground(int size) {
|
||||||
|
BufferedImage scaled = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
|
||||||
|
Graphics2D g = scaled.createGraphics();
|
||||||
|
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
|
||||||
|
|
||||||
|
g.drawImage(BACKGROUND, 0, 0, size, size, null);
|
||||||
|
|
||||||
|
return scaled;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void paintComponent(Graphics g1) {
|
protected void paintComponent(Graphics g1) {
|
||||||
Insets insets = getInsets();
|
Insets insets = getInsets();
|
||||||
int x = insets.left, y = insets.top, width = getWidth()-insets.left-insets.right, height = getHeight()-insets.top-insets.bottom;
|
int x = insets.left, y = insets.top, width = getWidth()-insets.left-insets.right, height = getHeight()-insets.top-insets.bottom;
|
||||||
Graphics2D g = (Graphics2D)g1.create(x, y, width, height);
|
Graphics2D g = (Graphics2D)g1.create(x, y, width, height);
|
||||||
|
int size = height/2;
|
||||||
|
BufferedImage scaledBackground = getScaledBackground(size);
|
||||||
|
|
||||||
for(int xpos = 0; xpos < width; xpos += BACKGROUND.getIconWidth()) {
|
for(int xpos = 0; xpos < width; xpos += size) {
|
||||||
BACKGROUND.paintIcon(this, g, xpos, 0);
|
g.drawImage(scaledBackground, xpos, 0, null);
|
||||||
}
|
}
|
||||||
for(int xpos = -32; xpos < width; xpos += BACKGROUND.getIconWidth()) {
|
for(int xpos = -size/3; xpos < width; xpos += size) {
|
||||||
BACKGROUND.paintIcon(this, g, xpos, 75);
|
g.drawImage(scaledBackground, xpos, size, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
||||||
RenderingHints.VALUE_ANTIALIAS_ON);
|
RenderingHints.VALUE_ANTIALIAS_ON);
|
||||||
|
|
||||||
|
getStonePainter().setScale(size*StonePainter.PIXEL_SCALE);
|
||||||
|
|
||||||
for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
|
for (Map.Entry<Stone, Position> entry : stones.entrySet()) {
|
||||||
getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
|
getStonePainter().paintStone(g, entry.getKey(), entry.getValue(),
|
||||||
selectedStones.contains(entry.getKey()));
|
selectedStones.contains(entry.getKey()));
|
||||||
|
|
|
@ -26,7 +26,7 @@ class StonePainter {
|
||||||
|
|
||||||
private static final float BRIGHTER_SCALE = 1.15f;
|
private static final float BRIGHTER_SCALE = 1.15f;
|
||||||
|
|
||||||
public static final float BOARD_SCALE = 75.0f*ASPECT_RATIO/DEFAULT_WIDTH;
|
public static final float PIXEL_SCALE = ASPECT_RATIO/DEFAULT_WIDTH;
|
||||||
|
|
||||||
|
|
||||||
private float scale;
|
private float scale;
|
||||||
|
|
|
@ -22,6 +22,10 @@ abstract class StonePanel extends JPanel implements IClickable {
|
||||||
return stonePainter;
|
return stonePainter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public StonePanel() {
|
||||||
|
this(1);
|
||||||
|
}
|
||||||
|
|
||||||
public StonePanel(float scale) {
|
public StonePanel(float scale) {
|
||||||
super(true); // Set double buffered
|
super(true); // Set double buffered
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ public class View extends JFrame implements IView {
|
||||||
private Table table;
|
private Table table;
|
||||||
private PlayerPanel playerPanel;
|
private PlayerPanel playerPanel;
|
||||||
|
|
||||||
private final static int PLAYER_PANEL_HEIGHT = 150;
|
private final static int PLAYER_PANEL_HEIGHT = 140;
|
||||||
|
|
||||||
|
|
||||||
public ITable getTable() {
|
public ITable getTable() {
|
||||||
|
|
Reference in a new issue