0

I'm trying to use enum to store a bunch of strings, but when I go to convert them into strings it doesn't work. I get the error "cannot convert from String to ChessSquare.SelectedPiece. I think it'll only be a little change, but I can't find what to change.

Here is my code:

package Logic;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;


    //chess square class, 1 instance of which for each square in the grid
    @SuppressWarnings("serial")
    public class ChessSquare extends JButton {

        //instance variables for position and pieces
        public int posX;
        public int posY;
        public String currentPiece;
        public enum selectedPiece{
            NONE, PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING
        }
        selectedPiece piece;

        //load images and cast into icons
        BufferedImage buttonIcon = ImageIO.read(new File(piece));
            ImageIcon Icon = new ImageIcon(buttonIcon);
                BufferedImage 

        //constructor for chess squares
        public ChessSquare(int x, int y, double p) throws IOException {
            this.setIcon(Icon);
                setVisible(true);
            }

        //accessor method for position
        public void squarePos(int x, int y){
            this.posX = x;
            this.posY = y;
        }

        //accessor method for currentPiece
        public void cPiece(){
            this.currentPiece = piece;
        }

        //specify what each value of enum slectedPiece represents
        public void selectedPiece(){
            switch (piece){
                case NONE:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg";
                case PAWN:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\Pawn.jpg";
                case ROOK:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\Rook.jpg";
                case KNIGHT:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\Knight.jpg";
                case BISHOP:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\Bishop.jpg";
                case QUEEN:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\Queen.jpg";
                case KING:
                        piece = "E:\\Eclipse\\ChessF\\src\\Images\\King.jpg";
            }
        }
    }
5
  • enum != String. You can't just interchange types like that. Commented Mar 22, 2013 at 15:09
  • I can't compile your code: ImageIcon Icon = new ImageIcon(buttonIcon); BufferedImage Commented Mar 22, 2013 at 15:11
  • Not answering your question but you really really should put break; and default: in your switch case. That would save you from hell. Commented Mar 22, 2013 at 15:16
  • The problem is in your cPiece method. What exactly is your cPiece method supposed to do? Commented Mar 22, 2013 at 15:18
  • FYI you can use / for the directory separator. The JVM will convert it to \ on Windows. Commented Mar 22, 2013 at 19:57

6 Answers 6

2

In Java, enum is a full fledged class... As such, you should put your enum operations (convert to/from string) inside it. For example:

    public enum SelectedPiece{
        NONE("E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg"), 
        PAWN("E:\\Eclipse\\ChessF\\src\\Images\\Pawn.jpg"), 
        ROOK("E:\\Eclipse\\ChessF\\src\\Images\\Rook.jpg"), 
        KNIGHT("E:\\Eclipse\\ChessF\\src\\Images\\Knight.jpg"), 
        BISHOP("E:\\Eclipse\\ChessF\\src\\Images\\Bishop.jpg"), 
        QUEEN("E:\\Eclipse\\ChessF\\src\\Images\\Queen.jpg"), 
        KING("E:\\Eclipse\\ChessF\\src\\Images\\King.jpg");

        private String imageFilename;
        private ImageIcon image;

        private SelectedPiece( String imageFilename ) throws IOException {
            this.imageFilename = imageFilename;
            this.image = new ImageIcon(ImageIO.read(new File(piece)));
        }

        public String getImageFilename() {
            return imageFilename;
        }

        public ImageIcon getImage() {
            return image;
        }
    }

And so forth... Then just use the enum values where needed.

Sign up to request clarification or add additional context in comments.

2 Comments

imageFilename and image should be final
@SteveKuo, not saying that they shouldn't be, but how would that help? (curious, not challenging...)
1

Enum is not a string. To convert between them, you need to use valueOf

E.g.,

selectedPiece.valueOf("ROOK");

Will return the enum selectedPiece.ROOK

When writing the enum, you can use a custom value, e.g.,

Public enum Piece {
  ROOK("Rook"),
  QUEEN("Queen")
}

Comments

0

When you do this.currentPiece = piece; you're attempting to assign an enum type in a String object. It's impossible.

Comments

0

piece is of type selectedPiece (your enum)

You are not allowed to assign a String to it. Store your paths in another variable.

eg.:

String piecePath;
switch (piece){
                    case NONE:
                            piecePath = "E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg";
 ...

Comments

0

You should use the enum as a variable in the class to determine its type. You could simply add:

    public selectedPiece pieceType;

Then set the type somewhere in the object:

    pieceType = selectedPiece.ROOK;

Then you can do the switch statement using the enum to assign the string to the string variable.

The way you organize this is up to you.

Comments

0

Maybe this is a case for using CONSTANTS?, after all if the graphic image for a pawn is always going to be the same image, doesn't that make it constant?

static final string IMAGE_NONE = "E:\\Eclipse\\ChessF\\src\\Images\\EmptySquare.jpg";
static final string IMAGE_PAWN = "E:\\Eclipse\\ChessF\\src\\Images\\Pawn.jpg";

and in your case statements

case PAWN:
  piece = IMAGE_PAWN 

etc etc

2 Comments

Java doesnt have var
I changed it static final string, Thank You for point out my mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.