package crazyOrb.gui;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.LinkedList;
import crazyOrb.mechanics.Ball;
import crazyOrb.mechanics.Block;
import crazyOrb.mechanics.DropableFigure;
import crazyOrb.mechanics.MechanicsFacade;
import crazyOrb.mechanics.MovableFigure;
import crazyOrb.mechanics.MoveBallDirection;
import crazyOrb.mechanics.Special;
/**
* @author Claudia, Marlene, Ragna-Diana
*/
public class PlayCanvas extends Canvas implements KeyListener {
private static final long serialVersionUID = 1L;
private static Color color = Color.RED;
public static Bild bild = Bild.RASEN;
private Image img = getToolkit().getImage(SpielGUI.class.getResource("/crazyOrb/gui/Gras.jpg"));
private Image img2 = getToolkit().getImage(SpielGUI.class.getResource("/crazyOrb/gui/Wolken.jpg"));
private Image img3 = getToolkit().getImage(SpielGUI.class.getResource("/crazyOrb/gui/Space.jpg"));
private String showString = "";
private MechanicsFacade mechanics;
private boolean laser;
public PlayCanvas(MechanicsFacade mechanics) {
this.mechanics = mechanics;
this.addKeyListener(this);
laser = false;
}
public void update(Graphics g) {
Graphics offgc;
Image offscreen = null;
Rectangle box = g.getClipBounds();
// create the offscreen buffer and associated Graphics
offscreen = createImage(box.width, box.height);
offgc = offscreen.getGraphics();
// clear the exposed area
offgc.setColor(getBackground());
offgc.fillRect(0, 0, box.width, box.height);
offgc.setColor(getForeground());
// do normal redraw
offgc.translate(-box.x, -box.y);
paint(offgc);
// transfer offscreen to window
g.drawImage(offscreen, box.x, box.y, this);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(color);
g2D.clearRect(0, 0, getWidth(), getHeight());
switch (bild) {
case RASEN:
g2D.drawImage(img, 0, 0, this);
break;
case WOLKEN:
g2D.drawImage(img2, 0, 0, this);
break;
case WELTALL:
g2D.drawImage(img3, 0, 0, this);
break;
}
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
LinkedList<DropableFigure> blocks = mechanics.getBlocks();
Ball ball = mechanics.getBall();
double xE = getWidth() / mechanics.getWidth();
double yE = getHeight() / mechanics.getHeight();
for (MovableFigure block : blocks) {
double x = Math.round(block.getxPos() * xE);
double y = block.getyPos() * yE;
if (block instanceof Block) {
g2D.setColor(Color.CYAN);
g2D.fill(new Rectangle2D.Double(x, y, xE, yE));
}
if (block instanceof Special) {
g2D.setColor(Color.PINK);
g2D.fill(new RoundRectangle2D.Double(x, y, xE, yE, xE / 10, yE / 2));
}
}
g2D.setColor(color);
g2D.fill(new Ellipse2D.Double(ball.getxPos() * xE, ball.getyPos() * yE, xE, yE));
if(laser){
g2D.setColor(Color.ORANGE);
g2D.fillRoundRect(
(int) (ball.getxPos() * xE + xE / 2 - 5 ),
0,
10,
(int) (yE * (mechanics.getHeight() - 1)), 5, 40);
laser = false;
}
if (!showString.equals("")) {
g2D.setColor(Color.WHITE);
g2D.fillRoundRect(7, (getHeight() / 2)-20, getWidth() - 17, 25, 10, 10);
g2D.setColor(Color.BLACK);
g2D.setFont(new Font("Arial", Font.BOLD, 20));
g2D.drawString(showString, 12, getHeight() / 2);
}
}
public static void setFarbe(Color farbe) {
color = farbe;
}
@Override
public void keyPressed(KeyEvent e) {
if (SpielGUI.getSpiel()) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
mechanics.MoveBall(MoveBallDirection.DIRECTION_RIGHT);
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
mechanics.MoveBall(MoveBallDirection.DIRECTION_LEFT);
}
if (e.getKeyCode() == KeyEvent.VK_P) {
if (mechanics.isPaused()) {
showString("");
mechanics.resumeMovement();
} else {
showString("Fortsetzen? P drücken!");
mechanics.pauseMovement();
}
}
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
mechanics.useSpecial();
}
}
}
@Override
public void keyReleased(KeyEvent arg0) {
}
@Override
public void keyTyped(KeyEvent arg0) {
}
public void showString(String str) {
showString = str;
}
public void setLaser(boolean laser) {
this.laser = laser;
}
}