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";
            }
        }
    }
JmJ
  • 1,989
  • 3
  • 29
  • 51

6 Answers6

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.

Lucas
  • 14,227
  • 9
  • 74
  • 124
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")
}
TonyArra
  • 10,607
  • 1
  • 30
  • 46
0

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

Guillaume M
  • 470
  • 1
  • 5
  • 12
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";
 ...
Tommy
  • 739
  • 8
  • 24
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.

AnxGotta
  • 1,006
  • 7
  • 28
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

Zeddy
  • 2,079
  • 1
  • 15
  • 23