package cardgamewindowsclient;
import cardgamecard.CGBaseCard;
import cardgamecard.CGBattleSetCard;
import cardgamecard.CGCardSelector;
import cardgameplayer.CGPlayer;
import cardgamescreen.CGBattleScreenModel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class BattleJFrame extends javax.swing.JFrame {
public class BattlePanel extends JPanel{
private CGBattleScreenModel mBattleScreenModel;
public BattlePanel( CGBattleScreenModel battle_screen_model ) {
setBorder(BorderFactory.createLineBorder(Color.black));
mBattleScreenModel = battle_screen_model;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(550,300);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("This is my custom Panel!",10,20);
}
public CGPlayer Player( int index ){
return mBattleScreenModel.Player(index);
}
}
public class PlayerPanel extends JPanel implements MouseListener{
private CGPlayer mPlayer;
public PlayerPanel( CGPlayer player ) {
setBorder(BorderFactory.createLineBorder(Color.red));
mPlayer = player;
addMouseListener(this);
//CGCardSelector selector = new CGCardSelector(player);
//selector.RandomCardSelect();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
//Надо нарисовать пирамиду боевых карт
BufferedImage image;
BufferedImage image_border = ImageIO.read(new File("img//border.png"));
//Рисуем пехоту
//рисуем карты
CGBaseCard[] cards = mPlayer.BattleCards().BattleCards();
int size_x = 12, size_y = 20;
int start_x = 50, start_y = 5;
for( int i = 0; i < CGBattleSetCard.MAX_CARD_FROM_SET - 1; i++ ){
if(i == 6){ //Закончился ряд с пехотой
start_x = 65; start_y = 30;
}
if(i == 10){ //Закончился ряд с башнями
start_x = 80; start_y = 55;
}
if( i == 12 ){ //Закончился ряд с поддержкой
start_x = 95; start_y = 80;
}
image = ImageIO.read(new File("img//" + cards[i].CardName() + ".png"));
g.drawImage(image, start_x, start_y, size_x, size_y, rootPane);
if(cards[i].isActive()){
g.drawLine(start_x, start_y, start_x, start_y + size_y);
g.drawLine(start_x + size_x, start_y, start_x + size_x, start_y + size_y);
g.drawLine(start_x, start_y, start_x + size_x, start_y);
g.drawLine(start_x, start_y + size_y, start_x + size_x, start_y + size_y);
}
start_x += size_x + 3;
if( i == 2 || i == 7 || i == 10 ) //Оставим место под героя и герб
start_x += size_x + 3;
}
} catch (IOException ex) {
Logger.getLogger(BattleJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void mouseClicked(MouseEvent e) {
int card_pos = -1;
int x = e.getX(), y = e.getY();
if(y > 5 && y < 30 ){
//Тыкнули по пехоте
if(x > 50 && x < 65)
card_pos = CGBaseCard.idxSOLDIER_1;
else if(x > 65 && x < 80)
card_pos = CGBaseCard.idxSOLDIER_2;
else if(x > 80 && x < 95)
card_pos = CGBaseCard.idxSOLDIER_3;
else if(x > 110 && x < 125)
card_pos = CGBaseCard.idxSOLDIER_4;
else if(x > 125 && x < 140)
card_pos = CGBaseCard.idxSOLDIER_5;
else if(x > 140 && x < 155)
card_pos = CGBaseCard.idxSOLDIER_6;
}
else if( y > 30 && y < 55 ){
//Тыкнули по башням
if(x > 65 && x < 80)
card_pos = CGBaseCard.idxTOWER_1;
else if(x > 80 && x < 95)
card_pos = CGBaseCard.idxTOWER_2;
else if(x > 110 && x < 125)
card_pos = CGBaseCard.idxTOWER_3;
else if(x > 125 && x < 140)
card_pos = CGBaseCard.idxTOWER_4;
}
else if(y > 55 && y < 80 ){
//тыкнули по поддержке
if(x > 80 && x < 95)
card_pos = CGBaseCard.idxSUPPORT_1;
else if(x > 110 && x < 125)
card_pos = CGBaseCard.idxSUPPORT_2;
}
else if(y > 80 && y < 105 ){
//Тыкнули в гигантского
if(x > 95 && x < 110)
card_pos = CGBaseCard.idxBIG_MONSTER_1;
}
if(card_pos > -1){
((SelectCardPanel)panelPlayer2).setCardToPosition(card_pos);
repaint();
}
}
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
}
public class SelectCardPanel extends JPanel implements MouseListener{
private CGPlayer mPlayer;
private CGCardSelector mCardSelector;
private int mTypeSelect;
public SelectCardPanel( CGPlayer player, int type_select ) {
setBorder(BorderFactory.createLineBorder(Color.red));
mTypeSelect = type_select;
mPlayer = player;
mCardSelector = new CGCardSelector(player);
mCardSelector.Init( 550 );
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
try {
//Рисуем стрелку слева
BufferedImage image = ImageIO.read(new File("img//leftArrow.png"));
g.drawImage(image, 0, 30, rootPane);
//рисуем карты
int start_x = 40;
List<CGBaseCard> cards = mCardSelector.VisibleCard();
BufferedImage image_border = ImageIO.read(new File("img//border.png"));
BufferedImage image_disable = ImageIO.read(new File("img//disable.png"));
for(CGBaseCard card: cards){
image = ImageIO.read(new File("img//" + card.CardName() + ".png"));
g.drawImage(image, start_x, 30, rootPane);
if(card.isActive())
g.drawImage(image_border, start_x - 1, 29, rootPane);
if(card.isBattle())
g.drawImage(image_disable, start_x, 30, rootPane);
start_x += 60;
}
//рисуем стрелку справа
image = ImageIO.read(new File("img//rightArrow.png"));
g.drawImage(image, 530, 30, rootPane);
} catch (IOException ex) {
Logger.getLogger(BattleJFrame.class.getName()).log(Level.SEVERE, null, ex);
}
g.drawString("I'am selectCard panel!!",10,20);
g.drawString( String.valueOf(mCardSelector.VisibleCard().size()),10,80);
}
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX(), y = e.getY();
if( x > 0 && x < 50){
mCardSelector.PrevCard();
repaint();
}else if(x > 500 && x < 550 ){
mCardSelector.NextCard();
repaint();
}
else{
int card_idx = (x - 40) / 60;
mCardSelector.SelectCard(card_idx);
repaint();
}
}
@Override
public void mousePressed(MouseEvent e) { }
@Override
public void mouseReleased(MouseEvent e) { }
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) { }
public void setCardToPosition( int pos ){
mCardSelector.SetCardToBattlePos(pos);
repaint();
}
public void setPlayer(CGPlayer player){
mPlayer = player;
}
}
public BattleJFrame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new BattlePanel(new CGBattleScreenModel());
panelPlayer1 = new PlayerPanel(((BattlePanel)jPanel1).Player(0));
panelPlayer2 = new SelectCardPanel(((BattlePanel)jPanel1).Player(0), CGCardSelector.CARD_SELECT_MANUAL);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
panelPlayer1.setName("");
javax.swing.GroupLayout panelPlayer1Layout = new javax.swing.GroupLayout(panelPlayer1);
panelPlayer1.setLayout(panelPlayer1Layout);
panelPlayer1Layout.setHorizontalGroup(
panelPlayer1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 685, Short.MAX_VALUE)
);
panelPlayer1Layout.setVerticalGroup(
panelPlayer1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 183, Short.MAX_VALUE)
);
panelPlayer2.setName("");
javax.swing.GroupLayout panelPlayer2Layout = new javax.swing.GroupLayout(panelPlayer2);
panelPlayer2.setLayout(panelPlayer2Layout);
panelPlayer2Layout.setHorizontalGroup(
panelPlayer2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 0, Short.MAX_VALUE)
);
panelPlayer2Layout.setVerticalGroup(
panelPlayer2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 191, Short.MAX_VALUE)
);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(panelPlayer1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelPlayer2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addComponent(panelPlayer2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelPlayer1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel panelPlayer1;
private javax.swing.JPanel panelPlayer2;
// End of variables declaration//GEN-END:variables
}