• Lucid Dreaming - Dream Views




    Results 1 to 5 of 5
    1. #1
      Member Rakjavik's Avatar
      Join Date
      Nov 2007
      Gender
      Location
      USA
      Posts
      462
      Likes
      7

      Java Swing, GUI locks up when painting component

      So I'm working on a crappy little 2d RPG in java to try to increase my knowledge. Ran into a problem with Swing. I've got a JFrame with 2 JScrollPanes, each JScrollPane has a custom JPanel attached for custom painting. Whenever the paint method gets called now, the GUI freezes. I've tried doing step by step debugging and still can't find the issue.

      I'll try to keep the pasted code as little as possible. If anyone has any ideas, or needs me to post more of my classes in the project let me know!


      // The JFrame and constructor //
      public class EditMaps extends javax.swing.JFrame {

      public static Board map;
      private MapJPanel mapPanel;
      private PalletteJPanel palletteJPanel;
      public static int selectedPallette = -1;
      public static int selectedTile = 0;

      public EditMaps() throws IOException, ClassNotFoundException
      {
      initComponents();
      this.setResizable(false);
      this.setLayout(null);
      map = new Board();


      mapPanel = new MapJPanel();
      mapPanel.setPreferredSize(new Dimension(1000, 1000));
      mapPanel.setBorder(BorderFactory.createLineBorder( Color.red));
      mapScrollPane.getViewport().add(mapPanel);
      mapScrollPane.setViewportView(mapPanel);
      palletteJPanel = new PalletteJPanel();
      palletteScrollPanel.getViewport().add(palletteJPan el);
      palletteScrollPanel.setViewportView(palletteJPanel );








      tileSetDropDown.addItem("Choose Tileset");
      for (String item : MapTool.getTileSetNames())
      {
      tileSetDropDown.addItem(item);
      }

      }

      //Method that causes freezing due to calling the revalidate methods on the panels//

      private void tileSetDropDownActionPerformed(java.awt.event.Acti onEvent evt) {
      if (tileSetDropDown.getSelectedIndex() > 0)
      {
      try
      {
      map.setTileSet(MapTool.getTileSet(tileSetDropDown. getSelectedIndex() - 1));
      int height = 35*(map.getTileSet().getTiles().size()/5)+10;
      palletteJPanel.setPreferredSize(new Dimension(palletteScrollPanel.getWidth(), height));
      palletteScrollPanel.setPreferredSize(new Dimension(palletteScrollPanel.getWidth(), height));
      palletteScrollPanel.revalidate();
      palletteJPanel.revalidate();

      }
      catch (IOException ex)
      {

      }
      catch (ClassNotFoundException ex)
      {

      }
      }
      }

      //The custom JPanel being updated in the previous method //
      /*
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */

      package rpg.maps;

      import java.awt.Color;
      import java.awt.Dimension;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.util.ArrayList;
      import javax.swing.JPanel;


      /**
      *
      * @author 501282234
      */
      public class PalletteJPanel extends JPanel
      {

      public PalletteJPanel()
      {
      setSize(220, 530);
      setPreferredSize(new Dimension(220, 530));

      }

      @Override
      public void paintComponent(Graphics g)
      {

      Graphics2D g2d = (Graphics2D) g;
      super.paintComponent(g2d);



      try
      {
      if (EditMaps.map.getTileSet().getTiles().get(0).getIm age() != null)
      {
      ArrayList<Tile> tiles = EditMaps.map.getTileSet().getTiles();
      int y = 10;
      int x = 16;
      int count=0;

      for (count = 0; count < tiles.size()
      {
      if (count % 5 == 4)
      {
      y+=35;
      x=16;
      }
      tiles.get(count).paint(x, y, 32, g2d);
      x+=35;
      }
      for (Tile tile : tiles)
      {


      if (count % 5 == 4)
      {
      y+=35;
      x=16;
      }
      tile.paint(x, y, 32, g2d);
      x+=35;
      count++;
      }
      }
      if (EditMaps.selectedPallette > -1)
      {


      int x = (EditMaps.selectedPallette % 5);
      int y = ((EditMaps.selectedPallette-x) / 5);


      x = x*35+16;
      y = y*35+10;

      g2d.setColor(Color.red);
      g2d.drawRect(x-1, y-1, 33, 33);
      }
      }
      catch(NullPointerException ex)
      {
      System.out.println("Null pointer when scanning tiles");
      }
      }

      }

    2. #2
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      Do you think it is a problem with your code or with Swing?

      Do you use an IDE? (as they can help with errors)
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    3. #3
      Member Rakjavik's Avatar
      Join Date
      Nov 2007
      Gender
      Location
      USA
      Posts
      462
      Likes
      7
      I use Netbeans as an IDE. I honestly don't think it's my code, but I may be wrong, hence my posting it on here.

    4. #4
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      When you say the GUI freezes, do you mean that the JFrame stops responding to any input, or does the java process (javaw.exe or something?) stop responding? Or both?
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    5. #5
      Member Rakjavik's Avatar
      Join Date
      Nov 2007
      Gender
      Location
      USA
      Posts
      462
      Likes
      7
      JFrame stop responding. Infinite loop. One of my for loops wasn't incrementing the counting int. Ugg. Thanks for the replies though

    Similar Threads

    1. Locks?
      By KaylaHansa in forum General Dream Discussion
      Replies: 2
      Last Post: 07-08-2010, 11:53 PM
    2. PC building + component swapping
      By Taosaur in forum Tech Talk
      Replies: 7
      Last Post: 05-03-2008, 02:46 PM
    3. Do not break the locks on the toilets
      By Original Poster in forum Dream Control
      Replies: 8
      Last Post: 05-18-2007, 08:33 AM
    4. Playstation 2 component output issues... weird
      By Replicon in forum Tech Talk
      Replies: 0
      Last Post: 05-06-2007, 07:47 PM

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •