Results 1 to 5 of 5
Thread: repaint() problem ?
- 02-09-2011, 05:35 AM #1
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:
Here is my GUI part where i add everything to the JFrame...Java Code:// code not following standard
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:
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 !Java Code://Code not following standard
Last edited by santa; 02-09-2011 at 09:36 AM. Reason: NON SSCCE
-
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.
- 02-09-2011, 06:41 AM #3
Im sorry i forgot main ...
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 standard
tough the gpsreciver has nothing to do with the repaint problem i left it out..Java Code:Code not following SSCCE
Here is my SSCCE version
Main:
Runs the GUI and starts the gps.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 }
GUI:
I commented on the relevant places ...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 } }
Graphic class:
and then I just call :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); } } }
so when i get a successfully parsed signal in Calculate.. then i change the boolan repaint ... create an object of GUI ..Java Code:repaint= !repaint; GUI g = new GUI(); g.cordinatepanel.revalidate(); g.cordinatepanel.repaint();
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
-
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.Java Code:repaint= !repaint; GUI g = new GUI(); g.cordinatepanel.revalidate(); g.cordinatepanel.repaint();
- 02-09-2011, 11:47 AM #5
Similar Threads
-
Problem with repaint();
By dunafrothint in forum AWT / SwingReplies: 8Last Post: 01-07-2010, 12:33 AM -
repaint problem
By amith in forum Java 2DReplies: 2Last Post: 07-01-2008, 12:10 AM -
Problem in repaint
By Preethi in forum AWT / SwingReplies: 16Last Post: 03-18-2008, 08:10 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 1Last Post: 02-16-2008, 09:12 PM -
Repaint problem
By swimberl in forum Java 2DReplies: 0Last Post: 01-06-2008, 03:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks