Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Button To Display An Image

Recommended Posts

I have an assignment that is due tomorrow that I am struggling with. I want to be able to click the next button in this applet, and then repaint all the graphics. Board contains an array with all of the chess pieces on a board, and when the button is clicked I want to be able to get the next move, but for now I just wanted to call a function to move one piece just so I know it's working. Here is my applet code, I could post all the other files, but I am almost positive they all work correctly, and I am just having trouble with the applet.

Code:

import java.awt.*; public class Test extends java.applet.Applet{    Button nextButton;    Image board, bp, wp, bn, wn, br, wr, bb, wb, bk, wk, bq, wq;    Board currentBoard = new Board();    public void init() {     	board = getImage(getCodeBase(), "../images/board.jpg");  bp = getImage(getCodeBase(), "../images/bp.gif");  wp = getImage(getCodeBase(), "../images/wp.gif");  bn = getImage(getCodeBase(), "../images/bn.gif");  wn = getImage(getCodeBase(), "../images/wn.gif");  br = getImage(getCodeBase(), "../images/br.gif");  wr = getImage(getCodeBase(), "../images/wr.gif");	  bb = getImage(getCodeBase(), "../images/bb.gif");  wb = getImage(getCodeBase(), "../images/wb.gif");  bk = getImage(getCodeBase(), "../images/bk.gif");  wk = getImage(getCodeBase(), "../images/wk.gif");  bq = getImage(getCodeBase(), "../images/bq.gif");  wq = getImage(getCodeBase(), "../images/wq.gif");    	nextButton = new Button("Next");        add(nextButton);    }    public boolean action(Event evt, Object arg) {        if (evt.target instanceof Button) {            getMove((Button) evt.target);            return true;        } else {            return false;        }    }    void getMove(Button b) {        changeBoard();        repaint();    }        public void paint(Graphics screen) {  //	Draw Board  screen.drawImage(board, 0, 50, 900, 930, this);  String hi = "hi";  for(int y = 1; y < 9; y++)  {  	  	char tempX = 'a';  	for(int x = 1; x < 9; x++)  	{    if(currentBoard.colorOn(tempX, y) != 'n')    {    	if(currentBoard.colorOn(tempX, y) == 'b')    	{      if (currentBoard.typeOn(tempX, y) == "Pawn")      {      	screen.drawImage(bp, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Rook")      {      	screen.drawImage(br, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Knight")      {      	screen.drawImage(bn, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Bishop")      {      	screen.drawImage(bb, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "King")      {      	screen.drawImage(bk, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Queen")      {      	screen.drawImage(bq, convertX(tempX), convertY(y), 100, 100, this);      }    	}    	if(currentBoard.colorOn(tempX, y) == 'w')    	{      if (currentBoard.typeOn(tempX, y) == "Pawn")      {      	screen.drawImage(wp, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Rook")      {      	screen.drawImage(wr, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Knight")      {      	screen.drawImage(wn, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Bishop")      {      	screen.drawImage(wb, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "King")      {      	screen.drawImage(wk, convertX(tempX), convertY(y), 100, 100, this);      }      if (currentBoard.typeOn(tempX, y) == "Queen")      {      	screen.drawImage(wq, convertX(tempX), convertY(y), 100, 100, this);      }          	}    }    tempX++;  	}  }	}	public int convertX(char a) {  return ((a-97)*(100)) + 90;	}		public int convertY(int a) {  return ((a-1)*(100)) + 90;	}	public void changeBoard(){  currentBoard.move('b',2,'b',3);  currentBoard.move('b',7,'b',5);	}}
Thanks,kvarnerexpress

Share this post


Link to post
Share on other sites

I suppose your assignment is passed by now, and I hope you found an answer to your question. I would be interested in the chess game if you could send me the projects files. I'm just starting to learn java myself,and this probally has not a thing to do with your question, but I was noticing that the applet source code only contains one import statment that doesnt seem right.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.