NickMeinholdTheCanvas v0.1


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



Copy this code and paste it in your HTML
  1. // modified 01/10/08
  2.  
  3. package project;
  4.  
  5. import java.awt.*;
  6. import javax.imageio.ImageIO;
  7. import javax.swing.*;
  8. import java.awt.event.*;
  9. import java.io.File;
  10. import java.util.ArrayList;
  11.  
  12. public class TheCanvas extends JPanel implements MouseListener, MouseMotionListener {
  13.  
  14. private int X, Y;
  15. private Image image;
  16. private ArrayList<Item> canvas_items = new ArrayList<Item>();
  17. Item current_item = new Vehicle("Tram", "black", "images/vehicle/tram_black.gif", "Tram", true);
  18.  
  19. public TheCanvas() {
  20. addMouseMotionListener(this);
  21. addMouseListener(this);
  22. setVisible(true);
  23. try{
  24. image = ImageIO.read(new File ("images/save.png"));
  25. }
  26. catch(Exception e) {
  27. System.out.println("The image file could not be found"+e);
  28. }
  29. }
  30. public void mouseMoved(MouseEvent event) {
  31. X = (int) event.getPoint().getX();
  32. Y = (int) event.getPoint().getY();
  33. repaint();
  34. }
  35. public void mouseDragged(MouseEvent event) {
  36. mouseMoved(event);
  37. }
  38. public void mousePressed(MouseEvent event) {
  39.  
  40. }
  41.  
  42. public void mouseReleased(MouseEvent event) {
  43.  
  44. }
  45.  
  46. public void mouseEntered(MouseEvent event) {
  47.  
  48. }
  49.  
  50. public void mouseExited(MouseEvent event) {
  51.  
  52. }
  53.  
  54. public void mouseClicked(MouseEvent event) {
  55. System.out.println(X+","+Y);
  56. }
  57. public void update(Graphics graphics) {
  58. paint(graphics);
  59. }
  60. public void paint(Graphics g) {
  61. // Dynamically calculate size information
  62. Dimension size = getSize();
  63. // diameter
  64. //int d = Math.min(size.width, size.height);
  65. //int x = (size.width - d)/2;
  66. //int y = (size.height - d)/2;
  67. g.setColor(Color.white);
  68. g.fillRect(0, 0, size.width, size.height);
  69. g.drawImage(image, X, Y, null);
  70. }
  71.  
  72. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.