Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package spielen;
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.event.MouseListener;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import javax.swing.JComponent;
- public final class Spieloberflaeche extends JComponent implements MouseMotionListener {
- /**
- * auto-generated serialVersionUID.
- */
- private static final long serialVersionUID = 1L;
- /**
- * Height of the field.
- */
- private static final int HEIGHT = 480;
- /**
- * Width of the field.
- */
- private static final int WIDTH = 480;
- /**
- * Anzahl Boxen in einer Reihe.
- */
- public static final int BOX_COUNT = 8;
- public static final int BOX_SIZE = 60;
- /**
- * The mainImage as a BufferedImage.
- */
- private BufferedImage mainImage;
- /**
- * Auswahlbox.
- */
- private BufferedImage box;
- /**
- * Gives the dimension of the field.
- */
- private Dimension dimension = new Dimension(WIDTH, HEIGHT);
- Point position = new Point(0, 0);
- /**
- * Creates a new JCompontet-Field.
- */
- public Spieloberflaeche() {
- initImage();
- this.setSize(dimension);
- this.setPreferredSize(dimension);
- this.setMinimumSize(dimension);
- this.setMaximumSize(dimension);
- this.addMouseMotionListener(this);
- this.setVisible(true);
- this.repaint();
- }
- /**
- * Initializes the mainImage of the field and draws Rect.
- */
- private void initImage() {
- this.mainImage = new BufferedImage(WIDTH, HEIGHT,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = this.mainImage.getGraphics().create();
- g.setColor(new Color(219, 244, 214));
- g.fillRect(0, 0, WIDTH, HEIGHT);
- this.box = new BufferedImage(WIDTH / BOX_COUNT, HEIGHT / BOX_COUNT,
- BufferedImage.TYPE_INT_RGB);
- g = this.box.getGraphics().create();
- g.setColor(new Color(84, 190, 87));
- g.fillRect(0, 0, WIDTH / BOX_COUNT, HEIGHT / BOX_COUNT);
- }
- /**
- * Overrides paintComponent to draw the mainImage and super method stuff.
- *
- * @param g
- * Graphic that has to be painted
- */
- @Override
- public void paintComponent(final Graphics g) {
- super.paintComponent(g);
- g.drawImage(this.mainImage, 0, 0, WIDTH, HEIGHT, null);
- g.drawImage(this.box, position.x * BOX_SIZE, position.y * BOX_SIZE, BOX_SIZE, BOX_SIZE, null);
- g.setColor(Color.black);
- for (int i= 0; i<8; i++){
- g.drawLine(BOX_SIZE*i, 0, BOX_SIZE*i, 480);
- g.drawLine(0, BOX_SIZE*i, 480, BOX_SIZE*i);
- }
- }
- @Override
- public void mouseDragged(MouseEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void mouseMoved(MouseEvent e) {
- position = e.getPoint();
- position.x = position.x/BOX_SIZE;
- position.y = position.y/BOX_SIZE;
- repaint();
- }
- }
Add Comment
Please, Sign In to add comment