Guest User

Untitled

a guest
Jul 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. package spielen;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Point;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseMotionListener;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.event.MouseListener;
  12. import java.awt.image.BufferedImage;
  13. import java.util.ArrayList;
  14.  
  15. import javax.swing.JComponent;
  16.  
  17.  
  18. public final class Spieloberflaeche extends JComponent implements MouseMotionListener {
  19.  
  20. /**
  21. * auto-generated serialVersionUID.
  22. */
  23. private static final long serialVersionUID = 1L;
  24. /**
  25. * Height of the field.
  26. */
  27. private static final int HEIGHT = 480;
  28. /**
  29. * Width of the field.
  30. */
  31. private static final int WIDTH = 480;
  32. /**
  33. * Anzahl Boxen in einer Reihe.
  34. */
  35. public static final int BOX_COUNT = 8;
  36.  
  37. public static final int BOX_SIZE = 60;
  38. /**
  39. * The mainImage as a BufferedImage.
  40. */
  41. private BufferedImage mainImage;
  42. /**
  43. * Auswahlbox.
  44. */
  45. private BufferedImage box;
  46. /**
  47. * Gives the dimension of the field.
  48. */
  49. private Dimension dimension = new Dimension(WIDTH, HEIGHT);
  50.  
  51. Point position = new Point(0, 0);
  52.  
  53. /**
  54. * Creates a new JCompontet-Field.
  55. */
  56. public Spieloberflaeche() {
  57. initImage();
  58. this.setSize(dimension);
  59. this.setPreferredSize(dimension);
  60. this.setMinimumSize(dimension);
  61. this.setMaximumSize(dimension);
  62. this.addMouseMotionListener(this);
  63. this.setVisible(true);
  64. this.repaint();
  65. }
  66.  
  67. /**
  68. * Initializes the mainImage of the field and draws Rect.
  69. */
  70. private void initImage() {
  71. this.mainImage = new BufferedImage(WIDTH, HEIGHT,
  72. BufferedImage.TYPE_INT_RGB);
  73. Graphics g = this.mainImage.getGraphics().create();
  74.  
  75. g.setColor(new Color(219, 244, 214));
  76. g.fillRect(0, 0, WIDTH, HEIGHT);
  77. this.box = new BufferedImage(WIDTH / BOX_COUNT, HEIGHT / BOX_COUNT,
  78. BufferedImage.TYPE_INT_RGB);
  79. g = this.box.getGraphics().create();
  80. g.setColor(new Color(84, 190, 87));
  81. g.fillRect(0, 0, WIDTH / BOX_COUNT, HEIGHT / BOX_COUNT);
  82.  
  83.  
  84.  
  85. }
  86.  
  87.  
  88. /**
  89. * Overrides paintComponent to draw the mainImage and super method stuff.
  90. *
  91. * @param g
  92. * Graphic that has to be painted
  93. */
  94. @Override
  95. public void paintComponent(final Graphics g) {
  96. super.paintComponent(g);
  97. g.drawImage(this.mainImage, 0, 0, WIDTH, HEIGHT, null);
  98. g.drawImage(this.box, position.x * BOX_SIZE, position.y * BOX_SIZE, BOX_SIZE, BOX_SIZE, null);
  99.  
  100. g.setColor(Color.black);
  101. for (int i= 0; i<8; i++){
  102. g.drawLine(BOX_SIZE*i, 0, BOX_SIZE*i, 480);
  103. g.drawLine(0, BOX_SIZE*i, 480, BOX_SIZE*i);
  104. }
  105. }
  106.  
  107. @Override
  108. public void mouseDragged(MouseEvent e) {
  109. // TODO Auto-generated method stub
  110.  
  111. }
  112.  
  113. @Override
  114. public void mouseMoved(MouseEvent e) {
  115. position = e.getPoint();
  116. position.x = position.x/BOX_SIZE;
  117. position.y = position.y/BOX_SIZE;
  118. repaint();
  119. }
  120. }
Add Comment
Please, Sign In to add comment