有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么从对象获取信息时会出现堆栈溢出错误?

您好,我正在尝试用java创建一个宫殿游戏。我参加了solo learn课程,但从未真正学会如何处理堆栈溢出错误。我的问题都没有在网上找到。目前处于可变状态 AllCards aceClover = new AllCards(); 上面说有个StackOverflowerr错误,我不知道为什么

package MainClasses;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.util.Random;
public class AllCards {
    private Image cardImg = null;// to set image
    private String cardImgURL = ""; // get image location
    private int cardOrder = 0; // Card order in a hierarchy
    public boolean ruleAffect = false;  // if rules is changed can affect
    private boolean cardPower = false; // is the card a special card?
    private int cardPowerNumber = 0; // which are the powers 1.Go again 2.Clear 3.mirror 4.Put card lower 5.Pick up deck unless have a copy
    AllCards aceClover = new AllCards(); // declared the Clover Ace
    AllCards twoClover = new AllCards();
    AllCards threeClover = new AllCards();
    AllCards fourClover = new AllCards();
    AllCards fiveClover = new AllCards();
    AllCards sixClover = new AllCards();
    AllCards sevenClover = new AllCards();
    AllCards eightClover = new AllCards();
    AllCards nineClover = new AllCards();
    AllCards tenClover = new AllCards();
    AllCards jackClover = new AllCards();
    AllCards queenClover = new AllCards();
    AllCards kingClover = new AllCards();
    public Image imageExtract;
    public String cardImgUrlExtractor;
    public int cardOrderExtract;
    public boolean ruleAffectExtract;
    public boolean cardPowerExtractor;
    public int cardPowerNumberExtractor;

    public AllCards() {


        aceClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile000.png"; // clover ace url
        if(aceClover.ruleAffect == false) {
            aceClover.cardOrder = 1;
        }else {
            aceClover.cardOrder = 14;
        }

        try {

            aceClover.cardImg = ImageIO.read(new File(aceClover.cardImgURL));

        }catch(Exception e) {

            System.out.println("Could't loud the Image! Error : "+e.getMessage());

        }

        twoClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile001.png";
        twoClover.cardOrder = 14;
        twoClover.cardPower = true;
        twoClover.cardPowerNumber = 2;
        try {
            twoClover.cardImg = ImageIO.read(new File(twoClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        threeClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile002.png";
        threeClover.cardOrder = 15;
        threeClover.cardPower = true;
        if(threeClover.ruleAffect == false) {
            threeClover.cardPowerNumber = 14;
        }else {
            threeClover.cardPowerNumber = 3;
        }

        try {
            threeClover.cardImg = ImageIO.read(new File(threeClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        fourClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile003.png";
        fourClover.cardOrder = 4;
        try {
            fourClover.cardImg = ImageIO.read(new File(fourClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        fiveClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile004.png";
        fiveClover.cardOrder = 5;
        try {
            fiveClover.cardImg = ImageIO.read(new File(fiveClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        sixClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile005.png";
        sixClover.cardOrder = 6;
        try {
            sixClover.cardImg = ImageIO.read(new File(sixClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        sevenClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile006.png";
        sevenClover.cardOrder = 14;
        sevenClover.cardPower = true;
        sevenClover.cardPowerNumber = 4;
        try {
            sevenClover.cardImg = ImageIO.read(new File(sevenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        eightClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile007.png";
        eightClover.cardOrder = 8;
        try {
            eightClover.cardImg = ImageIO.read(new File(eightClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        nineClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile008.png";
        nineClover.cardOrder = 9;
        try {
            nineClover.cardImg = ImageIO.read(new File(nineClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        tenClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile009.png";
        tenClover.cardOrder = 15;
        tenClover.cardPower = true;
        tenClover.cardPowerNumber = 14;
        tenClover.ruleAffect = true;
        try {
            tenClover.cardImg = ImageIO.read(new File(tenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        jackClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile010.png";
        jackClover.cardOrder = 11;
        try {
            jackClover.cardImg = ImageIO.read(new File(jackClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        queenClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile011.png";
        queenClover.cardOrder = 12;
        try {
            queenClover.cardImg = ImageIO.read(new File(queenClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }

        kingClover.cardImgURL = "C:\\Users\\Jhosua\\eclipse-workspace\\Palace Game\\bin\\All Cards\\tile012.png";
        kingClover.cardOrder = 13;
        try {
            kingClover.cardImg = ImageIO.read(new File(kingClover.cardImgURL));
        }catch(Exception e) {
            System.out.println("Could't loud the Image! Error : "+e.getMessage());
        }
    }
    public void setVariables(AllCards x) {
        imageExtract = x.cardImg;
        cardImgUrlExtractor = x.cardImgURL;
        cardOrderExtract = x.cardOrder;
        ruleAffectExtract = x.ruleAffect;
        cardPowerNumberExtractor = x.cardPowerNumber;
        cardPowerExtractor = x.cardPower;
    }
    public void randomizer() {
        Random rand = new Random();
        int rand_int1 = rand.nextInt(15-1)+1;
        System.out.print(rand_int1);
        if (rand_int1 == 1) {
            setVariables(aceClover);

        }else if (rand_int1 == 2) {
            setVariables(twoClover);
        }else if (rand_int1 == 3) {
            setVariables(threeClover);
        }else if (rand_int1 == 4) {
            setVariables(fourClover);
        }else if (rand_int1 == 5) {
            setVariables(fiveClover);   
        }else if (rand_int1 == 6) {
            setVariables(sixClover);
        }else if (rand_int1 == 7) {
            setVariables(sevenClover);
        }else if (rand_int1 == 8) {
            setVariables(eightClover);
        }else if (rand_int1 == 9) {
            setVariables(nineClover);
        }else if (rand_int1 == 10) {
            setVariables(tenClover);
        }else if (rand_int1 == 11) {
            setVariables(jackClover);
        }else if (rand_int1 == 12) {
            setVariables(queenClover);
        }else if (rand_int1 == 13) {
            setVariables(kingClover);
        }else {
            System.out.println("Couldn't return any Cards!");
        }
    }
}

在这个类中,我要做的是创建一个带有特定变量的对象。然后所有这些变量都被赋值,但一旦赋值,我需要一个函数,该函数获取一个随机整数,以选择某个对象,将信息发送给另一个类,然后该类将其显示在屏幕上

package MainClasses;
import java.awt.*;
import java.io.*;
import java.util.Random;

import javax.imageio.ImageIO;

public class DrawBoard extends Component{
    AllCards accessor = new AllCards();
    public void paint(Graphics g) { // function to draw onto the window
    Graphics2D g1 = (Graphics2D)g; // the component being used to access and write to the window
    g1.clearRect(0, 0, getWidth(), getHeight()); // clears rectangle every frame
    g1.setBackground(Color.green); // sets background color
    accessor.randomizer();
    g1.drawImage(accessor.imageExtract, 100, 100, null);
    }   
}

这是我用来从对象中提取信息的函数。我不是100%确定它能工作,但从以前的版本来看,它显示得很好

package MainClasses;
import java.awt.Color;

import javax.swing.*;
public class Main extends JFrame{

    public static void main(String[] args) {
        JFrame f = new JFrame("Palace");
        f.setSize(1920,1080);
        f.setResizable(false);
        f.setVisible(true);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.add(new DrawBoard());

    }

}

这是我用来创建JFrame的类,以备需要


共 (0) 个答案