without mouse != null i got an java exception mhhmm


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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | import greenfoot.*; import java.awt.Color; public class MyWorld extends World { //RASTER VAR ################################################################################# public final int SIZE_RASTER = 70 ; public final int LENGTH_RASTER_X = 20 ; public final int LENGTH_RASTER_Y = 20 ; private int x_raster_pos = 0 ; private int y_raster_pos = 0 ; private int x_mouse_pos = 0 ; private int y_mouse_pos = 0 ; private int x_raster_off = SIZE_RASTER; private int y_raster_off = SIZE_RASTER; private int x_drag_off = 0 ; private int y_drag_off = 0 ; //Button 3 released? private boolean button3_release = false ; //3 Dimensionales Array zur Speicherung aller Zellen (X,Y,Eigenschaften (Item?,Rotation?,Farbe?)) public int [][][] itemfield = new int [LENGTH_RASTER_X][LENGTH_RASTER_Y][ 3 ]; //Item Images private GreenfootImage[] image_item = new GreenfootImage[ 7 ]; //Item Colors private Color[] item_colorarray = {Color.gray,Color.red,Color.blue,Color.green}; //Item choosen private int item_actual = 1 ; private int item_rotation = 0 ; private int item_color = 0 ; //Mouse Info Variable ########################################################################### private MouseInfo mouse = Greenfoot.getMouseInfo(); public MyWorld() { super ( 700 , 700 , 1 ); mouse = Greenfoot.getMouseInfo(); ItemImageCreate(); ResetItemField(); Raster(); getBackground().setColor(Color.black); DrawRasterPos(); } public void act() { mouse = Greenfoot.getMouseInfo(); getBackground().setColor(Color.white); DrawRasterPos(); getBackground().setColor(Color.white); DrawItem(); Raster(); getBackground().setColor(Color.black); DrawRasterPos(); } //Draw Actual Item ################################################################################## private void ItemAtMouse() { GreenfootImage image_atmouse = new GreenfootImage(SIZE_RASTER,SIZE_RASTER); String key = Greenfoot.getKey(); if (key == "up" )item_actual++; if (key == "down" )item_actual--; if (key == "left" )item_color++; if (key == "right" )item_color--; if (mouse != null && mouse.getButton() == 3 && !Greenfoot.mouseDragEnded( this ) && button3_release == false ) { item_rotation++; if (item_rotation == 4 )item_rotation = 0 ; button3_release = true ; } if (mouse != null && mouse.getButton() != 3 ) { button3_release = false ; } if (mouse != null ) { image_atmouse.drawImage(image_item[item_actual], 0 , 0 ); image_atmouse.rotate(item_rotation * 90 ); if (item_actual != 0 )image_atmouse = ChangeImageColor(image_atmouse, new Color(item_colorarray[item_color].getRed(),item_colorarray[item_color].getGreen(),item_colorarray[item_color].getBlue(), 155 )); getBackground().drawImage(image_atmouse,(x_mouse_pos - x_raster_pos - 1 ) * SIZE_RASTER + x_raster_off,(y_mouse_pos - y_raster_pos - 1 ) * SIZE_RASTER + y_raster_off); image_atmouse.rotate( 360 - item_rotation * 90 ); } if (Greenfoot.mouseClicked( this ) && !Greenfoot.mouseDragEnded( this ) && mouse.getButton() == 1 ) { itemfield[x_mouse_pos][y_mouse_pos][ 0 ] = item_actual; itemfield[x_mouse_pos][y_mouse_pos][ 1 ] = item_rotation; itemfield[x_mouse_pos][y_mouse_pos][ 2 ] = item_color; } } //Item Images Creation ############################################################################## private void ItemImageCreate() { for ( int i = 0 ;i < 7 ;i++) { image_item[i] = new GreenfootImage(SIZE_RASTER,SIZE_RASTER); image_item[i].setColor(Color.black); } //Item_Clear int [] x_clear1 = { 0 , 10 ,SIZE_RASTER,SIZE_RASTER - 10 }; int [] y_clear1 = { 10 , 0 ,SIZE_RASTER - 10 ,SIZE_RASTER}; int [] x_clear2 = {SIZE_RASTER,SIZE_RASTER - 10 , 0 , 10 }; int [] y_clear2 = { 10 , 0 ,SIZE_RASTER - 10 ,SIZE_RASTER}; image_item[ 0 ].fillPolygon(x_clear1,y_clear1, 4 ); image_item[ 0 ].fillPolygon(x_clear2,y_clear2, 4 ); //Item_Full image_item[ 1 ].fill(); //Item_Half image_item[ 2 ].fillRect( 0 ,image_item[ 1 ].getHeight() / 2 ,SIZE_RASTER,SIZE_RASTER / 2 ); //Item_Triangle int [] x_triangle = { 0 ,SIZE_RASTER, 0 }; int [] y_triangle = { 0 ,SIZE_RASTER,SIZE_RASTER}; image_item[ 3 ].setColor(Color.black); image_item[ 3 ].fillPolygon(x_triangle,y_triangle, 3 ); //Item_Slope int [] x_slope = { 0 ,SIZE_RASTER, 0 }; int [] y_slope = {SIZE_RASTER / 2 ,SIZE_RASTER,SIZE_RASTER}; image_item[ 4 ].setColor(Color.black); image_item[ 4 ].fillPolygon(x_slope,y_slope, 3 ); //Item_Cycle image_item[ 5 ].setColor(Color.black); image_item[ 5 ].fillOval(-SIZE_RASTER, 0 ,SIZE_RASTER * 2 ,SIZE_RASTER * 2 ); //Item_Halfpipe for ( int y= 0 ;y < SIZE_RASTER;y++)image_item[ 6 ].drawLine(( int )(Math.sqrt((SIZE_RASTER - 1 ) * (SIZE_RASTER - 1 ) - y * y) + 1 ),y,SIZE_RASTER,y); } //Item Field Reset ################################################################################### private void ResetItemField() { for ( int i = 0 ;i < LENGTH_RASTER_X;i++) { for ( int z = 0 ;z < LENGTH_RASTER_Y;z++) { itemfield[i][z][ 0 ] = 0 ; itemfield[i][z][ 1 ] = 0 ; itemfield[i][z][ 2 ] = 0 ; } } } //DrawItemField on Raster ############################################################################ private void DrawItem() { GreenfootImage image_store = new GreenfootImage(SIZE_RASTER,SIZE_RASTER); for ( int i = x_raster_pos;i <= x_raster_pos + getBackground().getWidth() / SIZE_RASTER && i < LENGTH_RASTER_X;i++) { for ( int z = y_raster_pos;z <= y_raster_pos + getBackground().getHeight() / SIZE_RASTER && z < LENGTH_RASTER_Y;z++) { if (getBackground().getColor().equals(Color.white)) { getBackground().fillRect(SIZE_RASTER * (i - x_raster_pos) + x_raster_off - SIZE_RASTER,SIZE_RASTER * (z - y_raster_pos) + y_raster_off - SIZE_RASTER,SIZE_RASTER,SIZE_RASTER); } if (itemfield[i][z][ 0 ] != 0 && !getBackground().getColor().equals(Color.white)) { image_store.setColor(Color.white); image_store.fill(); image_store.drawImage(image_item[itemfield[i][z][ 0 ]], 0 , 0 ); image_store = ChangeImageColor(image_store,item_colorarray[itemfield[i][z][ 2 ]]); image_store.rotate(itemfield[i][z][ 1 ] * 90 ); getBackground().drawImage(image_store,SIZE_RASTER * (i - x_raster_pos) + x_raster_off - SIZE_RASTER,SIZE_RASTER * (z - y_raster_pos) + y_raster_off - SIZE_RASTER); image_store.rotate( 360 - itemfield[i][z][ 1 ] * 90 ); } } } } //Raster Pos ###################################################################################### private void DrawRasterPos() { getBackground().drawString( "X: " +(x_mouse_pos+ 1 ), 10 , 20 ); getBackground().drawString( "Y: " +(y_mouse_pos+ 1 ), 10 , 40 ); if (mouse != null && getBackground().getColor().equals(Color.white)) { x_mouse_pos = x_raster_pos + ( int )((mouse.getX() - x_raster_off - 2 + SIZE_RASTER) / SIZE_RASTER); y_mouse_pos = y_raster_pos + ( int )((mouse.getY() - y_raster_off - 2 + SIZE_RASTER) / SIZE_RASTER); } } //RASTER ########################################################################################## private void Raster() { getBackground().setColor(Color.white); DrawRaster(); RasterDrag(); ResetRasterOffset(); RasterBorder(); getBackground().setColor(Color.lightGray); DrawItem(); DrawRasterPos(); ItemAtMouse(); getBackground().setColor(Color.darkGray); DrawRaster(); } private void DrawRaster() { for ( int i = 0 ;i < getBackground().getWidth() / SIZE_RASTER;i++) { getBackground().drawLine(SIZE_RASTER * i + x_raster_off, 0 ,SIZE_RASTER * i + x_raster_off,getBackground().getHeight()); getBackground().drawLine( 0 ,SIZE_RASTER * i + y_raster_off,getBackground().getWidth(),SIZE_RASTER * i + y_raster_off); } } private void RasterDrag() { if (Greenfoot.mouseDragged( this ) && mouse != null ) { x_raster_off += mouse.getX() - x_drag_off; y_raster_off += mouse.getY() - y_drag_off; } if (mouse != null ) { x_drag_off = mouse.getX(); y_drag_off = mouse.getY(); } } private void RasterBorder() { if (x_raster_pos < 0 ) { x_raster_off = SIZE_RASTER; x_raster_pos = 0 ; } if (y_raster_pos < 0 ) { y_raster_off = SIZE_RASTER; y_raster_pos = 0 ; } if (x_raster_pos >= LENGTH_RASTER_X - getBackground().getWidth() / SIZE_RASTER) { x_raster_off = SIZE_RASTER; x_raster_pos = LENGTH_RASTER_X - getBackground().getWidth() / SIZE_RASTER; } if (y_raster_pos >= LENGTH_RASTER_Y - getBackground().getHeight() / SIZE_RASTER) { y_raster_off = SIZE_RASTER; y_raster_pos = LENGTH_RASTER_Y - getBackground().getHeight() / SIZE_RASTER; } } private void ResetRasterOffset() { while (x_raster_off > SIZE_RASTER || x_raster_off < 0 || y_raster_off > SIZE_RASTER || y_raster_off < 0 ) { if (x_raster_off > SIZE_RASTER) { x_raster_off -= SIZE_RASTER; x_raster_pos--; } if (x_raster_off < 0 ) { x_raster_off += SIZE_RASTER; x_raster_pos++; } if (y_raster_off > SIZE_RASTER) { y_raster_off -= SIZE_RASTER; y_raster_pos--; } if (y_raster_off < 0 ) { y_raster_off += SIZE_RASTER; y_raster_pos++; } } } //Change Color of an Image ################################################################################ private GreenfootImage ChangeImageColor(GreenfootImage image_store,Color color) { for ( int i = 0 ;i < image_store.getWidth();i++) { for ( int z = 0 ;z < image_store.getHeight();z++) { if (image_store.getColorAt(i,z).getAlpha() > 0 && !image_store.getColorAt(i,z).equals(Color.white)) { image_store.setColorAt(i,z,color); } } } return image_store; } } |
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 | private void ItemAtMouse() { GreenfootImage image_atmouse = new GreenfootImage(SIZE_RASTER,SIZE_RASTER); String key = Greenfoot.getKey(); if ( "up" .equals(key)) item_actual++; if ( "down" .equals(key)) item_actual--; if ( "left" .equals(key)) item_color++; if ( "right" .equals(key)) item_color--; if (Greenfoot.mouseClicked( this ) && !Greenfoot.mouseDragEnded( this ) && Greenfoot.getMouseInfo()..getButton() == 3 ) item_rotation = (item_rotation+ 1 )% 4 ; image_atmouse.drawImage(image_item[item_actual], 0 , 0 ); image_atmouse.rotate(item_rotation* 90 ); if (item_actual != 0 ) image_atmouse = ChangeImageColor(image_atmouse, new Color(item_colorarray[item_color].getRed(), item_colorarray[item_color].getGreen(), item_colorarray[item_color].getBlue(), 155 )); getBackground().drawImage(image_atmouse,(x_mouse_pos-x_raster_pos- 1 )*SIZE_RASTER+x_raster_off, (y_mouse_pos-y_raster_pos- 1 )*SIZE_RASTER+y_raster_off); image_atmouse.rotate( 360 - item_rotation * 90 ); } if (Greenfoot.mouseClicked( this ) && !Greenfoot.mouseDragEnded( this ) && Greenfoot.getMouseInfo().getButton() == 1 ) { MouseInfo mouse = Greenfoot.getMouseInfo(); int x_mouse_pos = mouse.getX(), y_mouse_pos = mouse.getY(); itemfield[x_mouse_pos][y_mouse_pos][ 0 ] = item_actual; itemfield[x_mouse_pos][y_mouse_pos][ 1 ] = item_rotation; itemfield[x_mouse_pos][y_mouse_pos][ 2 ] = item_color; } } |