Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default .repaint() method does not work?

    I am having issues with the repaint method. I overrode the paintComponent method, but when i call repaint with JPanel nothing happens.
    My code:
    Java Code:
     
    private class DrawPanel extends JPanel {
        private int[] zahlen;
        
        @Override
        public void paintComponent (Graphics g){
    
            super.paintComponents(g);
            int x = 8;
            
            for(int i = 0; i < zahlen.length; i++){
    
                int zahl = zahlen[i];
                g.drawLine(x, 233-(zahl*2), x, 233);
                x = x+3;
                
                
            }
        }
    }
    Java Code:
            String Stringbox1 = (String) sortBox1.getSelectedItem();
            String Stringbox2 = (String) sortBox2.getSelectedItem();
            String Stringbox3 = (String) sortBox3.getSelectedItem();
            String Stringbox4 = (String) sortBox4.getSelectedItem();
            String Selectionsort = "Selectionsort";
            String Bubblesort = "Bubblesort";
            String Insertionsort = "Insertionsort";
            String Quicksort = "Quicksort";
            String Mergesort = "Mergesort";
            
            
            
    if (Stringbox1.equals(Bubblesort)){
                Panel1.repaint();
                bubbleSort(zahlen);
                Panel1.repaint(); 
            } else if (Stringbox1.equals(Selectionsort)){
                selectionSort(zahlen);
                
    
                
              } 
              else if (Stringbox1.equals(Insertionsort)){
                Panel1.repaint();
                  insertionSort(zahlen);
                Panel1.repaint();
              } 
               else if (Stringbox1.equals(Quicksort)){
                Panel1.repaint();
                   quickSortKomplett(zahlen);
                Panel1.repaint();
              }
               else if (Stringbox1.equals(Mergesort)){
                  Panel1.repaint();
                   mergeSort(zahlen);
                  Panel1.repaint(); 
              }
    if you will need it, i can upload my whole project :)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: .repaint() method does not work?

    The action requested by a call to repaint() will be done some time after repaint() is called when the JVM gets the EDT and has a chance to call the paint/paintComponent methods. The action is not done in the call to repaint().
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    So repaint() doesnt directly call paintComponent? Any ideas how i can fix that in my case?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: .repaint() method does not work?

    If that code is not executing on the EDT, calling the SwingUtilities class's invokeAndWait() method could do what you want. I haven't used it to know for sure.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,412
    Rep Power
    5

    Default Re: .repaint() method does not work?

    paintComponent is called relatively quickly once a repaint is issued. You don't need to invoke paintComponent directly. What exactly is Paint1 set too? If it is an instance field it should be lower case (because otherwise it is confusing).

    Also, please submit a Short, Self Contained, Correct Example on how you are painting. I have done lots of graphics and have never had a response/delay problem. I suspect you are using it incorrectly.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    Even if i import javax.swing.SwingUtilities, i get an error if i want to write it after repaint(). Btw what do i have write in the brackets?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: .repaint() method does not work?

    i get an error
    Please copy the full text of the error message and paste it here.

    Follow Jim's suggestion and make a SSCCE that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    First of all i got another question: Is it definitly necessary to build up the gui in the code? If so, why is there the 'Design' button where you can just drag and drop your whole layout?
    I am working on an visualisation of different sorting algorithms. My goal is to display a few random figures in the different panels and during the sorting process the panel should refresh constantly so you can see how the sorting algorithm works. It should look like this:
    .repaint() method does not work?-visu.png
    My code by now:
    Java Code:
    private class DrawPanel extends JPanel {
        private int[] zahlen;
        
        @Override
        public void paintComponent (Graphics g){
    
            super.paintComponents(g);
            int x = 8;
            
            for(int i = 0; i < zahlen.length; i++){
    
                int zahl = zahlen[i];
                g.drawLine(x, 233-(zahl*2), x, 233);
                x = x+3;
                
                
            }
        }
    }
    
        private void jButton7MouseClicked(java.awt.event.MouseEvent evt) {                                           
            
            String Stringbox1 = (String) sortBox1.getSelectedItem();
            String Bubblesort = "Bubblesort";
          
    if (Stringbox1.equals(Bubblesort)){
                
                Panel1.repaint();
                bubbleSort(zahlen);
                Panel1.repaint(); 
            } else if (Stringbox1.equals(Selectionsort)){
                selectionSort(zahlen);
    I did the whole Layout in the Design Section so there is nothing else to show beside the sorting algorithms. But i am beginning to think that this is the problem

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,412
    Rep Power
    5

    Default Re: .repaint() method does not work?

    First, that is not an SSCCE (I can't begin to compile it). In this case SSCCE should show the "type" of problem you are having with painting (no more than 100 lines of code). Second, your Design question sounds IDE specific since I am unaware of a Design button as it relates to Java. Finally, as I stated before, you don't show where Panel1 is defined or even initialized.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    Java Code:
    import java.math.*;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.BorderFactory;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    /**
     *
     * @author 
     */
    public class Facharbeit extends javax.swing.JFrame {
        private JFrame f;
        private JPanel content;
        private JPanel p1;
        private JPanel p2;
        private JPanel p3;
        private JPanel p4;
        private JComboBox box1;
        private JComboBox box2;
        private JComboBox box3;
        private JComboBox box4;
        private JButton b1;
        int anzahl = 10;
        int[] zahlen;
        
        private void gui(){
            f = new JFrame("Visualisierung von Sortierverfahren");
            f.setVisible(true);
            f.setSize(1000, 800);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().setLayout(new BorderLayout());
            
            content = new JPanel(new GridBagLayout());
            content.setBackground(Color.GRAY);
            GridBagConstraints c = new GridBagConstraints();
            p1 = new JPanel();
            p2 = new JPanel();
            p3 = new JPanel();
            p4 = new JPanel();
            p1.setBorder(BorderFactory.createEtchedBorder());
            p1.setPreferredSize(new Dimension(100,200));
            p2.setBorder(BorderFactory.createRaisedBevelBorder());
            p2.setPreferredSize(new Dimension(100,200));
            p3.setBorder(BorderFactory.createRaisedBevelBorder());
            p3.setPreferredSize(new Dimension(100,200));
            p4.setBorder(BorderFactory.createRaisedBevelBorder());
            p4.setPreferredSize(new Dimension(100,200));
            String[] items = {"Bubblesort", "Selectionsort", "Insertionsort", "Quicksort", "Mergesort"};
            box1 = new JComboBox(items);
            box2 = new JComboBox(items);
            box3 = new JComboBox(items);
            box4 = new JComboBox(items);
            b1 = new JButton("sortieren");
            
            c.insets = new Insets(30, 30 , 30 , 30);
            c.gridx = 0;
            c.gridy = 2;
            b1.addActionListener(new ActionListener(){
               @Override
                public void actionPerformed(ActionEvent evt){
                    sortieren();
                    p1.repaint();
                    bubbleSort(zahlen);
                    p1.repaint();
                }
            });
            
            
            content.add(b1, c);
            c.gridx = 0;
            c.gridy = 1;        
            content.add(box1, c); 
                    c.gridx = 1;
            content.add(box2, c); 
                    c.gridx = 2;
            content.add(box3, c);
                    c.gridx = 3;
            content.add(box4, c);
            
            c.gridx = 0;
            c.gridy = 0;
            content.add(p1, c);
                    c.gridx = 1;
                    c.gridy = 0;
            content.add(p2, c); 
                    c.gridx = 2;
                    c.gridy = 0;        
            content.add(p3, c); 
                    c.gridx = 3;
                    c.gridy = 0;        
            content.add(p4, c);       
            f.add(content, BorderLayout.WEST);
        
        }
        public void sortieren(){
                zahlen = new int [anzahl];
            for (int i = 0; i < anzahl; i++){
                java.util.Random randomGenerator = new java.util.Random();
                zahlen[i] = randomGenerator.nextInt(100);
                
                  }
        }
    
         private class DrawPanel extends JPanel {
        private int[] zahlen;
        
        @Override
        public void paintComponent (Graphics g){
    
            super.paintComponents(g);
            int x = 8;
            
            for(int i = 0; i < zahlen.length; i++){
    
                int zahl = zahlen[i];
                g.drawLine(x, 233-(zahl*2), x, 233);
                x = x+3;
                
                
            }
        }
    }
    
        public int[] bubbleSort( int[] zahlen ) {
            int k = zahlen.length;
            
                while (k > 1) {
                    int i = 0;
                    while ( i < (k-1)) {
                        if (zahlen[i] > zahlen[i+1]) {
                            int puffer = zahlen[i];
                            zahlen[i] = zahlen[i+1];
                            zahlen[i+1] = puffer;
                           
    }
                            i = i+1;
    }
                            k = k-1;
    }
                            return zahlen;
    
    
        }
    
        public Facharbeit() {
            initComponents();
            gui();
        }
    I am sorry i didnt not understand the SSCCE properly. I know its a bit long, but otherwise it wouldnt compile. I worked on it so that the gui is directly seen in the code, but i got the same problem.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: .repaint() method does not work?

    There is no main() method for executing it.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    Damn, forgot the last part.
    Java Code:
        public static void main(String args[]) {
       
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                   new Facharbeit().setVisible(true);
                }
            });
        }
                     
    }

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,412
    Rep Power
    5

    Default Re: .repaint() method does not work?

    And initComponents() is missing.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    Isnt it enough to write it in public Facharbeit()?

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,412
    Rep Power
    5

    Default Re: .repaint() method does not work?

    Where is the method?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: .repaint() method does not work?

    What does the posted code do when it is executed? How can we see the problem?

    Isnt it enough to write it in public Facharbeit()?
    If would be if that method was defined in the JFrame class. But it is not defined, so the compiler gives an error.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: .repaint() method does not work?

    1. I don't see where you add the DrawPanel to the frame
    2. The DrawPanel does not have a preferred size so it will not display. You need to override the getPreferredSize() method of the DrawPanel to return an appropriate Dimension
    3. You should invoke super.paintComponent(g) (without the "s").

  18. #18
    lennarttw is offline Member
    Join Date
    Mar 2014
    Posts
    8
    Rep Power
    0

    Default Re: .repaint() method does not work?

    Quote Originally Posted by camickr View Post
    3. You should invoke super.paintComponent(g) (without the "s").
    Omg that was it!! Thank you so much, you guys are heros :D
    This thread can be closed.
    Lennarttw

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,412
    Rep Power
    5

    Default Re: .repaint() method does not work?

    Quote Originally Posted by lennarttw View Post
    Omg that was it!! Thank you so much, you guys are heros :D
    This thread can be closed.
    Didn't your IDE or at least the Javac compiler flag that as an compilation error? You described this as a runtime error.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: .repaint() method does not work?

    I don't know how you can get an IDE to tell you that you have called the wrong method.
    @Override will tell you if the method you are trying to override doesn't exist.
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. How does frame.repaint and paintComponent work?
    By fatabass in forum AWT / Swing
    Replies: 1
    Last Post: 12-07-2011, 11:52 PM
  2. Replies: 3
    Last Post: 11-20-2011, 05:50 PM
  3. Repaint calling Statement Doesn't Work in my code ?
    By nitin_daviet88 in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-24-2010, 03:09 PM
  4. repaint class doesnt work anymore... dunno why..
    By Addez in forum New To Java
    Replies: 9
    Last Post: 11-07-2009, 09:10 PM
  5. mouse click do not work after repaint
    By nobody in forum Java 2D
    Replies: 8
    Last Post: 12-07-2008, 04:43 PM

Posting Permissions

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