Gridarta Editor
AbstractSimpleIsoMapRenderer.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.map.renderer;
21 
22 import java.awt.Color;
23 import java.awt.Graphics;
24 import java.awt.Graphics2D;
25 import java.awt.Point;
26 import java.awt.Rectangle;
27 import java.awt.image.BufferedImage;
34 import net.sf.gridarta.utils.Size2D;
35 import org.jetbrains.annotations.NotNull;
36 
41 public abstract class AbstractSimpleIsoMapRenderer<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> extends AbstractMapRenderer<G, A, R> {
42 
46  private static final long serialVersionUID = 1;
47 
51  @NotNull
52  private static final Color BACKGROUND_COLOR = new Color(255, 255, 255, 0);
53 
57  @NotNull
58  private final MapModel<G, A, R> mapModel;
59 
63  @NotNull
65 
69  @NotNull
70  private final Point origin = new Point();
71 
78  super(mapModel, null);
79  this.mapModel = mapModel;
80  this.isoMapSquareInfo = isoMapSquareInfo;
81  }
82 
87  private void paintComponent2(@NotNull final Graphics2D graphics) {
88  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
89  final Rectangle rec = new Rectangle(0, 0, mapSize.getWidth(), mapSize.getHeight());
90  final Point pos = new Point();
91  final Point endPos = rec.getLocation();
92  endPos.translate(rec.width, rec.height);
93  for (pos.y = rec.y; pos.y < endPos.y; pos.y++) {
94  int xStart = origin.x - (pos.y - rec.x + 1) * isoMapSquareInfo.getXLen2();
95  int yStart = origin.y + (pos.y + rec.x) * isoMapSquareInfo.getYLen2();
96  for (pos.x = rec.x; pos.x < endPos.x; pos.x++) {
97  if (graphics.hitClip(xStart, yStart - isoMapSquareInfo.getYLen() * 4, isoMapSquareInfo.getXLen(), isoMapSquareInfo.getYLen() * 5)) {
99  if (square.isEmpty()) {
100  // empty square: Draw empty square icon if not a pickmap
101  // graphics.drawImage(emptySquareIcon.getImage(), xStart, yStart, emptySquareIcon.getImageObserver());
102  } else {
103  // normal square
104  for (final G node : square) {
105  paint(graphics, xStart, yStart, node);
106  }
107  }
108  }
109  xStart += isoMapSquareInfo.getXLen2();
110  yStart += isoMapSquareInfo.getYLen2();
111  }
112  }
113  }
114 
122  protected abstract void paint(@NotNull Graphics2D graphics2D, int xStart, int yStart, @NotNull G gameObject);
123 
128  private void calculateOrigin() {
129  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
130  origin.setLocation(mapSize.getHeight() * isoMapSquareInfo.getXLen2(), 0);
131  }
132 
133  @Override
134  public boolean getSquareLocationAt(@NotNull final Point point, @NotNull final Point retPoint) {
135  throw new IllegalStateException("operation not supported");
136  }
137 
138  @NotNull
139  @Override
140  public Size2D getImageSize() {
141  throw new IllegalStateException("operation not supported");
142  }
143 
144  @NotNull
145  @Override
146  public BufferedImage getFullImage() {
147  // set map dimensions for iso view
148  calculateOrigin();
149  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
150  final int sum = mapSize.getWidth() + mapSize.getHeight();
151  final int viewWidth = sum * isoMapSquareInfo.getXLen2();
152  final int viewHeight = sum * isoMapSquareInfo.getYLen2();
153 
154  // first create a storing place for the image
155  final BufferedImage image = new BufferedImage(viewWidth, viewHeight, BufferedImage.TYPE_INT_ARGB);
156  final Graphics2D graphics = image.createGraphics();
157  graphics.setColor(BACKGROUND_COLOR);
158  graphics.fillRect(0, 0, viewWidth, viewHeight);
159 
160  // paint the map view into the image
161  paintComponent2(graphics);
162  return image;
163  }
164 
165  @Override
166  public void forceRepaint() {
167  throw new IllegalStateException("operation not supported");
168  }
169 
170  @Override
171  protected void paintComponent(@NotNull final Graphics g) {
172  paintComponent2((Graphics2D) g);
173  }
174 
175  @NotNull
176  @Override
177  public Rectangle getSquareBounds(@NotNull final Point p) {
178  throw new IllegalStateException("unsupported operation");
179  }
180 
181 }
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer
Abstract base class for ISO MapRenderers.
Definition: AbstractSimpleIsoMapRenderer.java:41
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.mapModel
final MapModel< G, A, R > mapModel
The MapModel to render.
Definition: AbstractSimpleIsoMapRenderer.java:58
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AbstractSimpleIsoMapRenderer.java:46
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.paintComponent2
void paintComponent2(@NotNull final Graphics2D graphics)
Paints this component.
Definition: AbstractSimpleIsoMapRenderer.java:87
net.sf.gridarta.model.gameobject.IsoMapSquareInfo
Provides information about isometric map squares.
Definition: IsoMapSquareInfo.java:26
net.sf
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.getSquareLocationAt
boolean getSquareLocationAt(@NotNull final Point point, @NotNull final Point retPoint)
Definition: AbstractSimpleIsoMapRenderer.java:134
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.paintComponent
void paintComponent(@NotNull final Graphics g)
Definition: AbstractSimpleIsoMapRenderer.java:171
net.sf.gridarta.model.gameobject.IsoMapSquareInfo.getXLen
int getXLen()
Returns the horizontal size of a square.
Definition: IsoMapSquareInfo.java:66
net.sf.gridarta.gui.map.renderer.AbstractMapRenderer
Abstract base class for classes implementing MapRenderer.
Definition: AbstractMapRenderer.java:45
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.forceRepaint
void forceRepaint()
Definition: AbstractSimpleIsoMapRenderer.java:166
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.model.gameobject.IsoMapSquareInfo.getXLen2
int getXLen2()
Returns the horizontal center of a square.
Definition: IsoMapSquareInfo.java:74
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.AbstractSimpleIsoMapRenderer
AbstractSimpleIsoMapRenderer(@NotNull final MapModel< G, A, R > mapModel, @NotNull final IsoMapSquareInfo isoMapSquareInfo)
Creates a new instance.
Definition: AbstractSimpleIsoMapRenderer.java:77
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.origin
final Point origin
The origin is the point in the north-west corner.
Definition: AbstractSimpleIsoMapRenderer.java:70
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.getImageSize
Size2D getImageSize()
Definition: AbstractSimpleIsoMapRenderer.java:140
net.sf.gridarta.model.gameobject.IsoMapSquareInfo.getYLen
int getYLen()
Returns the vertical size of a square.
Definition: IsoMapSquareInfo.java:82
net.sf.gridarta.model.mapmodel.MapModel.getMapSquare
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.getSquareBounds
Rectangle getSquareBounds(@NotNull final Point p)
Definition: AbstractSimpleIsoMapRenderer.java:177
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.getFullImage
BufferedImage getFullImage()
Definition: AbstractSimpleIsoMapRenderer.java:146
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.model.baseobject.GameObjectContainer.isEmpty
boolean isEmpty()
Check whether this square is empty.
Definition: GameObjectContainer.java:182
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.isoMapSquareInfo
final IsoMapSquareInfo isoMapSquareInfo
The IsoMapSquareInfo to use.
Definition: AbstractSimpleIsoMapRenderer.java:64
net.sf.gridarta.model.gameobject.IsoMapSquareInfo.getYLen2
int getYLen2()
Returns the vertical center of a square.
Definition: IsoMapSquareInfo.java:90
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.BACKGROUND_COLOR
static final Color BACKGROUND_COLOR
The background Color for created images.
Definition: AbstractSimpleIsoMapRenderer.java:52
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.paint
abstract void paint(@NotNull Graphics2D graphics2D, int xStart, int yStart, @NotNull G gameObject)
Paints a single GameObject.
net.sf.gridarta.model.baseobject.GameObjectContainer
Base class for classes that contain GameObjects as children in the sense of containment.
Definition: GameObjectContainer.java:50
net.sf.gridarta.gui.map.renderer.AbstractSimpleIsoMapRenderer.calculateOrigin
void calculateOrigin()
The origin which is located in the NORTH_WEST-corner of the map is calculated.
Definition: AbstractSimpleIsoMapRenderer.java:128
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20