   [java] JPanel Array Won't Refresh
  Hey all,I am trying to write a really basic 2D editor for my 2D game.  I wanted to use Java and Swing because that is what we work with in class.  I figured doing a projet on my own would help me get down some things we don't necesarrily cover in class.I've hit a bit of a wall though.  Well, two problems.  First, for some reason my custom paintComponent() method in MapTile causes my program to use 98-100% of my CPU time.  If I comment out the super.repaint() line, it runs at normal usage, but does not function correctly.Second, when I click my load button, the load method loads all the map data correctly, if I have it dump the info to the console output stream, it looks perfect.  It even reports each MapTile's BackgroundColor as being correct.  However, in the GUI itself, the tiles will not display the correct color.I have been trying to figure this out for 2 days now.  I've tried Google'ing and searching these forums and I am completely stumped.  I will post some code and try to keep it as short as possible.  If you need ot see more or just want me to host the whole source, let me know.Any suggestions are appreciated.Thanks,ChrisCode:Map.Java:This file holds the array of data that represnts the map.This is passed to MapPanel and then displayed to the user.public class Map{private MapTile[][] tile;int w;int h;public void setMap(MapTile[][] t){tile = t;for(int y = 0; y < h; y++)for(int x = 0; x < w; x++){tile[x][y].revalidate();tile[x][y].repaint();}}MapPanel.javaThis is the constructor.  The MapPanel is the one that holds the array of MapTiles that are displayed to the userpublic MapPanel(Map m, int x1, int y1, Brush b){setLayout(new GridLayout(m.height(), m.width(), 1, 1));map = m;x = x1;y = y1;for(int iy = 0; iy < m.height(); iy++){for(int ix = 0; ix < m.width(); ix++){add(map.getTile(ix, iy));}}}MapTile.javaThis file is the actual tile that holds information about that specific square of the map.public class MapTile extends Tile{private int x, y;private int graphic;private Color[] bgColor = {Color.white, Color.pink, Color.blue, Color.green, Color.yellow};public MapTile(int x1, int y1, boolean p, boolean e, int g, Brush b){setPreferredSize(new Dimension(32, 32));x = x1;y = y1;passable = p;enemy = e;graphic = g;brush = b;setBackground(bgColor[graphic]);addMouseListener(new ClickListener());repaint();}// This method seems to be slowing the program down a great dealpublic void paintComponent(Graphics g){super.repaint();g.setColor(bgColor[graphic]);g.fillRect(0,0,getWidth(),getHeight());}OptionPanel.javaThis panel holds the GUI and functional methods of the "Option Bar".This consists of the New, Save, Load, Quit, and Dump(display map in text) JButtons.// Load methodprivate MapTile[][] load(String filename) throws IOException{MapTile[][] retmap;int w, h;File file = new File(filename);Scanner infile = new Scanner(file);w = infile.nextInt();h = infile.nextInt();retmap = new MapTile[w][h];for(int y = 0; y < h; y++){for(int x = 0; x < w; x++){int tx, ty, ipass, ienemy, graphic;boolean pass, enemy;tx = infile.nextInt();ty = infile.nextInt();ipass = infile.nextInt();ienemy = infile.nextInt();graphic = infile.nextInt();if(ipass == 0)pass = false;elsepass = true;if(ienemy == 0)enemy = false;elseenemy = true;retmap[x][y] = new MapTile(x, y, pass, enemy, graphic, map.getBrush());System.out.println("w= "+w+" || h= "+h+  "tx= "+tx+" || ty= "+ty+ "pass= "+pass+" || enemy= "+enemy+ "graphic= "+graphic);}}infile.close();return retmap;}// Implementing the load function by attaching it to the load buttonprivate class ButtonListener implements ActionListener{public void actionPerformed(ActionEvent e){ if(e.getSource() == loadButton){try{map.setMap(load("map.txt"));System.out.println("Load Complete.");}catch(IOException ioe){}}
// This method seems to be slowing the program down a great dealpublic void paintComponent(Graphics g){super.repaint();g.setColor(bgColor[graphic]);g.fillRect(0,0,getWidth(),getHeight());}You should not be calling super.repaint().  You need to call super.paintComponent(g).  repaint() schedules a call to update(), which then calls paint(), which then calls paintComponent().  You are getting high CPU because you are scheduling a paint call every time you paint.It's hard to say what your other problem is.  If you could put up the whole source somewhere, with your resources, that should help.   ;  OK, I tried changing the super.repaint() line to super.paintComponent(g).  However, then my scrollbars stop working.  If I draw a few tiles and then scroll, the top left is a white square(it looks slightly larger than 32x32).  The rest of the scroll pane(MapPanel) is that default Java-grey.I uploaded all my source files in a zip:Source ZipThanks so far,Chrisedit: fixed link;  You forgot the Scanner class.   ;  How so?  The only file that uses Scanner is OptionPanel.java, which hasimport java.util.*;So I believe the Scanner class should be covered there.  Plus wouldn't it not compile if that were the issue?[Edited by - Chryzmo on May 11, 2006 10:32:33 AM];  My bad.  I was using 1.4.2.  Switched to 1.5.0 and there it was.Your problem is that for some reason your map tiles are not being passed a properly clipped graphics object.  That is why you have that little white rectangle over everything in the scroll pane.  I haven't figured out why yet, but I made my own test class just to show it working.  Try it out.import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.GridLayout;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;public class Test extends JPanel {    Color color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());    public void paintComponent(Graphics g) {//   super.paintComponent(g);   g.setColor(color);   g.fillRect(0, 0, 32, 32);    }    public Dimension getPreferredSize() {   return new Dimension(32, 32);    }    public static void main(String args[]) {   JPanel p = new JPanel(new GridLayout(15, 15));   for(int i=0;i<15;i++) {  for(int j=0;j<15;j++) { p.add(new Test());  }   } JScrollPane sp = new JScrollPane(p);   sp.setPreferredSize(new Dimension(320, 320));   JFrame f = new JFrame();   f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   f.getContentPane().add(sp);   f.pack();   f.setVisible(true);    }}You should just ignore any Maximum and Minimum size settings.  Use only PreferredSize.  The other 2 are ignored under normal circumstances.  And under abnormal circumstances, things look weird anyway.  If you call setMax... or setMin..., you will get no results.  Calling setPref... will work.I will keep checking to see if I can find the problem.   ;  I am trying to reply with a fix, but it won't let me.   ;  I found the big problem.  You were creating methods getX() and getY().  Doing this you inadvertantly overrode the same methods from JPanel.  These methods are used to place the components in position on your window.  This was done in two files.You did this in the MapTile class and the MapPanel class.  I can't post the source for some reason.  The forum keeps locking up when I do.   ;Quote:Original post by CaptainJesterI found the big problem.  You were creating methods getX() and getY().  Doing this you inadvertantly overrode the same methods from JPanel.  These methods are used to place the components in position on your window.  This was done in two files.Yep, this did it.  It seems kind of silly that something this simple caused such a problem.  However, it is fixed and I can move on now.Thank you very much for taking the time to help :)-Chris-