spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Java Jpanel Repaint/update probleme


I can't say that I've read all your code, but your model never notifies the view. I mean nowhere inside of the model class's code is the notifyView() method called. It should be the model's responsibility to call this whenever its state is changed.

Note that this:

car.update(car.getGraphics()); //
<< the Problem is HERE

Should not be used as the Graphics obtained is not stable.

Also, your model holds view components, an ArrayList of Car, a class that extends JPanel, another thing that should never occur as the model should be completely ignorant of the view, with the exception being that it knows that some things may be listening to it, and it needs to notify those things, and that's it. Instead your Model should hold an ArrayList of non-view, non-JPanel logical Car objects.


Edit
You state:

In the model i have the method: public void notifyView() which is called by the car's method public void Move() , That means whenever the thread calls the move method of the car, it calls the notifyView of the model

No, the Car should not be calling this method -- only the model itself should call this when its state is changed.

Also, I see that you've got very dangerous code using Thread#suspend() and Thread#resume() method calls. These methods have been deprecated as they've been found to be dangerous. To find out why, please check the API for Thread as well as this useful article. You will most definitely want to avoid their use.


Suggestions

  • Make Car a logical non-GUI class that knows its position and can have its position changed.
  • Override your drawing JPanels protected void paintComonent(Graphics g) method and use the Car information from the model to draw the Cars. In other words, use the state of the model to affect the state of the view.
  • Use a Swing Timer instead of a background thread, to change your Car positions.
  • Have the model and only the model call the notifyView method when its state has been changed.

Edit
Your main bug is here:

class Car extends JPanel {
    private int id, x, y, vitess;

    //....

    public int getX() {
       return x;
    }

    public int getY() {
       return y;
    }

You're inadvertently overriding the Car JPanel's getX and getY methods, messing up the location of these components. This is another reason to avoid overriding Swing components unless necessary -- to avoid these hidden side effects.

Get rid of or rename these methods.


Categories : Java

Related to : Java Jpanel Repaint/update probleme
mysql join table query probleme
Try SELECT DISTINCT ... Without DISTINCT the WHERE filters from all possible combinations of a row from each FROM table. Using DISTINCT removes duplicates after that.

Categories : Mysql
KeyLisenter, repaint(), methods
Problems Player does not have methods... addKeyListener repaint It does not inherit from anything, therefore it can't call... super.paint, as it's parent classes do not have this method... You can't call what you don't have... Player does not meet to contractual requirements of the KeyListener interface, it does not implement public void keyReleased(KeyEvent e) Painting is for painti

Categories : Java
How to call java paintComponent using repaint
There are some minor issues with the code, but I assume that it's only a small snippet for demonstration purposes. For details, have a look at Performing Custom Painting. Actually, this tutorial would also answer your question, but to summarize it: The paintComponent method will be called automatically, "by the operating system", whenever the component has to be repainted. The call to repaint()

Categories : Java
Add JApplet to JPanel
As suggested here, JChemPaint is a standard Java application. See showInstance() for an example of constructing a JChemPaintPanel and adding it to a JFrame.

Categories : Java
Painting on JPanel
Try to create new panel and put it in the center, then override its paintComponent method. see Custom Painting and also here

Categories : Java
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda myMap.stream().count() != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.