Files
artloglaby/java/org/artisanlogiciel/games/maze/gui/HexagonCellRenderer.java
philippe lhardy 0c886f0cd1 Square or Hexagon Cell
- allows to select between square and hexagon representation
 - remark : this is still a square model
2020-12-28 15:09:54 +01:00

97 lines
2.4 KiB
Java

package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import java.awt.*;
/**
* draw cell as an hexagon
* using a square model ( LEFT UP only).
*/
public class HexagonCellRenderer
extends MazeCellRenderer
{
public final static int SUBCELL = 4;
public final static int SUBCELLY = 3;
public HexagonCellRenderer(WidthHeightProvider model, WidthHeightProvider frame, int x, int y) {
super(model, frame, x, y);
}
void drawLine(Graphics g, int refx, int refy, int x, int y, int a, int b) {
g.drawLine(
(int) ((refx + x) * width / SUBCELL), (int) ((refy + y) * height / SUBCELL),
(int) ((refx + a) * width / SUBCELL),(int) ((refy + b) * height / SUBCELL));
}
@Override
public void drawBackground(Graphics g, int pX, int pY, short walls) {
UV uv= new UV(pX,pY);
int x = offsetX + (int) (uv.getX() * width / SUBCELL);
int y = offsetY + (int) (uv.getY() * height / SUBCELL);
Color savecolor = g.getColor();
int greylevel = walls << 2;
g.setColor(getGreyedColor(savecolor,greylevel));
//g.fillRect(x+1,y+1,((int)width) -1,((int)height) - 1);
g.fillRect(x,y,((int)width),((int)height));
}
class UV
{
int u;
int v;
UV(int u, int v)
{
this.u=u;
this.v=v;
}
int getX()
{
return u*4 + 2*(v%2);
}
int getY()
{
return 3*v;
}
}
@Override
public void drawWalls(Graphics g, int pX, int pY, short walls) {
drawHalfNW(g, pX, pY, walls);
}
void drawHalfNW(Graphics g, int refx, int refy, short walls)
{
UV uv= new UV(refx,refy);
int x = uv.getX();
int y = uv.getY();
// NW : (0,1) - (2,0)
if (Brick.isFlagSet( Brick.UP, walls))
drawLine(g,x, y, 0,1,2,0);
// W : (0,1) - (0,3)
if (Brick.isFlagSet( Brick.LEFT, walls))
drawLine(g,x,y,0,1,0,3);
// NE : (2,0) - (4,1)
// if (Brick.isFlagSet( Brick.NE, walls))
drawLine(g,x,y,2,0,4,1);
}
void drawHalfSW(Graphics g)
{
// SW : (0,3) - (2,4)
// SE : (2,4) - (4,3)
// E : (4,3) - (4,1)
}
@Override
public double getHeight() {
return height * SUBCELLY / SUBCELL;
}
}