#3 Kit
Tohle by měl být kompletní zdroják i s obrázky.
Díky moc za trpělivost, nejsem ve těchto ohledech moc zkušený.
package cizek.kostky;
import java.util.*;
import cizek.random.Random;
import cizek.vyjimky.ChybnyVyber;
public class Hrac {
String jmenoHrace;
int pocetKostek;
Okno owner;
int[] hozeneKostky;
int[] skoreJednotlivychKol;
int skoreCelkove;
int kolo;
static int[] vybraneKostky;
Hrac(String jmeno, Okno ok, int pocetKol) {
jmenoHrace = jmeno;
pocetKostek = 6;
owner = ok;
skoreCelkove = 0;
skoreJednotlivychKol = new int[pocetKol];
kolo = 1;
}
public void hodKostek() {
hozeneKostky = null;
hozeneKostky = new int[pocetKostek];
for (int i = 0; i < pocetKostek; i++) {
hozeneKostky[i] = Random.nahodneCislo(1, 6);
}
owner.kostky.vlozitKostky(hozeneKostky, pocetKostek);
}
public void vybratKostky(int[] indexyVybranych, int pocetVybranych) {
vybraneKostky = null;
vybraneKostky = new int[pocetVybranych];
for (int i = 0; i < pocetVybranych; i++) {
vybraneKostky[i] = hozeneKostky[indexyVybranych[i]];
}
}
public int vypocetSkore(int[] indexyVybranych, int pocetVybranych, boolean zapsatDoSkoreKola) throws ChybnyVyber {
int skore = 0;
int pocetStejnych;
int cislo;
boolean rada;
int zacatek;
vybratKostky(indexyVybranych, pocetVybranych);
Arrays.sort(vybraneKostky);
for(int u = 0; (u<2)&&(vybraneKostky.length>2); u++) {
zacatek = 0;
cislo = 0;
pocetStejnych = 1;
rada = false;
for (int i = 0; i < vybraneKostky.length; i++) {
if (i != 0) {
if (vybraneKostky[i] == vybraneKostky[i - 1]) {
cislo = vybraneKostky[i];
pocetStejnych++;
rada = true;
zacatek = i-1;
} else {
if (rada && pocetStejnych > 2) {
if (cislo != 1) {
skore = skore + cislo * 100 * (pocetStejnych - 2);
} else {
skore = skore + 1000 * (pocetStejnych - 2);
}
odendaniKostek(zacatek, pocetStejnych);
}
zacatek = 0;
cislo = 0;
pocetStejnych = 1;
rada = false;
}
}
}
}
if(vybraneKostky.length != 0){
for(int i = 0; i < vybraneKostky.length;i++){
if(vybraneKostky[i] == 1 | vybraneKostky[i] == 1 ){
skore = skore+((vybraneKostky[i]==1)?100:50);
}else{
if(zapsatDoSkoreKola){
throw new ChybnyVyber(vybraneKostky[i]);
}
}
}
}
if(zapsatDoSkoreKola){
skoreJednotlivychKol[kolo-1] = skore;
kolo++;
}
return skore;
}
public static void odendaniKostek(int zacatek, int pocetKostek){
int[] pomocna = new int[vybraneKostky.length-pocetKostek];
int index = 0;
for(int i = 0; i<vybraneKostky.length;i++){
if(i == zacatek){
i = i+pocetKostek-1;
}else{
pomocna[index]=vybraneKostky[i];
index++;
}
}
vybraneKostky = pomocna;
}
}
package cizek.kostky;
import java.awt.*;
import java.awt.event.*;
import cizek.obrazky.*;
public class Okno extends Frame {
Obrazky obr;
//Hrac[] seznamHracu;
Hrac zkusebniHrac;
Label nadpis1, popHrace, aktHrac, nadpis2;
Button butt1, butt2;
Label vyberLB, skoreCelkoveLB, skoreKolaLB;
TextField vyberTF, skoreCelkoveTF, skoreKolaTF;
Label[] kostkyLB;
Platno[] kostkzPL;
Checkbox[] kostkyCHB;
VypisKostek kostky;
GridBagLayout gbl;
GridBagConstraints gbc;
int aktualniHrac;
static Toolkit t;
Okno(){
super("Kostky");
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
this.setLayout(gbl);
kostky = new VypisKostek(6);
obr = new Obrazky();
zkusebniHrac = new Hrac("Tomáš", this,1000);
sestaveniOkna();
Toolkit t = Toolkit.getDefaultToolkit();
butt1.addActionListener(new Butt1AL());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
this.addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e){
System.out.println("Cele okno: "+Okno.this.getWidth()+"x"+Okno.this.getHeight());
System.out.println("Panel: " + kostky.getWidth()+"x"+kostky.getHeight());
}
});
this.pack();
this.setIconImage(obr.getIkonka());
this.setLocation(((t.getScreenSize().width-this.getWidth())/2),(t.getScreenSize().height-this.getHeight())/4);
this.setResizable(false);
}
private void sestaveniOkna(){
//kostky.setBackground(Color.MAGENTA);
nadpis1 = new Label("KOSTKY",Label.CENTER);
nadpis1.setFont(new Font("SansSerif",Font.BOLD, 100));
//nadpis1.setBackground(Color.yellow);
nadpis2 = new Label("HOZENÉ KOSTKY",Label.CENTER);
nadpis2.setFont(new Font("SansSerif",Font.PLAIN,25));
//nadpis2.setBackground(Color.orange);
popHrace = new Label("Hráč na řadě:",Label.LEFT);
popHrace.setFont(new Font("SansSerif",Font.PLAIN,35));
//popHrace.setBackground(Color.blue);
aktHrac = new Label(zkusebniHrac.jmenoHrace,Label.CENTER); //////upravit!!!!!
aktHrac.setFont(new Font("SansSerif",Font.PLAIN,35));
//aktHrac.setBackground(Color.pink);
vyberLB = new Label("Výběr:",Label.RIGHT);
vyberLB.setFont(new Font("SansSerif",Font.PLAIN,20));
//vyberLB.setBackground(Color.cyan);
vyberTF = new TextField("0", 4);
vyberTF.setFont(new Font("SansSerif",Font.PLAIN,20));//////////////////////////////////////////
vyberTF.setEditable(false);
//vyberTF.setBackground(Color.green);
skoreCelkoveLB = new Label("Celkové skóre:",Label.RIGHT);
skoreCelkoveLB.setFont(new Font("SansSerif",Font.PLAIN,20));
//skoreCelkoveLB.setBackground(Color.blue);
skoreCelkoveTF = new TextField("0",6);
skoreCelkoveTF.setFont(new Font("SansSerif",Font.PLAIN,20));///////////////////////////
skoreCelkoveTF.setEditable(false);
//skoreCelkoveTF.setBackground(Color.yellow);
skoreKolaLB = new Label("Skóre kola:",Label.RIGHT);
skoreKolaLB.setFont(new Font("SansSerif",Font.PLAIN,20));
//skoreKolaLB.setBackground(Color.orange);
skoreKolaTF = new TextField("0",5);
skoreKolaTF.setFont(new Font("SansSerif",Font.PLAIN,20));///////////////////
skoreKolaTF.setEditable(false);
//skoreKolaTF.setBackground(Color.pink);
butt1 = new Button("Hodit");
butt1.setFont(new Font("SansSerif",Font.PLAIN,20));
//butt1.setBackground(Color.yellow);
butt2 = new Button("Konec tahu");
butt2.setFont(new Font("SansSerif",Font.PLAIN,20));
//butt2.setBackground(Color.gray);
Panel pan1 = new Panel();
pan1.setLayout(new GridLayout(1,2));
Panel pan2 = new Panel();
pan2.setLayout(new GridLayout(1,2));
Panel pan3 = new Panel();
pan3.setLayout(new GridLayout(1,2));
pan1.add(skoreCelkoveLB);
pan1.add(skoreCelkoveTF);
pan2.add(skoreKolaLB);
pan2.add(skoreKolaTF);
pan3.add(vyberLB);
pan3.add(vyberTF);
gbc.fill = GridBagConstraints.HORIZONTAL;
vlozKomponentu(new VodorovnaCara(),0,0,GridBagConstraints.REMAINDER,1,0,0,0,1,new Insets(0,0,0,0));
vlozKomponentu(nadpis1,0,1,GridBagConstraints.REMAINDER,1,0,0,0,0,new Insets(10,0,0,0));
vlozKomponentu(new VodorovnaCara(),0,2,GridBagConstraints.REMAINDER,1,0,0,0,1,new Insets(0,0,20,0));
vlozKomponentu(popHrace,0,3,2,1,0,0,0,0,new Insets(0,0,20,50));
vlozKomponentu(aktHrac,2,3,GridBagConstraints.REMAINDER,1,0,0,0,0,new Insets(0,0,20,0));
gbc.fill = GridBagConstraints.NONE;
vlozKomponentu(nadpis2,0,4,GridBagConstraints.REMAINDER,1,0,0,0,0,new Insets(0,0,10,0));
vlozKomponentu(kostky,0,5,GridBagConstraints.REMAINDER,1,0,0,0,0,new Insets(0,0,0,0));
vlozKomponentu(pan1,0,6,1,1,0,0,0,0,new Insets(25,0,0,10));
vlozKomponentu(pan2,1,6,1,1,0,0,0,0,new Insets(25,0,0,10));
vlozKomponentu(pan3,2,6,1,1,0,0,0,0,new Insets(25,0,0,0));
gbc.fill = GridBagConstraints.BOTH;
vlozKomponentu(butt1,3,6,1,1,0,0,20,10,new Insets(25,50,0,0));
vlozKomponentu(butt2,4,6,GridBagConstraints.REMAINDER,1,0,0,20,0,new Insets(25,10,0,0));
butt2.setEnabled(false);
}
public void vlozKomponentu(Component co, int gridx, int gridy, int gridwidth, int gridheight, int weightx, int weighty, int ipadx, int ipady, Insets ins){
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.ipadx = ipadx;
gbc.ipady = ipady;
gbc.insets = ins;
gbl.setConstraints(co, gbc);
this.add(co);
}
public Insets getInsets(){
return new Insets(48,25,25,25);
}
class Butt1AL implements ActionListener {
public void actionPerformed(ActionEvent e){
int[] pomocna = {1,2,3,4,5};
kostky.vlozitKostky(pomocna, pomocna.length);
}
}
public static void main(String[] args){
Okno ok = new Okno();
ok.setVisible(true);
}
}
package cizek.kostky;
import java.awt.*;
public class Platno extends Canvas {
Image img;
boolean vykreslit;
public Platno(Image img) {
this.img = img;
this.vykreslit = vykreslit;
}
public void paint(Graphics g){
Dimension d = getSize();
int sirka = (d.width>d.height)?d.width:d.height;
g.drawImage(img,0,0,sirka,sirka,this);
}
public void nastavitObrazek(Image img){
this.img = img;
repaint();
}
}
package cizek.kostky;
import java.awt.*;
import cizek.obrazky.Obrazky;
public class VypisKostek extends Panel {
Platno[] kostkyPL;
Checkbox[] kostkyCH;
int pocetVypsanychKostek;
GridBagConstraints gbc;
GridBagLayout gbl;
Obrazky obr;
Image[] obrKostek;
VypisKostek(int pocetKostek) {
obr = new Obrazky();
obrKostek = obr.getAll();
gbc = new GridBagConstraints();
gbl = new GridBagLayout();
this.setLayout(gbl);
kostkyPL = new Platno[pocetKostek];
kostkyCH = new Checkbox[pocetKostek];
for (int i = 0; i < pocetKostek; i++) {
kostkyPL[i] = new Platno(obrKostek[0]);
kostkyPL[i].setSize(150, 150);
vlozKomponentu(kostkyPL[i], i, 0, 1, 1, 0, 0);
}
for (int i = 0; i < pocetKostek; i++) {
kostkyCH[i] = new Checkbox((i + 1) + ". kostka");
kostkyCH[i].setFont(new Font("SansSerif", Font.PLAIN, 14));
vlozKomponentu(kostkyCH[i], i, 1, 1, 1, 0, 0);
}
pocetVypsanychKostek = 6;
}
public void vlozKomponentu(Component co, int gridx, int gridy, int gridwidth, int gridheight, int weightx, int weighty) {
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
gbc.weightx = 1;
gbc.weighty = 1;
gbl.setConstraints(co, gbc);
this.add(co);
}
public Dimension getMinimumSize() {
return new Dimension(900, 173);
}
public Dimension getPreferredSize() {
return getMinimumSize();
}
public void vlozitKostky(int[] kostky, int pocetKostek) {
odstranitVse();
gbl = new GridBagLayout();
gbc = new GridBagConstraints();
this.setLayout(null);
this.setLayout(gbl);
for (int i = 0; i < pocetKostek; i++) {
vlozKomponentu(kostkyPL[i], i, 0, 1, 1, 0, 0);
kostkyPL[i].nastavitObrazek(obrKostek[kostky[i] - 1]);
}
for (int i = 0; i < pocetKostek; i++) {
vlozKomponentu(kostkyCH[i], i, 0, 1, 1, 0, 0);
}
pocetVypsanychKostek = pocetKostek;
}
public void odstranitVse() {
for (int i = 0; i < pocetVypsanychKostek; i++) {
this.remove(kostkyPL[i]);
}
for (int i = 0; i < pocetVypsanychKostek; i++) {
kostkyCH[i].setState(false);
this.remove(kostkyCH[i]);
}
}
}
package cizek.obrazky;
import java.awt.*;
import java.net.URL;
public class Obrazky {
private Image[] img;
private Image ikonka;
public Obrazky() {
Toolkit t = Toolkit.getDefaultToolkit();
img = new Image[6];
URL url1 = getClass().getResource("jedna.png");
img[0] = t.getImage(url1);
URL url2 = getClass().getResource("dva.png");
img[1] = t.getImage(url2);
URL url3 = getClass().getResource("tri.png");
img[2] = t.getImage(url3);
URL url4 = getClass().getResource("ctyri.png");
img[3] = t.getImage(url4);
URL url5 = getClass().getResource("pet.png");
img[4] = t.getImage(url5);
URL url6 = getClass().getResource("sest.png");
img[5] = t.getImage(url6);
URL url7 = getClass().getResource("kostka.png");
ikonka = t.getImage(url7);
}
public Image getImage1(){
return img[0];
}
public Image getImage2(){
return img[1];
}
public Image getImage3(){
return img[2];
}
public Image getImage4(){
return img[3];
}
public Image getImage5(){
return img[4];
}
public Image getImage6(){
return img[5];
}
public Image[] getAll(){
return img;
}
public Image getIkonka(){
return ikonka;
}
}
package cizek.obrazky;
import java.awt.*;
public class VodorovnaCara extends Canvas {
public void paint(Graphics g){
Dimension d = getSize();
g.fillRect(0,0,d.width,d.height);
}
}
package cizek.random;
public class Random {
public static int nahodneCislo(int min, int max) {
max++;
double nahoda = Math.random();
nahoda = (nahoda * (max - min)) + min;
int cele = (int) Math.floor(nahoda);
return (cele);
}
}
package cizek.vyjimky;
public class ChybnyVyber extends Exception {
public ChybnyVyber(int cislo) {
super("Nelze vybrat kostku č. " + cislo);
}
}
Připojen obrázek.
Připojen obrázek.
Připojen obrázek.
Připojen obrázek.
Připojen obrázek.
Připojen obrázek.
Připojen obrázek.