//http://www.java-forum.org/de/viewtopic.php?p=315684#315684
import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.io.*;
import java.awt.print.*;
class AC_ferflixteVier extends JFrame
{
int widthPane=1000,heightPane=400;
//merken
int felder0Oder2=-1;
int amStart;
int letztesDran;
CMeineCanvas fieldC,blueC,redC;
CMeinThread rotZaehler,blauZaehler;
public static void main(String[] args)
{
AC_ferflixteVier ferflixteVier=new AC_ferflixteVier("Die ferflixte vier");
ferflixteVier.setUndecorated(false);
ferflixteVier.pack();
ferflixteVier.setSize(ferflixteVier.widthPane,ferflixteVier.heightPane);
ferflixteVier.setResizable(true);
ferflixteVier.setLocation(50,50);
ferflixteVier.show();
}
AC_ferflixteVier(String titel)
{
super(titel);
//LAYOUT >
getContentPane().setLayout(new BorderLayout());
JPanel ueberschrift=new JPanel();
getContentPane().add("North",ueberschrift);
JPanel unten=new JPanel();
unten.setLayout(new GridLayout(1,3));
getContentPane().add("Center",unten);
JPanel blau=new JPanel();
JPanel rot=new JPanel();
JPanel feld=new JPanel();
blau.setLayout(new GridLayout(1,1));
rot.setLayout(new GridLayout(1,1));
feld.setLayout(new GridLayout(1,1));
ueberschrift.setLayout(new GridLayout(1,1));
//blau.setBackground(Color.white);
//rot.setBackground(Color.white);
ueberschrift.setBackground(Color.black);
feld.setBackground(Color.white);
ueberschrift.setForeground(Color.white);
unten.add(blau);
unten.add(feld);
unten.add(rot);
//Layout <
// ZWEITE EINFACHE SCHICHT >
JLabel ueberschrift_JLabel=new JLabel("Die verflixte vier",SwingConstants.CENTER);
ueberschrift_JLabel.setForeground(Color.white);
ueberschrift.add(ueberschrift_JLabel);
blueC=new CMeineCanvas("blue");
redC=new CMeineCanvas("red");
fieldC=new CMeineCanvas("field");
//Threads
rotZaehler=new CMeinThread(redC,10,10,10,1,1);
rotZaehler.start();
blauZaehler=new CMeinThread(blueC,40,100,100,1,1);
blauZaehler.start();
rot.add(redC);
blau.add(blueC);
feld.add(fieldC);
// ZWEITE EINFACHE SCHICHT <
//MENUBAR >
JMenuBar myMenuBar=new JMenuBar();
setJMenuBar(myMenuBar);
JMenu view=new JMenu("Anzeige");
myMenuBar.add(view);
JPopupMenu breakup=new JPopupMenu("Auflösung");
view.add(breakup);
//breakup.setMnemonic(KeyEvent.VK_S);
JMenu a400=new JMenu("400/160");
JMenu a800=new JMenu("800/320");
JMenu a1000=new JMenu("1000/400");
JMenu a1200=new JMenu("1200/480");
breakup.add(a400);
breakup.add(a800);
breakup.add(a1000);
breakup.add(a1200);
//MENUBAR <
}
//CMeinMouseMotion CMeinMouseMotion CMeinMouseMotion CMeinMouseMotion
class CMeinMouseListener extends MouseAdapter
{
String can;
int felder3Breite=(int)(widthPane/3);
CMeinMouseListener(String can)
{
this.can=can;
}
//mousePressed(mousePressed(mousePressed(mousePressed(mousePressed(
public void mousePressed(MouseEvent e)
{
//System.out.println("rot: "+redC.countBall);
//System.out.println("blau: "+blueC.countBall);
int m_Xpos=e.getX();
int m_Ypos=e.getY();
if(m_Xpos<felder3Breite && can.equals("blue"))
{
if(felder0Oder2!=1)
felder0Oder2=1;
else
felder0Oder2=-1;
System.out.println("blau");
//blauZaehler.start();
}
if(m_Xpos>1 && can.equals("red"))
{
if(felder0Oder2!=2)
felder0Oder2=2;
else
felder0Oder2=-1;
System.out.println("rot");
//rotZaehler.start();
}
//rotZaehler=new CMeinThread(redC,10,10,10,1,1);
//rotZaehler.start();
}
//mouseReleased(mouseReleased(mouseReleased(mouseReleased(mouseReleased(
public void mouseReleased(MouseEvent e)
{
int m_Xpos=e.getX();
int m_Ypos=e.getY();
//zum Feld
if(((can.equals("field") || m_Xpos>=felder3Breite) && felder0Oder2==1) || ((can.equals("field") || m_Xpos<=1) && felder0Oder2==2))
{
if (felder0Oder2==letztesDran)
{
JOptionPane.showMessageDialog(null,"Sie waren gerade trann.");
}
else
{
String fehler=fieldC.chipInField(m_Xpos,m_Ypos,felder0Oder2);
//letztesDran=felder0Oder2;
felder0Oder2=-1;
//fieldC.repaint();//neu malen
if(fehler.length()>0)
JOptionPane.showMessageDialog(null,fehler,"Fehler",JOptionPane.INFORMATION_MESSAGE);
}
}
blauZaehler=new CMeinThread(blueC,40,100,100,1,1);
blauZaehler.start();
}
}
public class CMeinThread extends Thread
{
int sekPerRunde;
int uebrigeZeit;
JComponent zaehler;
int posX,posY,hoehe,breite;
CMeinThread(JComponent feld,int sekPerRunde,int posX,int posY,int hoehe,int breite)
{
this.sekPerRunde=sekPerRunde;
zaehler=feld;
}
public void run()
{
while(1==1)
{
//System.out.println("das wird gemacht: ");
repaint();
try{
Thread.sleep(380);
}
catch(InterruptedException e)
{
return;
}
}
}
public void paint(Graphics g)
{
g=zaehler.getGraphics();
g.fillOval(1,1,20,20);
g.drawLine(1,23,34,5);
g.drawLine(234,33,32,56);
g.setColor(Color.green);
g.fillOval(33,22,4,4);
g.fillOval(34,4,4,55);
g.dispose();
}
}
//CMeineCanvas CMeineCanvas CMeineCanvas CMeineCanvas CMeineCanvas CMeineCanvas
class CMeineCanvas extends JComponent
{
String can;
int heightCan=(int)(heightPane*0.8),widthCan=widthPane/3;
int widthCanField;
//field
int widthField=6,heightField=7;
int startField;
//pyramiden
boolean erfolgreich;//kann ein Chip entfert werden
int[][] chipIn=new int[heightField+1][widthField+1];
int size=(int)(widthCan * 0.085);//:2
int widthBallsUp=9;
int widthB;
int countBall;
int zahl;
CMeineCanvas(String can)
{
setBackground(Color.white);
setForeground(Color.black);
this.can=can;
addMouseListener(new CMeinMouseListener(can));
//count balls
countBall=widthBallsUp;
int stockBall=widthBallsUp;
do
{
stockBall-=2;
countBall+=stockBall;
}while(stockBall>0);
//startcount of balls
}
public void paint(Graphics g)
{
super.paint(g);
if(!can.equals("field"))
{
g.translate((widthCan/2),0);
if(can.equals("blue"))
{
setForeground(Color.blue);
}
if(can.equals("red"))
{
setForeground(Color.red);
}
//anzahl untere Blöcke
int heightStart=heightCan-size*2;
int widthStart=0-size;
//System.out.println("Breite: "+this.widthB);
widthB=widthBallsUp;
int anzeigen=0;
int i=0;
//System.out.println("countBall: "+countBall);
while(this.widthB>0)
{
if(countBall>=anzeigen)
g.fillOval(widthStart,heightStart,size,size);
anzeigen++;
for(int j=1;j<=(widthB/2);j++)
{
if(countBall>=anzeigen)
g.fillOval(widthStart-j*size,heightStart,size,size);
anzeigen++;
if(countBall>=anzeigen)
g.fillOval(widthStart+j*size,heightStart,size,size);
anzeigen++;
}
i++;
heightStart=heightStart-size;
widthB-=2;
}
}
else// field
{
if(letztesDran==1)
g.drawString("Spieler: rot",1,(int)(heightField*2.0));
if(letztesDran==2)
g.drawString("Spieler: blau",1,(int)(heightField*2.0));
startField=heightCan-heightField*size-size*2;
Color colorChip;
widthCanField=(widthCan-widthField*size)/2;
g.translate(widthCanField,0);
for(int i=1;i<=heightField;i++)
{
for(int j=1;j<=widthField;j++)
{
g.setColor(Color.black);
g.drawRect(j*size-size,size*i+startField,size,size);
if(chipIn[i-1][j-1]==1|| chipIn[i-1][j-1]==2)
{
if(chipIn[i-1][j-1]==1)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.fillOval(j*size-size,size*i+startField,size,size);
}
}
}
}
}
Dimension setPreferredSize()
{
return new Dimension(widthCan,heightCan);
}
Dimension setMinimumSize()
{
return setPreferredSize();
}
//chipInField(chipInField(chipInField(chipInField(chipInField(
String chipInField(int m_Xpos,int m_Ypos,int felder0Oder2)
{
erfolgreich=false;
// widthCanField;
for(int i=(widthCanField),aktWidth=0;i<=(widthField*size+widthCanField-size);i=i+size,aktWidth++)
{
if(i<=m_Xpos && (i+size)>m_Xpos)
{
int j=heightField-1;
while(chipIn[j][aktWidth]!=0)
{
j--;
if(j<0)
return "Die Spalte ist schon voll!";
}
erfolgreich=true;
if(felder0Oder2==1 && blueC.countBall>=0)
{
blueC.countBall=blueC.countBall-1;
blueC.CMyRepaint();
chipIn[j][aktWidth]=felder0Oder2;
System.out.println("blau: "+chipIn[j][aktWidth]+"j:"+j+"aktWidth:"+aktWidth+"felder0Oder2:"+felder0Oder2);
letztesDran=1;
System.out.println("letztesDran"+letztesDran);
}
if(felder0Oder2==2 && redC.countBall>=0)
{
redC.countBall=redC.countBall-1;
redC.CMyRepaint();
chipIn[j][aktWidth]=felder0Oder2;
System.out.println("rot: "+chipIn[j][aktWidth]+"j:"+j+"aktWidth:"+aktWidth+"felder0Oder2:"+felder0Oder2);
letztesDran=2;
System.out.println("letztesDran"+letztesDran);
}
}
}
repaint();
return "";
}
void CMyRepaint()
{
repaint();
}
}
}