如何重新绘制我的JFrame来更新变量值?

时间:2013-06-12 03:55:36

标签: java swing jframe repaint static

package greed;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.*;
import java.util.Scanner;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;


public class Greed extends JPanel {

    public static BufferedImage slot1, slot2, slot3, slot4, slot5;

    public static int diceOne, diceTwo, diceThree, diceFour, diceFive = 0;

    public static String diceLoc1 = "C:/Users/Kameron/Downloads/Dice/DiceBlankResize.jpg";
    public static String diceLoc2 = "C:/Users/Kameron/Downloads/Dice/DiceBlankResize.jpg";
    public static String diceLoc3 = "C:/Users/Kameron/Downloads/Dice/DiceBlankResize.jpg";
    public static String diceLoc4 = "C:/Users/Kameron/Downloads/Dice/DiceBlankResize.jpg";
    public static String diceLoc5 = "C:/Users/Kameron/Downloads/Dice/DiceBlankResize.jpg";
    public static String diceLoc6 = "C:/Users/Kameron/Downloads/Dice/DiceBlankResize.jpg";

    public static boolean canRerollDiceOne = true;
    public static boolean canRerollDiceTwo = true;
    public static boolean canRerollDiceThree = true;
    public static boolean canRerollDiceFour = true;
    public static boolean canRerollDiceFive = true;

    public static double playerOneScore = 0;
    public static double playerTwoScore = 0;
    public static double playerThreeScore = 0;
    public static double playerFourScore = 0;

    public static double OneValue = 100;
    public static double TwoValue = 0;
    public static double ThreeValue = 0;
    public static double FourValue = 0;
    public static double FiveValue = 50;
    public static double SixValue = 0;

    public static int playerTurn = 0;

    /*
    public static int diceOneRoll;
    public static int diceTwoRoll;
    public static int diceThreeRoll;
    public static int diceFourRoll;
    public static int diceFiveRoll;
    */

    public static String choiceReroll = "";

    // The menus
    public static String menu = "";
    public static int rerollSel;

    public static byte[] contents;
    public static int pointer = 0;

    public static JFrame f = new JFrame("Greed");
    public static JMenuBar mb = new JMenuBar();
    public static JTextArea jTextArea1 = new JTextArea();
    public static JTextArea jTextArea2 = new JTextArea();
    public static JScrollPane jScrollPane1 = new JScrollPane(jTextArea1);
    public static JPanel jPanel1 = new JPanel();
    public static JLabel diceLab1 = new JLabel("Dice 1");
    public static JLabel diceLab2 = new JLabel("Dice 2");
    public static JLabel diceLab3 = new JLabel("Dice 3");
    public static JLabel diceLab4 = new JLabel("Dice 4");
    public static JLabel diceLab5 = new JLabel("Dice 5");

    public static Toolkit tk = Toolkit.getDefaultToolkit();  
    public static int xSize = ((int) tk.getScreenSize().getWidth());
    public static int ySize = 500;

    public static int height = ((int) tk.getScreenSize().getHeight());
    public static int width = ((int) tk.getScreenSize().getWidth());

    public static int slot1x, slot2x, slot3x, slot4x, slot5x;
    public static int sloty = 550;

    public static int x1Img1 = 250;
    public static int x2Img1 = 375;
    public static int x1Img2 = 495;
    public static int x2Img2 = 620;
    public static int x1Img3 = 740;
    public static int x2Img3 = 865;
    public static int x1Img4 = 985;
    public static int x2Img4 = 1110;
    public static int x1Img5 = 1230;
    public static int x2Img5 = 1355;
    public static int yLine = 550;
    public static int yImgSize = 200;

    public static void Greed() {
        f.setJMenuBar(mb);
        //jScrollPane1.setBounds(10,60,780,500);
        //jScrollPane1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    }

    public void setDie1(BufferedImage slot1) {
        Greed.slot1 = slot1;
    }
    public void setDie2(BufferedImage slot2) {
        this.slot2 = slot2;
    }
    public void setDie3(BufferedImage slot3) {
        this.slot3 = slot3;
    }
    public void setDie4(BufferedImage slot4) {
        this.slot4 = slot4;
    }
    public void setDie5(BufferedImage slot5) {
        this.slot5 = slot5;
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        //g.drawRect(250, 550, 120, 120);
        //g.drawRect(495, 550, 120, 120);
        //g.drawRect(740, 550, 120, 120);
        //g.drawRect(985, 550, 120, 120);
        //g.drawRect(1230, 550, 120, 120);
        g.drawImage(slot1, 250, sloty, this);
        g.drawImage(slot2, 495, sloty, this);
        g.drawImage(slot3, 740, sloty, this);
        g.drawImage(slot4, 985, sloty, this);
        g.drawImage(slot5, 1230, sloty, this);
    }

    public static void main(String[] args) throws IOException {
        Greed gui = new Greed();
        gui.launchFrame();
        //run();

        Container contentPane = f.getContentPane();
        contentPane.add(new Greed());

        Scanner is = new Scanner(System.in);
        System.out.println("Welcome to Greed...");
            do {
        System.out.println("Would you like to play? (yes/no)");
            menu = is.next();
                switch (menu) {
                    case "yes":
                        diceOne = 0;
                        diceTwo = 0;
                        diceThree = 0;
                        diceFour = 0;
                        diceFive = 0;
                        Game();

                        break;
                    case "no":
                        System.out.println("Goodbye...");
                        System.exit(0);

                        break;
                    default:
                        invalidInput();

                        break;
                }
            } while (!"yes".equals(menu) || !"no".equals(menu));
    }

    public static void Game() throws IOException {
        rollDiceOne();
        rollDiceTwo();
        rollDiceThree();
        rollDiceFour();
        rollDiceFive();

        displayDice();
        displayDiceValues();
        f.repaint();
        System.out.println(diceLoc1);

        choiceRerollDice();
    }

    public static void choiceRerollDice() {
        Scanner is = new Scanner(System.in);
            do {
                if (!canRerollDiceOne && !canRerollDiceTwo && !canRerollDiceThree && !canRerollDiceFour && !canRerollDiceFive) {
                System.out.println("Sorry, but you may not reroll any more dice...");
                displayDiceValues();
                System.exit(0);
            }
            else {
        System.out.println("Would you like to reroll any (more) dice? (yes/no)");
            choiceReroll = is.next();
                switch (choiceReroll) {
                    case "yes":
                        rerollDice();
                        break;
                    case "no":
                        //endTurn();
                        displayDice();
                        displayDiceValues();
                        f.repaint();
                        System.out.println("Thank you for playing!");
                        System.out.println("Goodbye!");
                        System.exit(0);
                    default:
                        invalidInput();
                    }
                }
            } while (!"yes".equals(choiceReroll) || !"no".equals(choiceReroll));
    }

    public static void rerollDice() {
        Scanner is = new Scanner(System.in);
        System.out.println("Which dice would you like to reroll? (1-5)");
        rerollSel = is.nextInt();
                switch (rerollSel) {
                    case 1:
                        if (canRerollDiceOne) {
                            diceOne = 0;
                            rollDiceOne();
                            canRerollDiceOne = false;
                        }
                        else {
                            System.out.println("Sorry, but you may not reroll this dice!");
                            choiceRerollDice();
                        }

                        break;
                    case 2:
                        if (canRerollDiceTwo) {
                            diceTwo = 0;
                            rollDiceTwo();
                            canRerollDiceTwo = false;
                            }
                        else {
                            System.out.println("Sorry, but you may not reroll this dice!");
                            choiceRerollDice();
                        }

                        break;
                    case 3:
                        if (canRerollDiceThree) {
                        diceThree = 0;
                        rollDiceThree();
                        canRerollDiceThree = false;
                        }
                        else {
                            System.out.println("Sorry, but you may not reroll this dice!");
                            choiceRerollDice();
                        }

                        break;
                    case 4:
                        if (canRerollDiceFour) {
                        diceFour = 0;
                        rollDiceFour();
                        canRerollDiceFour = false;
                        }
                        else {
                            System.out.println("Sorry, but you may not reroll this dice!");
                            choiceRerollDice();
                        }

                        break;
                    case 5:
                        if (canRerollDiceFive) {
                        diceFive = 0;
                        rollDiceFive();
                        canRerollDiceFive = false;
                        }
                        else {
                            System.out.println("Sorry, but you may not reroll this dice!");
                            choiceRerollDice();
                        }

                        break;
                    default:
                        System.out.println("Invalid dice number...");
                        System.out.println("Please enter a dice 1-5...");
                        rerollDice();

                        break;
                }
    }

    public static void invalidInput() {
        System.out.println("Invalid input...");
        System.out.println("Please enter either 'yes' or 'no'...");
    }

    public static void playerTurn() {
        switch (playerTurn) {
            case 1:

                break;
            case 2:

                break;
            case 3:

                break;
            case 4:

                break;
        }
    }

    public static void rollDiceOne() {
        diceOne = 1 + (int)(Math.random()*6);
    }
    public static void rollDiceTwo() {
        diceTwo = 1 + (int)(Math.random()*6);
    }
    public static void rollDiceThree() {
        diceThree = 1 + (int)(Math.random()*6);
    }
    public static void rollDiceFour() {
        diceFour = 1 + (int)(Math.random()*6);
    }
    public static void rollDiceFive() {
        diceFive = 1 + (int)(Math.random()*6);
    }

    public static void resetDiceOne() {
        diceOne = 0;
    }
    public static void resetDiceTwo() {
        diceTwo = 0;
    }
    public static void resetDiceThree() {
        diceThree = 0;
    }
    public static void resetDiceFour() {
        diceFour = 0;
    }
    public static void resetDiceFive() {
        diceFive = 0;
    }

    public static void displayDice() {
        switch (diceOne) {
            case 1:
                diceLoc1 = "C:/Users/Kameron/Downloads/Dice/Dice1Resize.jpg";
                break;
            case 2:
                diceLoc1 = "C:/Users/Kameron/Downloads/Dice/Dice2Resize.jpg";
                break;
            case 3:
                diceLoc1 = "C:/Users/Kameron/Downloads/Dice/Dice3Resize.jpg";
                break;
            case 4:
                diceLoc1 = "C:/Users/Kameron/Downloads/Dice/Dice4Resize.jpg";
                break;
            case 5:
                diceLoc1 = "C:/Users/Kameron/Downloads/Dice/Dice5Resize.jpg";
                break;
            case 6:
                diceLoc1 = "C:/Users/Kameron/Downloads/Dice/Dice6Resize.jpg";
                break;
        }
        switch (diceTwo) {
            case 1:
                diceLoc2 = "C:/Users/Kameron/Downloads/Dice/Dice1Resize.jpg";
                break;
            case 2:
                diceLoc2 = "C:/Users/Kameron/Downloads/Dice/Dice2Resize.jpg";
                break;
            case 3:
                diceLoc2 = "C:/Users/Kameron/Downloads/Dice/Dice3Resize.jpg";
                break;
            case 4:
                diceLoc2 = "C:/Users/Kameron/Downloads/Dice/Dice4Resize.jpg";
                break;
            case 5:
                diceLoc2 = "C:/Users/Kameron/Downloads/Dice/Dice5Resize.jpg";
                break;
            case 6:
                diceLoc2 = "C:/Users/Kameron/Downloads/Dice/Dice6Resize.jpg";
                break;
        }
        switch (diceThree) {
            case 1:
                diceLoc3 = "C:/Users/Kameron/Downloads/Dice/Dice1Resize.jpg";
                break;
            case 2:
                diceLoc3 = "C:/Users/Kameron/Downloads/Dice/Dice2Resize.jpg";
                break;
            case 3:
                diceLoc3 = "C:/Users/Kameron/Downloads/Dice/Dice3Resize.jpg";
                break;
            case 4:
                diceLoc3 = "C:/Users/Kameron/Downloads/Dice/Dice4Resize.jpg";
                break;
            case 5:
                diceLoc3 = "C:/Users/Kameron/Downloads/Dice/Dice5Resize.jpg";
                break;
            case 6:
                diceLoc3 = "C:/Users/Kameron/Downloads/Dice/Dice6Resize.jpg";
                break;
        }
        switch (diceFour) {
            case 1:
                diceLoc4 = "C:/Users/Kameron/Downloads/Dice/Dice1Resize.jpg";
                break;
            case 2:
                diceLoc4 = "C:/Users/Kameron/Downloads/Dice/Dice2Resize.jpg";
                break;
            case 3:
                diceLoc4 = "C:/Users/Kameron/Downloads/Dice/Dice3Resize.jpg";
                break;
            case 4:
                diceLoc4 = "C:/Users/Kameron/Downloads/Dice/Dice4Resize.jpg";
                break;
            case 5:
                diceLoc4 = "C:/Users/Kameron/Downloads/Dice/Dice5Resize.jpg";
                break;
            case 6:
                diceLoc4 = "C:/Users/Kameron/Downloads/Dice/Dice6Resize.jpg";
                break;
        }
        switch (diceFive) {
            case 1:
                diceLoc5 = "C:/Users/Kameron/Downloads/Dice/Dice1Resize.jpg";
                break;
            case 2:
                diceLoc5 = "C:/Users/Kameron/Downloads/Dice/Dice2Resize.jpg";
                break;
            case 3:
                diceLoc5 = "C:/Users/Kameron/Downloads/Dice/Dice3Resize.jpg";
                break;
            case 4:
                diceLoc5 = "C:/Users/Kameron/Downloads/Dice/Dice4Resize.jpg";
                break;
            case 5:
                diceLoc5 = "C:/Users/Kameron/Downloads/Dice/Dice5Resize.jpg";
                break;
            case 6:
                diceLoc5 = "C:/Users/Kameron/Downloads/Dice/Dice6Resize.jpg";
                break;
        }
    }

    public static void displayDiceValues() {
        System.out.println("Dice One = " + diceOne);
        System.out.println("Dice Two = " + diceTwo);
        System.out.println("Dice Three = " + diceThree);
        System.out.println("Dice Four = " + diceFour);
        System.out.println("Dice Five = " + diceFive);
    }

    public static void diceValuable() {

    }

    public static void scoreDicePlayerOne() {
        switch (diceOne) {
            case 1:

        }
    }    
    public static void scoreDicePlayerTwo() {
        switch (diceOne) {
            case 1: 

        }
    }
    public static void scoreDicePlayerThree() {
        switch (diceOne) {
            case 1: 

        }
    }
    public static void scoreDicePlayerFour() {
        switch (diceOne) {
            case 1: 

        }
    }

    public static void calculateScorePlayer1() {

    }
    public static void calculateScorePlayer2() {

    }
    public static void calculateScorePlayer3() {

    }
    public static void calculateScorePlayer4() {

    }

    public void launchFrame() throws IOException {
          // Display Frame
        BufferedImage slot1 = ImageIO.read(new File(diceLoc1));
        BufferedImage slot2 = ImageIO.read(new File(diceLoc2));
        BufferedImage slot3 = ImageIO.read(new File(diceLoc3));
        BufferedImage slot4 = ImageIO.read(new File(diceLoc4));
        BufferedImage slot5 = ImageIO.read(new File(diceLoc5));

        setDie1 contentPane1 = new setDie1(slot1);
        setDie2 contentPane2 = new setDie2(slot2);
        setDie3 contentPane3 = new setDie3(slot3);
        setDie4 contentPane4 = new setDie4(slot4);
        setDie5 contentPane5 = new setDie5(slot5);

        contentPane1.setOpaque(false);
        contentPane2.setOpaque(false);
        contentPane3.setOpaque(false);
        contentPane4.setOpaque(false);
        contentPane5.setOpaque(false);

          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.pack(); //Adjusts panel to components for display
          f.setVisible(true);
          f.add(jTextArea1);
          //f.add(jTextArea2);
          //f.add(jPanel1);
          //f.add(jScrollPane1);
          f.add(diceLab1);
          f.add(diceLab2);
          f.add(diceLab3);
          f.add(diceLab4);
          f.add(diceLab5);
          f.add(contentPane1);
          f.add(contentPane2);
          f.add(contentPane3);
          f.add(contentPane4);
          f.add(contentPane5);
          f.setSize(750, 500);
          jTextArea1.setSize(xSize, ySize);
          //jTextArea2.setSize(750, 50);
          //jPanel1.setSize(xSize, 120);
          jTextArea1.setLocation(0, 0);
          //jTextArea2.setLocation(0, 500);
          //jPanel1.setLocation(0, 500);
          diceLab1.setLocation(292, 515);
          diceLab2.setLocation(537, 515);
          diceLab3.setLocation(782, 515);
          diceLab4.setLocation(1027, 515);
          diceLab5.setLocation(1272, 515);
          contentPane1.setLocation(292, 515);
          contentPane2.setLocation(537, 515);
          contentPane3.setLocation(782, 515);
          contentPane4.setLocation(1027, 515);
          contentPane5.setLocation(1272, 515);
          diceLab1.setSize(36, 50);
          diceLab2.setSize(36, 50);
          diceLab3.setSize(36, 50);
          diceLab4.setSize(36, 50);
          diceLab5.setSize(36, 50);
          contentPane1.setSize(36, 50);
          contentPane2.setSize(36, 50);
          contentPane3.setSize(36, 50);
          contentPane4.setSize(36, 50);
          contentPane5.setSize(36, 50);
          //jPanel1.setBackground(Color.red);
          //jTextArea2.setBackground(Color.blue);
          Greed.this.repaint();
          System.out.println(ySize);
          System.out.println("Screen Resolution is: " + height + " x " + width);
    }

    public static void run()
    {
        redirectSystemStreams();
    }

//The following codes set where the text get redirected. In this case, jTextArea1    
 private static void updateTextArea(final String text) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        jTextArea1.append(text);
      }
    });
  }

//Followings are The Methods that do the Redirect, you can simply Ignore them. 
  private static void redirectSystemStreams() {
    OutputStream out = new OutputStream() {
      @Override
      public void write(int b) throws IOException {
        updateTextArea(String.valueOf((char) b));
      }

      @Override
      public void write(byte[] b, int off, int len) throws IOException {
        updateTextArea(new String(b, off, len));
      }

      @Override
      public void write(byte[] b) throws IOException {
        write(b, 0, b.length);
      }
    };

    System.setOut(new PrintStream(out, true));
    System.setErr(new PrintStream(out, true));
  }
}

class setDie1 extends JPanel {
    public setDie1 (BufferedImage slot1) {
        Greed.slot1 = slot1;
    }
}
class setDie2 extends JPanel {
    public setDie2 (BufferedImage slot2) {
        Greed.slot2 = slot2;
    }
}
class setDie3 extends JPanel {
    public setDie3 (BufferedImage slot3) {
        Greed.slot3 = slot3;
    }
}
class setDie4 extends JPanel {
    public setDie4 (BufferedImage slot4) {
        Greed.slot4 = slot4;
    }
}
class setDie5 extends JPanel {
    public setDie5 (BufferedImage slot5) {
        Greed.slot5 = slot5;
    }
}

这是我的代码 当我调用f.repaint();要重新绘制我的框架,它不会更新任何内容。 当我尝试调用Greed.repaint();它告诉我无法从静态上下文引用非静态变量。 我该怎么办? 我已经谷歌搜索了几个小时,我甚至尝试过referenceObject.methodName();但那种格式对我来说并不适用。 我已经知道我的代码可能有点乱,因为我还在掌握java,我知道我很擅长静态... 但任何想法?

提前谢谢, Kameron

1 个答案:

答案 0 :(得分:1)

  1. 为什么要将GUI与命令行界面混合使用?所有用户交互都应通过GUI
  2. 完成
  3. 没有任何改变,因为没有任何改变(从UI的角度来看)。您可以更改图像引用,但不要将其应用于任何可以实际绘制该更改的内容。
  4. 我首先要更加努力地看待......