Results 1 to 5 of 5
  1. #1
    creative86 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default flicker in a JPanel

    Hi, I have the following problem, I'm drawing a graph in a JPanel, and I could not fix the flickering that has the JPanel, implements double buffering and I think that is wrong, the code is as follows,I attach the project developed

    PHP Code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.Graphics2D;
    import java.awt.Graphics;
    
    
    public class PruebaDeDibujoView1 extends JFrame implements Runnable {
        Thread hilo = null;
    
    
    
        final static float dash1[] = {6.0f},dash2[]={1.0f};
        final static BasicStroke dashed = new BasicStroke(1.0f,
                                              BasicStroke.CAP_BUTT,
                                              BasicStroke.JOIN_MITER,
                                             30, dash1, 100);
        final static BasicStroke dashed2 = new BasicStroke(2.0f,
                                              BasicStroke.CAP_ROUND,
                                              BasicStroke.JOIN_MITER,
                                              10.0f, dash2, 0);
        int ancho=400, alto=400;
    //    public double posx[] = new double[10000000],posy[] = new double[10000000];
        public double posx[] = new double[ancho*2], posy[] = new double[ancho*2];
        public Image bi;
        public Graphics2D big;
    
    
    
        /** Creates new form PruebaDeDibujoView */
        public void run(){
    
                while (true) {
            try{
                Thread.sleep(10);
            }catch (InterruptedException e) { }
            desplazar();
            repaint();
            jPanel1.repaint();
            
    
        }
    
        
    
        }
    
    
    
        public PruebaDeDibujoView1() {
            initComponents();
            inicializarEje();
        }
        private void inicializarEje(){
            for (int i=0; i<posx.length; i++)
          //    posx[i] = i + 200;
                posx[i] = i;
        }
    
        private void desplazar(){
            if (posx[0]>399)
                    inicializarEje();
            for (int i=0; i<posx.length; i++)
                posx[i] = posx[i] - 1;
        }
      
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jPanel1 = new javax.swing.JPanel();
            botonLimpiar = new javax.swing.JButton();
            botonDesplazar = new javax.swing.JButton();
            botonFSeno = new javax.swing.JButton();
            botonDesplazar2 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jPanel1.setBackground(java.awt.Color.black);
            jPanel1.setPreferredSize(new java.awt.Dimension(400, 400));
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 400, Short.MAX_VALUE)
            );
    
            botonLimpiar.setText("Limpiar");
            botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    botonLimpiarActionPerformed(evt);
                }
            });
    
            botonDesplazar.setText("Desplazar");
            botonDesplazar.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    botonDesplazarActionPerformed(evt);
                }
            });
    
            botonFSeno.setText("Funcion Seno");
            botonFSeno.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    botonFSenoActionPerformed(evt);
                }
            });
    
            botonDesplazar2.setText("Desplazar 2");
            botonDesplazar2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    botonDesplazar2ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(59, Short.MAX_VALUE))
                .addGroup(layout.createSequentialGroup()
                    .addGap(46, 46, 46)
                    .addComponent(botonDesplazar)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(botonDesplazar2, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(botonFSeno)
                    .addGap(18, 18, 18)
                    .addComponent(botonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(56, 56, 56))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(24, 24, 24)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(27, 27, 27)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonFSeno)
                        .addComponent(botonDesplazar2)
                        .addComponent(botonDesplazar)
                        .addComponent(botonLimpiar))
                    .addContainerGap(30, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
       
        private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    
        }                                          
    
    
        private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {                                             
       
            Graphics q=jPanel1.getGraphics();
            Graphics2D s=(Graphics2D) q ;
            s.fillRect(0, 0, alto, ancho);
            
        }                                            
    
    
        public void paint(Graphics g){
            super.paint(g);
            //super.paintComponents(g);
            Graphics q = jPanel1.getGraphics();
            Graphics2D g2 = (Graphics2D) q;
          
    
            bi = createImage(ancho, alto);
            big=(Graphics2D) bi.getGraphics();
            big.setColor(Color.black);        
            big.fillRect(0, 0, alto, ancho);        //se borra el anterio gráfico
            big.setColor(Color.yellow);
            big.setStroke(new BasicStroke(1.5f)); // grosor de 2.5 pixels
            big.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);//Para suavizar la imagen, borra las altas frecuencias.
            big.setColor(Color.black);
            big.fillRect(0, 0, alto, ancho);        //se borra el anterio gráfico
            big.setColor(Color.yellow);
    
    
                for (int i=0; i<posx.length-1; i++){
                    big.draw(new Line2D.Double(posx[i],posy[i],posx[i+1],posy[i+1]));
                }
            g2.drawImage(bi, 0, 0, null);
            g2.dispose();
    
        }
    
        private void funcion(){
            for (int i=0; i<posx.length; i++){
                posy[i] = 200 * Math.sin(0.05*i) + 200 ;
            }
        }
    
    
    
        private void botonDesplazarActionPerformed(java.awt.event.ActionEvent evt) {                                               
            repaint();
            desplazar();
      
        }                                              
    
        private void botonFSenoActionPerformed(java.awt.event.ActionEvent evt) {                                           
            funcion();
            repaint();
        }                                          
    
        private void botonDesplazar2ActionPerformed(java.awt.event.ActionEvent evt) {                                                
            botonDesplazar2.setEnabled(false);
            if (hilo == null){
                hilo = new Thread(this);
                hilo.start();
            }
        }                                               
    
      
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
    
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    
                    new PruebaDeDibujoView1().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton botonDesplazar;
        private javax.swing.JButton botonDesplazar2;
        private javax.swing.JButton botonFSeno;
        public javax.swing.JButton botonLimpiar;
        public javax.swing.JPanel jPanel1;
        // End of variables declaration
    
    
    
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    First, you should not perform drawing the paint method, rather override the paintComponent method. Next, do not use getGraphics to perform drawing...override the paintComponent method and use the graphics passed to this method for drawing. Suggested reading: Java 2D

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

    Default

    Quote Originally Posted by doWhile View Post
    First, you should not perform drawing the paint method, rather override the paintComponent method. Next, do not use getGraphics to perform drawing...override the paintComponent method and use the graphics passed to this method for drawing. Suggested reading: Java 2D
    ++

    Also, don't use getGraphics for a JComponent. The tutorials will tell you more on this.

  4. #4
    creative86 is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    utilizando netbeans, y no se otra manera de dibujar dentro del jpanel, ademas , no he podido hacer funcionar el programa reescribiendo el metodo paintcomponent

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

    Default

    Quote Originally Posted by creative86 View Post
    utilizando netbeans, y no se otra manera de dibujar dentro del jpanel
    No estoy de acuerdo. Se puede usar paintComponent con or sin utilizando netbeans.


    ademas , no he podido hacer funcionar el programa reescribiendo el metodo paintcomponent
    entonces es probable que no usa el metodo correctamente.

    (sorry for the poor spanish).

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2011, 05:30 PM
  2. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  3. Alpha Fade Flicker
    By jamesfrize in forum Java Applets
    Replies: 3
    Last Post: 04-02-2008, 02:02 PM
  4. flicker
    By angel_eyez in forum New To Java
    Replies: 3
    Last Post: 01-14-2008, 10:52 PM
  5. Flicker flicker!
    By angel_eyez in forum Java 2D
    Replies: 1
    Last Post: 01-13-2008, 07:52 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
  •