Package view

Source Code of view.card_view

package view;

import game_model.position;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FontFormatException;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;

import data_model.card;

public class card_view extends JPanel implements Observer{
//Globale Variablen--------------------------------------------------------------------------------------------------------------
  boolean state = false; //Gibt an die Karte aufgedeckt (true) oder verdeckt (false) ist
  String position; //Gibt an ob die Karte noch auf der Hand "hand" oder schon auf dem Spielfeld "square" ist | "emptysquare" ist ein leeres Feld auf dem Spielfeld
   
  //4 JLabels zum !Anzeigen! der Angriffswerte der gelegten Karte
  JLabel north_value = new JLabel("0");
  JLabel east_value = new JLabel("0");
  JLabel south_value = new JLabel("0");
  JLabel west_value = new JLabel("0");
 
  position pos;
   
  BufferedImage cardimg;
//--------------------------------------------------------------------------------------------------------------------------------
 
//Konstruktoren-------------------------------------------------------------------------------------------------------------------
  public card_view(String position,position pos) throws IOException{
    this.position = position;
    this.pos = pos;
    this.pos.addObserver(this);
   
    this.setLayout(new BorderLayout());
    this.add(north_value,BorderLayout.NORTH);
    this.add(east_value,BorderLayout.EAST);
    this.add(south_value,BorderLayout.SOUTH);
    this.add(west_value,BorderLayout.WEST);
   
    try {
      north_value.setFont(statics.getFont(14));
      east_value.setFont(statics.getFont(14));
      south_value.setFont(statics.getFont(14));
      west_value.setFont(statics.getFont(14));
    } catch (FontFormatException e) {
      System.out.println("Font konnte nicht geladen werden");
    }
   
    north_value.setHorizontalAlignment(JLabel.CENTER);
    south_value.setHorizontalAlignment(JLabel.CENTER);
   
    flip();
    if(pos.getOwn()!=null){
      cardimg = ImageIO.read(new File(pos.getOwn().getPicture()));
      this.setBackground(pos.getOwner().getColor());
      setValues();
    }
    initializeListener();
  }
//--------------------------------------------------------------------------------------------------------------------------------
 
//Getter&Setter-------------------------------------------------------------------------------------------------------------------
 
//--------------------------------------------------------------------------------------------------------------------------------

//Listener------------------------------------------------------------------------------------------------------------------------
  public void initializeListener(){
    this.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt){
        mouse_action(evt);
      }
       });
  }
 
  public void mouse_action(java.awt.event.MouseEvent evt) {
    //Unterscheidung zwischen Kartenpositionen des Spielfelds und Handkarten �ber 'position'
      if(position.equals("hand") && state){
        if(statics.getSelected_pos()==null){
          statics.setSelected_pos(this.pos);
          statics.setSelected_card(this);
        }
        else {
          statics.setSelected_pos(null);
          statics.setSelected_card(null);
        }
      }

      if(position.equals("emptysquare")){
        //Wird eine Platz auf dem Spielfeld ausgew�hlt muss folgendes getan werden:
        //  -Pr�fen ob vorher eine Karte aus der Hand ausgew�hlt wurde
        //  -Pr�fen ob Feld noch frei
        //  -Karte auf Feld ablegen (genauer?)
        //  -Karte aus Handkarten entfernen
        //  -von neuer Kartenposition aus einen Angriff starten
        if(statics.getSelected_pos()==null){
          Container v = this;
          while(v.getParent()!=null){
            v=v.getParent();
          }
          //new msg_window("Bitte zuerst eine Handkarte ausw�hlen",v);
          //System.out.println(this.pos);
        }
        else{
          if(statics.getSelected_pos()!=null && this.pos.getOwn()==null){
              position pospuff1 = this.pos.getNorth();
              position pospuff2 = this.pos.getEast();
              position pospuff3 = this.pos.getSouth();
              position pospuff4 = this.pos.getWest();
             
            this.pos.setOwn(statics.getSelected_pos().getOwn());
            this.pos.setOwner(statics.getSelected_pos().getOwner());
           
            if(pos.getOwner().equals(statics.getPlayer1()))statics.getFm().getGp().getP1hand().disposeCard(statics.getSelected_card());
          else statics.getFm().getGp().getP2hand().disposeCard(statics.getSelected_card());
            statics.setSelected_card(null);
            //Nachbarn der Position m�ssen erhalten bleiben daher werden sie in Puffer-Variablen gespeichert
            //und nach dem setzen der neuen Position wieder zugewiesen
           
            this.pos.setNeighbour(pospuff1,pospuff2,pospuff3,pospuff4);
              this.position="square";
          try {
            cardimg = ImageIO.read(new File(pos.getOwn().getPicture()))
          } catch (IOException e) {
            System.out.println("Bild konnte nicht geladen werden");
          }
          if(pos.getOwner().equals(statics.getPlayer1()))this.setBackground(statics.getPlayer1().getColor());
          else this.setBackground(statics.getPlayer2().getColor());
          setValues();
          north_value.setVisible(true);
          east_value.setVisible(true);
          south_value.setVisible(true);
          west_value.setVisible(true);
              statics.setSelected_pos(null);
              pos.attack();
          //System.out.println(this.pos);             
              statics.getFm().next_turn();
          }
        }
              
      }     
    }
//--------------------------------------------------------------------------------------------------------------------------------

//Sonstige Methoden---------------------------------------------------------------------------------------------------------------
  public void setValues(){
    north_value.setText(pos.getOwn().getNorth()+"");
    east_value.setText(pos.getOwn().getEast()+"");
    south_value.setText(pos.getOwn().getSouth()+"");
    west_value.setText(pos.getOwn().getWest()+"");
  }
 
  public void flip(){
    if(state || pos.getOwn()==null){
      //Verdecke die Karte
      //Bild auf Kartenr�cken setzen und Werte ausblenden
      north_value.setVisible(false);
      east_value.setVisible(false);
      south_value.setVisible(false);
      west_value.setVisible(false);
    }
    else{
      //Decke die Karte auf
      //Bild auf Kartenbild setzen und Werte einblenden
      north_value.setVisible(true);
      east_value.setVisible(true);
      south_value.setVisible(true);
      west_value.setVisible(true);
    }
    state = !state;
    repaint();
  }
 
  protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if(!position.equals("emptysquare"))
    {
          if(state){
            Graphics2D g2d = (Graphics2D)g;
            Rectangle rect = this.getBounds();

          Color col1 = Color.white;
          Color col2;
         
          if(pos.getOwner().equals(statics.getPlayer1()))col2 = statics.getPlayer1().getColor();
          else col2 = statics.getPlayer2().getColor();
         
            GradientPaint gp = new GradientPaint( 0f, 55f,col1, 200f, 55f,col2);
            g2d.setPaint(gp);
            g2d.fillRect(0,0,rect.width,rect.height);
            g2d.drawImage(cardimg,0,0,rect.width,rect.height,null);

          }
      else
        try {
          g.drawImage(statics.getCardBack(),0,0,this.getWidth(),this.getHeight(),null);
        } catch (IOException e) {
          System.out.println("Kartenhintergrund konnte nicht geladen werden");
        }
    }
    else{
      try {
        g.drawImage(statics.getCardField(),0,0,this.getWidth(),this.getHeight(),null);
      } catch (IOException e) {
        System.out.println("Kartenfeldbild konnte nicht geladen werden");
      }
    }
    }
 
  public void update(Observable arg0, Object arg1) {
    if(pos.getOwner().equals(statics.getPlayer1()))this.setBackground(statics.getPlayer1().getColor());
    else this.setBackground(statics.getPlayer2().getColor());
  }
//--------------------------------------------------------------------------------------------------------------------------------
}
TOP

Related Classes of view.card_view

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.