/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// added code to select an item from the canvas, currently just prints the item's image path // just to show it works. The properties toolbar needs to change accordingly package project; import java.awt.*; import javax.imageio.ImageIO; import javax.swing.*; import java.awt.event.*; import java.io.File; import java.util.ArrayList; import java.util.Iterator; private int X, Y; static public ArrayList<Item> canvas_items = new ArrayList<Item>(); public TheCanvas() { addMouseMotionListener(this); addMouseListener(this); setVisible(true); } X = (int) event.getPoint().getX(); Y = (int) event.getPoint().getY(); repaint(); } mouseMoved(event); } } } } } if(MapMaker.current_item != null) { Item placed_item = new Item(MapMaker.current_item); placed_item.setPos(X, Y); canvas_items.add(placed_item); MapMaker.current_item = null; repaint(); } else { for (int i = canvas_items.size()-1; i >= 0; i--) { Item item = (Item) canvas_items.get(i) ; if(item.picked(X, Y)) { break; } } } } paint(graphics); } // Dynamically calculate size information // diameter //int d = Math.min(size.width, size.height); //int x = (size.width - d)/2; //int y = (size.height - d)/2; g.fillRect(0, 0, size.width, size.height); for (Iterator<Item> it = canvas_items.iterator (); it.hasNext (); ) { Item item = it.next (); g.drawImage(item.getImg(), item.getPos().x, item.getPos().y, null); } if(MapMaker.current_item != null) g.drawImage(MapMaker.current_item.getImg(), X, Y, null); } }