|
| 1 | +package textScreenEmu; |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * @author sirrandalot |
| 6 | + * |
| 7 | + */ |
| 8 | +public class TileImage { |
| 9 | + |
| 10 | + /** |
| 11 | + * The number of horizontal tiles in the image. |
| 12 | + */ |
| 13 | + public final int imageWidth; |
| 14 | + |
| 15 | + /** |
| 16 | + * The number of vertical tiles in the image. |
| 17 | + */ |
| 18 | + public final int imageHeight; |
| 19 | + |
| 20 | + /** |
| 21 | + * The values for all the tiles. |
| 22 | + */ |
| 23 | + protected int[] tileValues; |
| 24 | + |
| 25 | + /** |
| 26 | + * The foreground and background colour values for all the tiles. |
| 27 | + */ |
| 28 | + protected int[][] colourValues; |
| 29 | + |
| 30 | + |
| 31 | + /** |
| 32 | + * Constructor taking the width and height of the image. |
| 33 | + * Creates a totally blank image with default 0 values. |
| 34 | + * @param width The width of the image. |
| 35 | + * @param height The height of the image. |
| 36 | + */ |
| 37 | + public TileImage(int width, int height){ |
| 38 | + this(width, height, new int[width*height], new int[width*height][2]); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a TileImage using the width and height, an array of tile values and |
| 44 | + * an array of background and foreground colour values. |
| 45 | + * @param width The width of the image. |
| 46 | + * @param height The height of the image. |
| 47 | + * @param vals The tile values. |
| 48 | + * @param cols The colour values. |
| 49 | + */ |
| 50 | + public TileImage(int width, int height, int[] vals, int[][] cols){ |
| 51 | + |
| 52 | + //Check if the input values are appropriate |
| 53 | + try{ |
| 54 | + if(width <= 0 || height <= 0) |
| 55 | + throw new Exception("Width and height must be greater than 0."); |
| 56 | + |
| 57 | + if(vals.length != width*height) |
| 58 | + throw new Exception("Wrong number of tile values: " + vals.length + ", must be " + (width*height) + "."); |
| 59 | + |
| 60 | + if(cols.length != width*height) |
| 61 | + throw new Exception("Wrong number of colour values: " + cols.length + ", must be " + (width*height) + "."); |
| 62 | + |
| 63 | + }catch(Exception e){ |
| 64 | + System.out.println("ERROR: " + e); |
| 65 | + e.printStackTrace(); |
| 66 | + } |
| 67 | + |
| 68 | + imageWidth = width; |
| 69 | + imageHeight = height; |
| 70 | + |
| 71 | + tileValues = vals.clone(); |
| 72 | + |
| 73 | + colourValues = new int[width*height][2]; |
| 74 | + for(int i = 0; i < cols.length; i++) |
| 75 | + colourValues[i] = new int[]{cols[i][0], cols[i][1]}; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sets the value and colours of a tile at a given position. |
| 80 | + * @param x The x position of the tile. |
| 81 | + * @param y The y position of the tile. |
| 82 | + * @param val The value to set. |
| 83 | + * @param background The background colour to set. |
| 84 | + * @param foreground The foreground colour to set. |
| 85 | + */ |
| 86 | + public void setTile(int x, int y, int val, int background, int foreground){ |
| 87 | + setTileValue(x, y, val); |
| 88 | + setTileColour(x, y, background, foreground); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sets the value of a tile at a given position. |
| 93 | + * @param x The x position of the tile. |
| 94 | + * @param y The y position of the tile. |
| 95 | + * @param val The value to set. |
| 96 | + */ |
| 97 | + public void setTileValue(int x, int y, int val){ |
| 98 | + |
| 99 | + //Check if the input values are appropriate |
| 100 | + try{ |
| 101 | + |
| 102 | + if(x < 0 || x >= imageWidth) |
| 103 | + throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively."); |
| 104 | + |
| 105 | + if(y < 0 || y >= imageHeight) |
| 106 | + throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively."); |
| 107 | + |
| 108 | + }catch(Exception e){ |
| 109 | + System.out.println("ERROR: " + e); |
| 110 | + e.printStackTrace(); |
| 111 | + } |
| 112 | + |
| 113 | + tileValues[y*imageWidth + x] = val; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets the colours of a tile at a given position. |
| 118 | + * @param x The x position of the tile. |
| 119 | + * @param y The y position of the tile. |
| 120 | + * @param background The background colour to set. |
| 121 | + * @param foreground The foreground colour to set. |
| 122 | + */ |
| 123 | + public void setTileColour(int x, int y, int background, int foreground){ |
| 124 | + |
| 125 | + //Check if the input values are appropriate |
| 126 | + try{ |
| 127 | + |
| 128 | + if(x < 0 || x >= imageWidth) |
| 129 | + throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively."); |
| 130 | + |
| 131 | + if(y < 0 || y >= imageHeight) |
| 132 | + throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively."); |
| 133 | + |
| 134 | + }catch(Exception e){ |
| 135 | + System.out.println("ERROR: " + e); |
| 136 | + e.printStackTrace(); |
| 137 | + } |
| 138 | + |
| 139 | + colourValues[y*imageWidth + x] = new int[]{background, foreground}; |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets the tile value at a specified position. |
| 145 | + * @param x The x position. |
| 146 | + * @param y The y position. |
| 147 | + * @return The tile value. |
| 148 | + */ |
| 149 | + public int getTileValue(int x, int y){ |
| 150 | + |
| 151 | + //Check if the input values are appropriate |
| 152 | + try{ |
| 153 | + |
| 154 | + if(x < 0 || x >= imageWidth) |
| 155 | + throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively."); |
| 156 | + |
| 157 | + if(y < 0 || y >= imageHeight) |
| 158 | + throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively."); |
| 159 | + |
| 160 | + }catch(Exception e){ |
| 161 | + System.out.println("ERROR: " + e); |
| 162 | + e.printStackTrace(); |
| 163 | + } |
| 164 | + |
| 165 | + return tileValues[y*imageWidth + x]; |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + /** |
| 170 | + * Gets the background and foreground colours of a tile ad a specified position. |
| 171 | + * @param x The x position. |
| 172 | + * @param y The y position. |
| 173 | + * @return The background and foreground colours. |
| 174 | + */ |
| 175 | + public int[] getTileColours(int x, int y){ |
| 176 | + |
| 177 | + //Check if the input values are appropriate |
| 178 | + try{ |
| 179 | + |
| 180 | + if(x < 0 || x >= imageWidth) |
| 181 | + throw new Exception("X position " + x + " out of range, must be between 0 and " + (imageWidth-1) + " inclusively."); |
| 182 | + |
| 183 | + if(y < 0 || y >= imageHeight) |
| 184 | + throw new Exception("Y position " + y + " out of range, must be between 0 and " + (imageHeight-1) + " inclusively."); |
| 185 | + |
| 186 | + }catch(Exception e){ |
| 187 | + System.out.println("ERROR: " + e); |
| 188 | + e.printStackTrace(); |
| 189 | + } |
| 190 | + |
| 191 | + return colourValues[y*imageWidth + x].clone(); |
| 192 | + } |
| 193 | +} |
0 commit comments