package battleship;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener{
/** Width of the JFrame **/
public final int WIDTH = 1000;
/** Height of the JFrame **/
public final int HEIGHT = 600;
/** Stroke of the Grid **/
public final int STROKE = 2;
/** Size of the Grid **/
public final int GRID_SIZE = 400;
/** Backgroundimage from the Grid **/
private BufferedImage imgGrid;
/** Images from the Ships **/
private BufferedImage[] imgShips;
/** Graphics to paint **/
private BufferedImage imgContent;
private Graphics2D gImage;
/** Buttons **/
JButton bMute = new JButton();
JButton bExit = new JButton();
public GUI(){
super("Battleship");
try{
imgGrid = ImageIO.read(new File("src/battleship/images/grid.png"));
imgShips = new BufferedImage[4];
for(int i = 0; i < imgShips.length; i++){
imgShips[i] = ImageIO.read(new File("src/battleship/images/ship_" + (i+2) + ".png"));
}
}catch(IOException e){
System.out.println(e.getMessage());
}
this.setSize(WIDTH, HEIGHT);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setIconImage(imgShips[0]);
this.setVisible(true);
//Unvisible Buttons
this.setLayout(null);
this.add(bMute);
bMute.setBounds(300, 510,100,60);
bMute.setBorderPainted(false);
bMute.setOpaque(false);
bMute.setContentAreaFilled(false);
// Listener register
bMute.addActionListener(this);
}
private BufferedImage paintBKG(){
try{
return ImageIO.read(new File("src/battleship/images/bkg_2.jpg"));
}catch(IOException e){
System.out.println(e.getMessage());
return null;
}
}
private void paintGrids(Graphics2D g){
gImage = imgGrid.createGraphics();
gImage.setColor(new Color(0,0,0,150));
gImage.setStroke(new BasicStroke(STROKE));
gImage.drawRect(GRID_SIZE/20, GRID_SIZE/20, GRID_SIZE, GRID_SIZE);
for(int i=1; i<=9; i++){
gImage.fillRect((GRID_SIZE/20 + STROKE/2),
(GRID_SIZE/20 - STROKE/2)+(i*GRID_SIZE/10),
GRID_SIZE - STROKE,
STROKE);
gImage.fillRect((GRID_SIZE/20 - STROKE/2)+(i*GRID_SIZE/10),
(GRID_SIZE/20 + STROKE/2),
STROKE,
GRID_SIZE - STROKE);
}
g.drawImage(imgGrid, 40, 100, null);
g.drawImage(imgGrid, 520, 100, null);
}
public void paintText(Graphics2D g){
Font font = new Font("28 Days Later", 4, 35); //Font, Options, Size
//g.setColor(new Color(255,255,255,150));
g.setFont(font);
g.drawString("Your turn", 30, 80);
g.drawString("Mute", WIDTH/3, HEIGHT-HEIGHT/40);
g.drawString("Exit",WIDTH-WIDTH/3, HEIGHT-HEIGHT/40);
}
public void paintShips(Graphics2D g, int[] acoordinate, int isize, boolean bhorizontal){
if(bhorizontal){
g.drawImage(imgShips[isize-2], 170, 100, null);
}else{
g.drawImage(rotateImage(imgShips[isize-2]), 20, 10, null);
}
}
@Override
public void paint(Graphics g){
imgContent = paintBKG();
paintGrids(imgContent.createGraphics());
paintText(imgContent.createGraphics());
gImage.drawImage(imgShips[2], 90, 100, null);
gImage.drawImage(rotateImage(imgShips[1]), 20, 10, null);
//Test
// gImage.drawImage(imgShips[1], 170, 100, null);
g.drawImage(imgContent, 0, 0, null);
}
public void target_positiv(int x, int y){
gImage = imgGrid.createGraphics();
gImage.setColor(new Color(255,0,0,120));
gImage.fillRect(GRID_SIZE/20+STROKE/2+x*GRID_SIZE/10,GRID_SIZE/20+STROKE/2+y*GRID_SIZE/10,GRID_SIZE/10-STROKE,GRID_SIZE/10-STROKE);
this.getGraphics().drawImage(imgGrid, 520, 100, null);
}
public void target_negativ(int x, int y)
{
gImage = imgGrid.createGraphics();
gImage.setColor(new Color(0,0,0,180));
gImage.fillRect(GRID_SIZE/20+STROKE/2+x*GRID_SIZE/10,GRID_SIZE/20+STROKE/2+y*GRID_SIZE/10,GRID_SIZE/10-STROKE,GRID_SIZE/10-STROKE);
this.getGraphics().drawImage(imgGrid, 520, 100, null);
}
/**
* Rotates an Image 90 degrees
* @param src
* @return
*/
private BufferedImage rotateImage(BufferedImage src){
AffineTransform tx = new AffineTransform();
// last, width = height and height = width :)
tx.translate(src.getHeight() / 2,src.getWidth() / 2);
tx.rotate(Math.PI / 2);
// first - center image at the origin so rotate works OK
tx.translate(-src.getWidth() / 2,-src.getHeight() / 2);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
// new destination image where height = width and width = height.
BufferedImage rotatetImage =new BufferedImage(src.getHeight(), src.getWidth(), src.getType());
op.filter(src, rotatetImage);
return rotatetImage;
}
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == bMute){
System.out.println("Mute Button Check");
}
if (e.getSource() == bExit){
System.out.println("Exit Button Check");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
GUI bs = new GUI();
bs.target_positiv(2, 2);
bs.target_negativ(7, 2);
bs.target_negativ(8, 6);
int[] array = new int[2];
array[0] = 0;
array[1] = 1;
bs.paintShips(imgContent.createGraphics(),test,3,true);
}
}