paintComponent not painting to screen
Hi i'm trying to up load maps, they read in from the file selected and then they get put into a 2d array but for some reason the paintcomponent method isn't displaying anything to the screen.
PLEASE HELP!!
JMenuExample class
package sokoban;
/*
*JMenuExample
*@author Michael Heron
*
*A simple applet showing how to implement menus in a java application.
*
**/
// Standard imports.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
// Standard class definition for a Java applet.
class JToolBarExampleextends JFrameimplements ActionListener{
JMenuBar myMenuBar;
JMenu fileMenu, helpMenu;
JMenuItem fileLevel, fileRestart, fileHighScore, fileExit, helpAbout, helpContents;
String menuItem;
File selectedFile;
FleschComputer myComputer;
public JToolBarExample(){
// Okay, get our menu bar.
myMenuBar =new JMenuBar ();
// Now get a JMenu for each category... we're just going to have
// three.
fileMenu =new JMenu ("File");
helpMenu =new JMenu ("Help");
// Now we need a JMenuItem for each option on each menu. These
// generate an Action event when clicked, so we need to register
// an ActionListener for each.
fileLevel =new JMenuItem ("Select Level");
fileLevel.addActionListener (this);
fileRestart =new JMenuItem ("Restart");
fileRestart.addActionListener (this);
fileHighScore =new JMenuItem ("High Score");
fileHighScore.addActionListener (this);
fileExit =new JMenuItem ("Exit");
fileExit.addActionListener (this);
helpAbout =new JMenuItem ("About");
helpAbout.addActionListener (this);
helpContents =new JMenuItem ("Contents");
helpContents.addActionListener (this);
// Okay, now we add each of the JMenuItem objects to the appropriate
// JMenu. For example, open and save belong under the File menu, so
// we add them to that.
fileMenu.add (fileLevel);
fileMenu.add (fileRestart);
fileMenu.add (fileHighScore);
fileMenu.add (fileExit);
// For neatness, we're going to add a seperator on the menu.
helpMenu.add (helpAbout);
helpMenu.add (helpContents);
// Okay, now we have each menu, we bolt them onto our menubar.
myMenuBar.add (fileMenu);
myMenuBar.add (helpMenu);
// Phew. Now we set the menu bar of the application as the MenuBar
// we just set up.
setJMenuBar (myMenuBar);
}
publicstaticvoid main(String args[]){
System.out.println("Starting JPanelApplication...");
JToolBarExample mainFrame =new JToolBarExample();
mainFrame.setSize(400, 400);
mainFrame.setTitle("JPanelApplication");
mainFrame.setVisible(true);
}
// JMenuItem objects generate an actionPerformed event when clicked,
// so we simply add the handler code for each in this method. Essentially
// they act like buttons that are hidden away somewhere neat.
publicvoid actionPerformed (ActionEvent e){
if (e.getSource() == fileLevel){
JFileChooser myChooser =new JFileChooser();
if (myChooser.showOpenDialog (this) == JFileChooser.APPROVE_OPTION){
selectedFile = myChooser.getSelectedFile();
myComputer =new FleschComputer (selectedFile);
add(myComputer);
//myText.setText (myComputer.getText());
/*int returnVal = chooser.showOpenDialog(this);
if(returnVal == JFileChooser.APPROVE_OPTION)
*/
}
//JOptionPane.showInputDialog (null, "Level Selection", "Please enter in level", JOptionPane.INFORMATION_MESSAGE);
}
elseif (e.getSource() == fileRestart){
menuItem ="Restart level";
JOptionPane.showMessageDialog (null,"The menu item selected was "
+ menuItem);
}
elseif (e.getSource() == fileHighScore){
menuItem ="View highscores";
JOptionPane.showMessageDialog (null,"The menu item selected was "
+ menuItem);
}
elseif (e.getSource() == fileExit){
System.exit(0);
}
elseif (e.getSource() == helpAbout){
JOptionPane.showMessageDialog (null,"Created by Toni Scullion 2007","Help", JOptionPane.INFORMATION_MESSAGE);
}
elseif(e.getSource() == helpContents){
menuItem ="Contents";
JOptionPane.showMessageDialog (null,"The menu item selected was "
+ menuItem);
}
// Finally, we prompt up a message showing which menu item was just
// clicked.
//JOptionPane.showMessageDialog (null, "The menu item selected was "
//+ menuItem);
}
}
FleschComputer class:
package sokoban;
/**
* @(#)FleschComputer.java
*
*
* @author
* @version 1.00 2007/4/18
*/
import java.awt.Color;
import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
publicclass FleschComputerextends JPanel{
//arraylist to store the lines of the file we are reading in
//using an arraylist as often we dont know in advance how many lines
//there will be
//if your map will always be the same size you can use an array
ArrayList <String> lines =new ArrayList <String> ();
int lgth;
int bdth;
String userResponse;
int level;
//dynamic 2d array that will change in size depending on size of map file
char[][] myMap;
String temp;
public FleschComputer (File myFile){
openFile (myFile);
parseMap();
}
privatevoid openFile(File workingFile){
String line ="";
try{
FileReader myReader =new FileReader (workingFile);
BufferedReader in =new BufferedReader (myReader);
line = in.readLine();
while (line !=null){
lines.add(temp);
line = in.readLine();
}
in.close();
}
catch (IOException ex){
System.out.println ("A horrible exception has occured!");
}
}
/*
try {
FileReader myReader = new FileReader (workingFile);
BufferedReader in = new BufferedReader (myReader);
line = in.readLine();
while (line != null) {
lines.add(temp);
line = in.readLine();
}
in.close();
System.out.print("file found");
}
catch (IOException ex) {
System.out.println ("A horrible exception has occured!");
}
}
*/
publicvoid parseMap (){
String aLine;
aLine ="";
char tmp;
tmp ='a';
System.out.println("parse");
lgth = lines.size();
bdth = lines.get(0).length();
System.out.print("" + lgth + bdth);
myMap =newchar[lgth][bdth];
//loop over each of the lines in the arraylist and fill the 2d array
for (int i = 0; i < lines.size(); i++){
aLine = lines.get(i);
//loop over a line and take the chars off one at a time
//then store in the correct location in the 2d array of chars
for (int j = 0; j < aLine.length(); j++){
tmp = aLine.charAt(j);
myMap [i][j] = tmp;
}
}
for (int i = 0; i < lgth; i++){
for (int j = 0; j < bdth; j++){
System.out.print (myMap[i][j]);
}
System.out.println();
}
System.out.println("parseMap");
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
//gx and gy are graphical constants. Sets the x and y coords for where to draw
int gx, gy = 40;
for (int i = 0; i < lgth; i++){
gx = 20;
for (int j = 0; j < bdth; j++){
g.fillRect (gx, gy, lgth, bdth);
switch (myMap[i][j]){
case'#' : g.setColor (Color.RED);break;
case'.' : g.setColor (Color.GREEN);break;
case'@' : g.setColor (Color.BLUE);break;
case'$' : g.setColor (Color.ORANGE);break;
case'^' : g.setColor (Color.PINK);break;
case'M' : g.setColor (Color.YELLOW);break;
default : g.setColor (Color.BLACK);break;
}
g.fillRect (gx, gy, 20, 20);
gx += 20;
}
gy += 20;
}
}
}
one of the maps that can get loaded but does not get displayed:
"M^^^^^^^^^^^########" +
"M^^^^^^^^^^^# ....#" +
"M############ ....#" +
"M## $ $....#" +
"M# $$$#$ $ # ....#" +
"M# $$ # ....#" +
"M# $$ #$ $ $########" +
"M# $ ##" +
"M## #########" +
"M####" +
"M#$##" +
"M# $$#$$ @#" +
"M####" +
"M###########"
i don't get any errors or anything it just simply doesn't paint to the screen and i'm not sure why?
PLEASE HELP!!
Cheers Paul.g

