Hi, I having been struggling to find the ClassCastException Error in the following code which is designed to display a digital number depending on the number entered into the option pane:
Code:
import java.applet.Applet;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
public class AssignmentTask2 {
private SDisplay display;
public void init() {
SDisplay display = new SDisplay(100, 100, 10, 50);
display.startDisplay();
}
public void paint (Graphics g) {
display.drawNumber(g);
}
}
class SDisplay extends Applet{
int num;
int segmentW;
int segmentL;
int Coord [][];
String Number; // The number entered by the user
int Segments; // The number after conversion from string to integer
boolean SegmentStatus;
public SDisplay(int width, int length, int x, int y){
this.segmentW = width;
this.segmentL = length;
this.Coord = new int [2] [7];
this.Coord[0][0] = segmentW + x;
this.Coord[1][0] = 0 + y;
this.Coord[0][1] = segmentW + segmentL + x;
this.Coord[1][1] = segmentW + y;
this.Coord[0][2] = Coord[0][1];
this.Coord[1][2] = Coord[1][1] + segmentW + segmentL;
this.Coord[0][3] = segmentW + x;
this.Coord[1][3] = (segmentW + segmentL)*2 + y;
this.Coord[0][4] = 0 + x;
this.Coord[1][4] = Coord[1][2];
this.Coord[0][5] = 0 + x;
this.Coord[1][5] = segmentW + y;
this.Coord[0][6] = Coord[0][5] + segmentW;
this.Coord[1][6] = Coord[1][5] + segmentL;
//this.num = numbers;
}
public void startDisplay(){
// Read the number from the user
Number = JOptionPane.showInputDialog (null, "Enter a number between 0 - 9");
// Convert the string to an integer value
Segments = Integer.parseInt(Number);
}
public boolean Segment ( int choice, int segmentId ) {
boolean segmentStatus = false;
switch (segmentId) {
case 0 : if ((choice == 0) | (choice == 2) | (choice == 3) | (choice == 5) | (choice == 6)| (choice == 7) | (choice == 8) |
(choice == 9)) SegmentStatus = true;
else SegmentStatus = false;
break;
case 1 : if ((choice == 0) | (choice == 1) | (choice == 2) | (choice == 3) | (choice == 4)| (choice == 7) | (choice == 8) |
(choice == 9)) SegmentStatus = true;
else SegmentStatus = false;
break;
case 2 : if ((choice == 0) | (choice == 1) | (choice == 3) | (choice == 4) | (choice == 5)| (choice == 6) | (choice == 7) |
(choice == 8) | (choice == 9)) SegmentStatus = true;
else SegmentStatus = false;
break;
case 3 : if ((choice == 0) | (choice == 2) | (choice == 3) | (choice == 5) | (choice == 6)| (choice == 8) | (choice == 9)) SegmentStatus = true;
else SegmentStatus = false;
break;
case 4 : if ((choice == 0) | (choice == 2) | (choice == 6) | (choice == 8))
segmentStatus = true;
else segmentStatus = false;
break;
case 5 : if ((choice == 0) | (choice == 4) | (choice == 5) | (choice == 6) | (choice == 8)| (choice == 9))
segmentStatus = true;
else segmentStatus = false;
break;
case 6 : if ((choice == 2) | (choice == 3) | (choice == 4) | (choice == 5) | (choice == 6)| (choice == 8) | (choice == 9))
segmentStatus = true;
else segmentStatus = false;
break;
}
return segmentStatus;
}
private void FilledRectangle ( int choice, boolean Seg, Graphics g){
g.setColor(Color.red);
if (Seg) {
g.fillRect(this.Coord[0][choice], this.Coord[0][choice], this.segmentW, this.segmentL);}
else{
g.drawRect(this.Coord[0][choice], this.Coord[0][choice], this.segmentW, this.segmentL);
}
}
private void drawEmpty(int a, boolean v, Graphics g){
g.setColor(Color.white);
if (v)
g.fillRect(this.Coord[0][a], this.Coord[1][a], this.segmentW, this.segmentL);
else
g.fillRect(this.Coord[0][a], this.Coord[1][a], this.segmentL, this.segmentW);
}
private void drawSeg (int segments, boolean status, Graphics g){
if ((status) & ((segments == 1) | (segments == 2) | (segments == 4) | (segments == 5))){
boolean t = true;
FilledRectangle(segments, t, g);
}
else if ((status) & ((segments == 0) | (segments == 3) | (segments == 6))){
boolean t = false;
FilledRectangle(segments, t, g);
}
else if ((!status) & ((segments == 1) | (segments == 2) | (segments == 4) | (segments == 5))){
boolean t = true;
drawEmpty(segments, t, g);
}
else if ((!status) & ((segments == 0) | (segments == 3) | (segments == 6))){
boolean t = false;
drawEmpty(segments, t, g);
}
}
public void drawNumber(Graphics g) {
for (int Segments=0; Segments<=6;Segments++){
boolean state = Segment(this.num, Segments);
drawSeg ( Segments, state, g);
}
}
}