import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ImageObserver;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public abstract class Card extends JPanel implements ImageObserver, MouseListener
{
public Card()
{
super();
this.setBorder(BorderFactory.createLineBorder(Color.red));
this.addMouseListener(this);
}
int i, pacmanAngle1=45, pacmanAngle2=280 ;
Color squareColor;
double relativeSize;
boolean disguised=false;
Color black = new Color(0,0,0);
Color white = new Color(255, 255, 255);
File fhLogo = new File("bilder");
Image myPhoto=Toolkit.getDefaultToolkit().getImage("bilder");
public void setColor(Color color)
{
squareColor = color;
}
public void setPhotoNumber(int photoNumber)
{
i=photoNumber;
}
public void setRelativeSize(double size)
{ if(size>=100)
{
relativeSize = 100;
}
relativeSize = size;
}
public void disguise()
{
disguised =! disguised;
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
Dimension mySize = this.getSize();
int length=Math.min(mySize.height,mySize.width);
int size = (int)(length*relativeSize/100);
if(disguised)
{
g.drawImage(myPhoto, 0, 0, mySize.width, mySize.height,null);
pacmanAngle1= pacmanAngle1+90;
}
else
{
g.setColor(squareColor);
specialPaint(g, size,mySize);
}
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
}
@Override
public void mouseClicked(MouseEvent me)
{
if (me.getButton() == 1)
{
disguise();
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.Dimension;
import java.awt.Graphics;
public class Circle extends Card
{
public Circle()
{
super();
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
g.fillOval(mySize.width/2-size/2, mySize.height/2-size/2, size, size);
}
}
import java.awt.Color;
import java.util.Arrays;
import java.util.Random;
public class Main {
/**
* @param args
*/
public static void main(String[] args)
{
int myCardArrayLength=64;
MyFrame frame = new MyFrame(600,600,"Fenster");
Card[] myCards = new Card[myCardArrayLength+1];
Card temp;
int photoNumber= 0;
for(int j=1; j<(myCardArrayLength/2)+1; j++)
{
int rand = (int)(Math.random()*6);
Color zufallsFarbe = Farbe();
double relativeGroeße = relativeGroeße();
//verhindert das bilder doppelt geladen werden
if (photoNumber>=6)
{
rand=(int)(Math.random()*5);
}
for(int i=0;i<2;i++)
{
switch (rand)
{
case 0: myCards[j*i] = new Square();
break;
case 1 : myCards[j*i] = new PacMan();
break;
case 2: myCards[j*i] = new Triangle();
break;
case 3: myCards[j*i] = new Circle();
break;
case 4: myCards[j*i] = new MySpecialShape();
break;
case 5: myCards[j*i]= new Photo();
myCards[j*i].setPhotoNumber(photoNumber);
break;
default: myCards[j*i] = new Circle();
break;
}
frame.add(myCards[j*i]);
myCards[j*i].setRelativeSize(relativeGroeße);
myCards[j*i].setColor(zufallsFarbe);
}
//geht zum naechsten foto des verzeichnises ueber
if(rand==5)
{
photoNumber++;
}
}
for(int l= 0;l<myCardArrayLength*2;l++)
{
int z1;
int z2;
do
{
z1=Randomize(myCardArrayLength);
z2=Randomize(myCardArrayLength);
}while(z1!=z2);
temp=myCards[z1];
myCards[z1]=myCards[z2];
myCards[z2]=temp;
}
for(int k=1; k<myCardArrayLength+1;k++)
{
frame.add(myCards[k]);
}
}
private static int Randomize(int range)
{
double rawResult;
rawResult = Math.random();
return (int) (rawResult * range);
}
public static void shuffle1(Card[] array)
{
for(int i= 0;i<10000;i++)
{
int z1=(int) (Math.random()*64);
int z2=(int) (Math.random()*64);
array[z1]=array[z2];
array[z2]=array[z1];
}
}
public static void shuffle (Card[] array)
{
Random rng = new Random();
int n = array.length;
while (n > 0)
{
int k = rng.nextInt(n);
n--;
Card temp = array[n];
array[n] = array[k];
array[k] = temp;
}
}
public static Color Farbe()
{
Color zufallsFarbe = new Color((int)(255*Math.random()),(int)(255*Math.random()),(int)(255*Math.random()));
return zufallsFarbe;
}
public static double relativeGroeße()
{
double relativeSize =30+Math.random()*60;
return relativeSize;
}
}
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.JFrame;
public class MyFrame extends JFrame
{
public MyFrame(int a, int b, String title )
{
super();
setSize(a,a);
setTitle(title);
setLocation(b,0);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(8,8,a/64,a/64));
setVisible(true);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
public class MySpecialShape extends Card
{
public MySpecialShape()
{
super();
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
int x [] = new int[4];
int y [] = new int[4];
x[0] = mySize.width/2 -size/2;
x[1] = mySize.width/2 -size/3 ;
x[2] = mySize.width/2 +size/3;
x[3] = mySize.width/2 + size/2;
y[0] = mySize.height/2 + size/2;
y[1] = mySize.height/2-size/2;
y[2] = mySize.height/2-size/2;
y[3] = mySize.height/2 + size/2;
g.fillPolygon(x,y,4);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
public class PacMan extends Card
{
public PacMan()
{
super();
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
g.fillArc(mySize.width/2-size/2, mySize.height/2-size/2, size, size, pacmanAngle1 ,pacmanAngle2);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
public class Photo extends Card
{
public Photo()
{
super();
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
File myPhotoDirectory = new File("bilder");
File [] myPhotos = myPhotoDirectory.listFiles();
Image picture = Toolkit.getDefaultToolkit().getImage(myPhotos[i].getPath());
g.drawImage(picture, 0, 0, mySize.width, mySize.height,null);
repaint();
}
}
import java.awt.Dimension;
import java.awt.Graphics;
public class Square extends Card
{
public Square()
{
super();
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
g.fillRect(mySize.width/2-size/2, mySize.height/2-size/2, size, size);
}
}
import java.awt.Dimension;
import java.awt.Graphics;
public class Triangle extends Card
{
public Triangle()
{
super();
}
public void specialPaint(Graphics g, int size, Dimension mySize)
{
int x [] = new int[3];
int y [] = new int[3];
x[0] = mySize.width/2 -size/2;
x[1] = mySize.width/2 ;
x[2] = mySize.width/2 + size/2;
y[0] = mySize.height/2 + size/2;
y[1] = mySize.height/2-size/2;
y[2] = mySize.height/2 + size/2;
g.fillPolygon(x,y,3);
}
}