Results 1 to 5 of 5
  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Unhappy repaint() problem ?

    Hi guys, I fixed my last problem with displaying a circle with .setLayout(overlay);... now the part of displaying a circle works.. now i only have to update it also (maybe by a timer?).. Anyway nothing hampends when i run repaint() on my JPanel. This is my first time using repaint so dont hate me ;)

    So:
    Java Code:
    // code not following standard
    Here is my GUI part where i add everything to the JFrame...
    I also make a Graphic object with Graphic graph = new Graphic(); and add it to my JPanel :cordinatepanel.add(graph);

    Here is what the Graphics.java look like:
    Java Code:
    code not following standard
    So here im just doing a simple test trying to paint 2 different circles in different places...


    Java Code:
    //Code not following standard
    this class Calculate.java recives info from the GPS class and parse it... After it parses the data it changes the circle with repaint= !repaint; and creates a new object of GUI class and repaints cordinatepanel.
    Java Code:
    //Code not following standard
    So what am i doing wrong? ... cause it updates fine when i resize my window =/ Thank you for all toughts/help !
    Last edited by santa; 02-09-2011 at 09:36 AM. Reason: NON SSCCE

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your code cannot run or display for us (I have no idea what JFrame is being displayed for instance since none are in the code posted), and has much code present unrelated to your problem. You should learn how to reduce your problem to its essence so that you have only the minimal code to reproduce the problem but enough to allow us to compile and run it, an SSCCE. Please check out the link for the requirements, for if you follow the recommendations, you'll help us and you'll help yourself.

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Im sorry i forgot main ...
    Java Code:
    Code not following SSCCE standard
    Im have troble writing your SSCCE tough I'm a newbeginner and don't know what is depending on what in java ... I'm sorry for this.. along with the gps class GPSReader.java ... you now have all code:

    Java Code:
    Code not following SSCCE
    tough the gpsreciver has nothing to do with the repaint problem i left it out..

    Here is my SSCCE version
    Main:
    Java Code:
    public class Main {
    
     
        public static void main(String[] args) {
            GUI g = new GUI();
            GPSReader gps = new GPSReader();
            g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            g.setSize(942, 690);
            g.setResizable(false);
            g.show(true);
            gps.runGps();
        }
    //test
    }
    Runs the GUI and starts the gps.

    GUI:
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class GUI extends JFrame {
        public JPanel cordinatepanel;
        public JLabel statusbar;
        // Made by Mikael Arvidsson
       
        public GUI(){
            super("GPS BETA VERSON KREATION");
            Graphic graph = new Graphic(); // Graphic Object
            cordinatepanel = new JPanel();
            cordinatepanel.setSize(942, 643);
            cordinatepanel.add(graph); // add to cordinatepanel
            ImageIcon map = new ImageIcon("C://Users//Micke//Desktop//Map//map.jpg");
            cordinatepanel.setBackground(Color.WHITE);
            add(cordinatepanel, BorderLayout.CENTER);
            cordinatepanel.add(new JLabel(map));
            panelX=(double)cordinatepanel.getWidth();
            panelY=(double)cordinatepanel.getHeight();
            statusbar = new JLabel("Awaiting cordinates...");
           add(statusbar,BorderLayout.SOUTH);
            LayoutManager overlay = new OverlayLayout(cordinatepanel); // new layout
            cordinatepanel.setLayout(overlay); // set overlay
    
    
        }
            
    
    }
    I commented on the relevant places ...

    Graphic class:
    Java Code:
    class Graphic extends JPanel {
      public static int posX = -1, posY = -1 ;
    
    
        @Override
        public void paint(Graphics g) {
            super.paintChildren(g);
        Graphics2D g2d = (Graphics2D)g;
    
        
       // if (posX>=0 && posY >= 0){
        if(Calculate.repaint){ // repaint is a boolan that turns true of false every time i get a cordinate from the gps. Located in calculate..
        g2d.setColor(Color.RED);      // change the position/color to show that repaint() works.
        g2d.fillOval(10, 10, 11, 11);
        }else{
               g2d.setColor(Color.BLUE);
        g2d.fillOval(30,30, 40, 40);
            }
    
    }
     
       }
    and then I just call :
    Java Code:
    repaint= !repaint; 
        GUI g = new GUI();
        g.cordinatepanel.revalidate();
        g.cordinatepanel.repaint();
    so when i get a successfully parsed signal in Calculate.. then i change the boolan repaint ... create an object of GUI ..
    and try to repaint the panel...

    Im sorry if im unclear ... but i dident sleep all night because of the problem =( Thank you anyway for trying to help me =)
    Last edited by santa; 02-09-2011 at 09:38 AM. Reason: NON SSCCE

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
    repaint= !repaint; 
        GUI g = new GUI();
        g.cordinatepanel.revalidate();
        g.cordinatepanel.repaint();
    It's as I suspected. The GUI you're creating here has nothing to do with the one being displayed. Yes it's the same class, but it's a completely distinct object, and calling methods on it will have absolutely no effect on the GUI object that is being displayed, the one created in your now visible main method. Perhaps you want to pass the GUI object created into the GPSReader class, possibly via a constructor parameter, and do your method calls on that object, not on a random new GUI object created somewhere.

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    5

    Default

    Thanks =) My teacher said the same thing ... I'v been up all night not knowing what is wrong ;P maybe i should study the basics more =(

Similar Threads

  1. Problem with repaint();
    By dunafrothint in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2010, 12:33 AM
  2. repaint problem
    By amith in forum Java 2D
    Replies: 2
    Last Post: 07-01-2008, 12:10 AM
  3. Problem in repaint
    By Preethi in forum AWT / Swing
    Replies: 16
    Last Post: 03-18-2008, 08:10 PM
  4. Repaint problem
    By swimberl in forum Java 2D
    Replies: 1
    Last Post: 02-16-2008, 09:12 PM
  5. Repaint problem
    By swimberl in forum Java 2D
    Replies: 0
    Last Post: 01-06-2008, 03:28 AM

Posting Permissions

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