NickMeinholdTheCanvas v0.2


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package project;
  2.  
  3. import java.awt.*;
  4. import javax.imageio.ImageIO;
  5. import javax.swing.*;
  6.  
  7. import java.awt.event.*;
  8. import java.io.File;
  9. import java.util.ArrayList;
  10. import java.util.Iterator;
  11.  
  12. public class TheCanvas extends JPanel implements MouseListener, MouseMotionListener {
  13.  
  14. private int X, Y;
  15. private Image image;
  16. static public ArrayList<Item> canvas_items = new ArrayList<Item>();
  17.  
  18. public TheCanvas() {
  19. addMouseMotionListener(this);
  20. addMouseListener(this);
  21. setVisible(true);
  22. }
  23. public void mouseMoved(MouseEvent event) {
  24. X = (int) event.getPoint().getX();
  25. Y = (int) event.getPoint().getY();
  26. repaint();
  27. }
  28. public void mouseDragged(MouseEvent event) {
  29. mouseMoved(event);
  30. }
  31. public void mousePressed(MouseEvent event) {
  32.  
  33. }
  34.  
  35. public void mouseReleased(MouseEvent event) {
  36.  
  37. }
  38.  
  39. public void mouseEntered(MouseEvent event) {
  40.  
  41. }
  42.  
  43. public void mouseExited(MouseEvent event) {
  44.  
  45. }
  46.  
  47. public void mouseClicked(MouseEvent event) {
  48. System.out.println(X+","+Y);
  49. if(MapMaker.current_item != null) {
  50. Item placed_item = new Item(MapMaker.current_item);
  51. placed_item.setPos(X, Y);
  52. canvas_items.add(placed_item);
  53. MapMaker.current_item = null;
  54. repaint();
  55. }
  56.  
  57. }
  58. public void update(Graphics graphics) {
  59. paint(graphics);
  60. }
  61. public void paint(Graphics g) {
  62. // Dynamically calculate size information
  63. Dimension size = getSize();
  64. // diameter
  65. //int d = Math.min(size.width, size.height);
  66. //int x = (size.width - d)/2;
  67. //int y = (size.height - d)/2;
  68. g.setColor(Color.white);
  69. g.fillRect(0, 0, size.width, size.height);
  70. for (Iterator<Item> it = canvas_items.iterator (); it.hasNext (); ) {
  71. Item item = it.next ();
  72. g.drawImage(item.getImg(), item.getPos().x, item.getPos().y, null);
  73. }
  74. if(MapMaker.current_item != null)
  75. g.drawImage(MapMaker.current_item.getImg(), X, Y, null);
  76. }
  77.  
  78. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.