Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have about 7 classes all in the same file related to each other for a project to build a game called 'Tower of Hanoi'

I have about 7 classes all in the same file related to each other for a project to build a game called 'Tower of Hanoi' but every time I run the code a white menu pops up. I think it is something to do with my 'GamePanel' class that is supposed to be a customized JPanel. Is there anyway I could get someone to look at the code and get back to me? I am literally going to fail the course if I do not have this ready. The project is due today after 5 hours.

GamePanel Class:

1 import javax.swing.*;

2 import java.awt.*;

3 import java.util.*;

4 import java.awt.event.*;

5

6

7 public class GamePanel extends JPanel

8 {

9 //Attributes

10 Stack firstTower = new Stack();

11 Stack secondTower = new Stack();

12 Stack thirdTower = new Stack();

13

14 boolean isSelectedfirstTower = true;

15 boolean isSelectedsecondTower = false;

16 boolean isSelectedthirdTower = false;

17

18 Brick carriedBrick = null;

19

20 //Default Constructor

21 public GamePanel()

22 {

23

24 addKeyListener(new ControlAdapter());//performs actions in regards to the keyboard

25

26 setFocusable(true); //lets JPanel gain power of getting focused

27 //Bricks //select particular gui element and focus on that

28 firstTower.move(new Brick(90));

29 firstTower.move(new Brick(70));

30 firstTower.move(new Brick(50));

31 firstTower.move(new Brick(30));

32

33

34

35 }

36 @Override //allows subclass to provide specific implementation of a method that is alreaady in a super class

37 //method that paints the towers

38 public void paintComponents(Graphics g)

39 {

40 //painting the components only

41 super.paintComponents(g);

42

43 //displaying the objects

44

45 //Towers

46 g.setColor(Color.lightGray);

47 //First Tower

48 g.fillRect(70, 40, 20, 160);

49 //Second Tower

50 g.fillRect(230, 40, 20, 160);

51 //Third Tower

52 g.fillRect(390, 40, 20, 160);

53

54 //Bricks (as rectangles, with their properties)

55 firstTower.drawBricks(g, 30);

56 secondTower.drawBricks(g, 190);

57 thirdTower.drawBricks(g, 350);

58

59 //carried brick

60

61 if(carriedBrick != null)

62 {

63 int xPosition = 0;

64 if(isSelectedfirstTower)

65 xPosition = 30 + (carriedBrick.getLength())/2;

66 else if(isSelectedsecondTower)

67 xPosition = 190 + (carriedBrick.getLength())/2;

68 else if(isSelectedthirdTower)

69 xPosition = 350 + (carriedBrick.getLength())/2;

70 g.fillRect(xPosition, 20,carriedBrick.getLength(),19);

71 }

72

73

74 //Selector

75

76 g.setColor(Color.red);

77 if(isSelectedfirstTower)

78 g.drawRect(1,20,158,190);

79 else if(isSelectedsecondTower)

80 g.drawRect(161,20,158,190);

81 else if(isSelectedthirdTower)

82 g.drawRect(321,20,158,190);

83

84

85 }

86 private void checkMove()

87 {

88 if(secondTower.counter()== 4 || thirdTower.counter() == 4)

89 {

90 JOptionPane.showMessageDialog(GamePanel.this, "You Win!");

91 }

92 }

93 //key adapter to create key event listener interface recieve keyboard events

94 public class ControlAdapter extends KeyAdapter {

95 @Override //to override method in subclass

96 public void keyPressed(KeyEvent e) {

97

98 int key = e.getKeyCode();

99 if (key == KeyEvent.VK_RIGHT) {

100 if(isSelectedfirstTower) {

101 isSelectedsecondTower = true;

102 isSelectedfirstTower = false;

103 }

104

105 else if(isSelectedsecondTower) {

106 isSelectedthirdTower = true;

107 isSelectedsecondTower = false;

108 }

109

110 }

111

112 if (key == KeyEvent.VK_LEFT) {

113 if(isSelectedsecondTower) {

114 isSelectedfirstTower = true;

115 isSelectedsecondTower = false;

116 }

117

118 else if(isSelectedthirdTower) {

119 isSelectedsecondTower = true;

120 isSelectedthirdTower = false;

121 }

122

123 }

124

125 if(key == KeyEvent.VK_UP) {

126 if(carriedBrick == null){

127 if(isSelectedfirstTower)

128 carriedBrick = firstTower.pop();

129 else if(isSelectedsecondTower)

130 carriedBrick = secondTower.pop();

131 else if(isSelectedthirdTower)

132 carriedBrick = thirdTower.pop();

133 }

134

135 }

136

137 if(key == KeyEvent.VK_DOWN) {//for moving the bricks

138 if(carriedBrick != null) {

139 if(isSelectedfirstTower){

140 firstTower.move(carriedBrick);

141 }

142 else if(isSelectedsecondTower){

143 secondTower.move(carriedBrick);

144 }

145 else if(isSelectedthirdTower){

146 thirdTower.move(carriedBrick);

147 }

148 carriedBrick = null;

149

150 }

151

152

153 }

154 repaint(); //executes update method

155 checkMove();

156 }

157

158

159 }

160 }

161

GameFrame Class:

1 //import java.awt.BorderLayout;

2 import java.awt.*;

3 import javax.swing.*;

4

5 public class GameFrame extends JFrame

6 {

7 //Attributes

8 static JPanel JP;

9 static JFrame frame;

10

11 static GameFrame GF;

12 static GamePanel GP;

13

14 //main method

15 public static void main(String [] args)

16 {

17 //method that proceeds after the GUI events have been processed

18 EventQueue.invokeLater(new Runnable()

19 {

20 public void run()

21 {

22 try//test a block of code for errors

23 {

24 JP = new JPanel();

25 frame = new JFrame();

26

27 GF = new GameFrame();

28 GP = new GamePanel();

29

30 frame = GF;

31 JP = GP;

32

33 frame.setVisible(true);

34

35 }

36 catch(Exception e) //lets you handle the error

37 {

38 e.printStackTrace();

39 }

40 }

41 });

42 }

43 //Constructor

44 public GameFrame()

45 {

46 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

47 setBounds(100, 100, 500, 300);

48 setContentPane(new GamePanel());

49 frame.add(JP, BorderLayout.CENTER); //adds the panel

50 }

51 }

52

THESE ARE THE OTHER TWO CLASSES I AM WORKING WITH

Brick class:

1 public class Brick

2 {

3 //Attributes

4 private int length;

5 private Brick next;

6

7 //Parametrized constructor

8 public Brick(int length)

9 {

10 this.length = length;

11 next = null;

12 }

13 //Accessor that returns length

14 public int getLength()

15 {

16 return length;

17 }

18 //Mutator that modifies length

19 public void setLength(int length)

20 {

21 this.length = length;

22 }

23 //Accessor that returns next

24 public Brick getNext()

25 {

26 return next;

27 }

28 //Mutator that modifies next

29 public void setNext(Brick next)

30 {

31 this.next = next;

32 }

33 }

Stack Class

1 import javax.swing.*;

2 import java.awt.*;

3

4 public class Stack

5 {

6 //Attribute

7 private Brick mainBrick;

8

9 //Accessor that returns mainBrick

10 public Brick getMainBrick()

11 {

12 return mainBrick;

13 }

14 //Mutator that modifies mainBrick

15 public void setMainBrick(Brick _mainBrick)

16 {

17 mainBrick = _mainBrick;

18 }

19

20 //Add Brick

21 public void move(Brick brick)

22 {

23 if(mainBrick == null)

24 {

25 mainBrick = brick;

26 }

27 else

28 {

29 Brick currentBrick = mainBrick;

30 while(currentBrick.getNext() != null)

31 {

32 currentBrick = currentBrick.getNext();

33 }

34 currentBrick.setNext(brick);

35 }

36

37 }

38 //Remove Brick

39 public Brick pop()

40 {

41 Brick popBrick = null;

42 if(counter()==1)

43 {

44 popBrick = mainBrick;

45 mainBrick = null;

46 }

47 else if(counter() > 1)

48 {

49 Brick currentBrick = mainBrick;

50 for(int i = 1; i < counter()-1; i++)

51 {

52 currentBrick = currentBrick.getNext();

53 }

54 popBrick = currentBrick.getNext();

55 currentBrick.setNext(null);

56 }

57

58 return popBrick;

59 }

60

61 //Get Brick

62 public Brick peek()//retrive or fetch the first element of the stack

63 {

64 Brick peekBrick = null;

65 if(counter() > 0)

66 {

67 Brick currentBrick = mainBrick;

68 while(currentBrick.getNext() != null)

69 {

70 currentBrick = currentBrick.getNext();

71 }

72 return currentBrick;

73 }

74 else

75 {

76 return null;

77 }

78 }

79 //count of bricks in a stack

80 public int counter()

81 {

82 int counter = 0;

83 Brick currentBrick = mainBrick;

84 while(currentBrick != null)

85 {

86 counter++;

87 currentBrick = currentBrick.getNext();

88 }

89 return counter;

90 }

91 public void drawBricks(Graphics g, int x)

92 {

93 Brick currentBrick = mainBrick;

94 for(int i = 0; i < counter(); i++)

95 {

96 int xPosition = x + (100-currentBrick.getLength())/2;

97 int yPosition = 100 - (i*20);

98

99 g.setColor(Color.blue);

100 g.fillRect(xPosition,yPosition, currentBrick.getLength(), 19);

101 currentBrick = currentBrick.getNext();

102 }

103 }

104 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

What does discretization mean in the finite element method?

Answered: 1 week ago