Return to Snippet

Revision: 8699
at October 3, 2008 09:20 by nickmeinhold


Initial Code
// 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;

public class TheCanvas extends JPanel implements MouseListener, MouseMotionListener {
	
	private int X, Y;
	private Image image;
	static public ArrayList<Item> canvas_items = new ArrayList<Item>();
	
	public TheCanvas() {
		addMouseMotionListener(this);
		addMouseListener(this);
	    setVisible(true);
	}
	public void mouseMoved(MouseEvent event) {
		X = (int) event.getPoint().getX();
		Y = (int) event.getPoint().getY();
		repaint();
	}
	public void mouseDragged(MouseEvent event) {
		mouseMoved(event);
	}
	public void mousePressed(MouseEvent event) {
		
	}
	
	public void mouseReleased(MouseEvent event) {
		
	}
	
	public void mouseEntered(MouseEvent event) {
	   
	}
	
	public void mouseExited(MouseEvent event) {
	   
	}
	
	public void mouseClicked(MouseEvent event) {
		System.out.println(X+","+Y);
		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)) {
			    	System.out.println(item.getImgpath());
			    	break;
			    }
			    
		  }
		}
		
	}
	  public void update(Graphics graphics) {
	    paint(graphics);
	  }
	  public void paint(Graphics g) {
		// Dynamically calculate size information
	        Dimension size = getSize();
	        // diameter
	        //int d = Math.min(size.width, size.height); 
	        //int x = (size.width - d)/2;
	        //int y = (size.height - d)/2;
		  g.setColor(Color.white);
		  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);
	  }
	  
}

Initial URL

                                

Initial Description

                                

Initial Title
NickMeinholdTheCanvas v0.3

Initial Tags

                                

Initial Language
Java