Announcement: Be excellent to each other.


Caravel Forum : Other Boards : Anything : Java programming problem with transparency flashing.
New Topic New Poll Post Reply
Poster Message
bradwall
Level: Smiter
Avatar
Rank Points: 423
Registered: 02-12-2003
IP: Logged
icon Java programming problem with transparency flashing. (0)  
I was playing around with transparancy in Java and wanted to create a selection (rubber band) box. When I click and drag the initial band works fine... however, the transparent fill in of the box flashes (espessially when the box is big). Does anyone know how to get around this? The flashing is real annoying. (Sorry about all the commented out crap... I was playing around).

Here is the code for the JPanel that I put into a Frame:
package rubberbandtest;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.Rectangle;


public class MyPanel extends javax.swing.JPanel {
    
    /** Creates new form MyPanel */
    public MyPanel() {
        initComponents();
        //System.setProperty("sun.awt.noerasebackground", "true");
    }

    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();

        setLayout(null);

        addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                formMouseDragged(evt);
            }
        });
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
        });

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Some value");
        jLabel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 0, 102)));
        jLabel1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        add(jLabel1);
        jLabel1.setBounds(90, 90, 70, 10);

    }


    private void formMouseDragged(java.awt.event.MouseEvent evt) {
        int X = evt.getX();
       int Y = evt.getY();
       System.out.println("X: " + X + "   Y: " + Y);
        
       c_x2= X; 
       c_y2= Y; 
       if (X < 0) {X = 0;}; 
       if (Y < 0) {Y = 0;}; 

       doBox(); 

       return; 
    }

    private void formMouseReleased(java.awt.event.MouseEvent evt) {

        repaint();
    }

    private void formMousePressed(java.awt.event.MouseEvent evt) {
       int X = evt.getX();
       int Y = evt.getY();
       
       System.out.println("X: " + X + "   Y: " + Y);
        
       c_x1= X; 
       c_y1= Y; 
       if (X < 0) {X = 0;}; 
       if (Y < 0) {Y = 0;}; 

       return; 
    }
    
    
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    // End of variables declaration
    
    /* 
 *********************** 
 * doBox  
 *  
 * Purpose: Draws rubber band box with Java. 
 */ 
    private final void doBox(){ 
    
       
      Graphics g = this.getGraphics();
      Graphics2D g2d = (Graphics2D)this.getGraphics();
      //this.update(g);
      super.paint(g);   //eliminates flashing a bit better than this.update(g) does.
      //this.update(g2d);
      //super.paint(g2d);
       
      g.setColor(Color.blue); 
      g.drawLine(c_x1,c_y1,c_x1,c_y2); 
      g.drawLine(c_x1,c_y2,c_x2,c_y2); 
      g.drawLine(c_x2,c_y2,c_x2,c_y1); 
      g.drawLine(c_x2,c_y1,c_x1,c_y1);
      

      
	//Color tempColor = g2d.getColor();
		g2d.setColor(Color.blue);
		float alpha = .2f;
		Composite composite = g2d.getComposite();
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
		//Rectangle rect = getRectangle();
		//g2d.fill(rect);
                if(c_x1 < c_x2 && c_y1 < c_y2)
                    g2d.fillRect(c_x1, c_y1, c_x2-c_x1, c_y2-c_y1);
                if(c_x1 > c_x2 && c_y1 > c_y2)
                    g2d.fillRect(c_x2, c_y2, c_x1-c_x2, c_y1-c_y2);
                if(c_x1 < c_x2 && c_y1 > c_y2)
                    g2d.fillRect(c_x1, c_y2, c_x2-c_x1, c_y1-c_y2);
                if(c_x1 > c_x2 && c_y1 < c_y2)
                    g2d.fillRect(c_x2, c_y1, c_x1-c_x2, c_y2-c_y1);

                
                
		g2d.setComposite(composite);
		//g2d.draw(rect);
		//g2d.setColor(tempColor);
        
        //this.update(g);
                

      
    } 

       int                              c_xval1=0;          // initial x for rubber band box 
       int                              c_yval1=0;          // initial y for rubber band box 
       int                              c_xval2=0;          // final x for rubber band box 
       int                              c_yval2=0;          // final y for rubber band box 
       int                              c_x1=0;                 // initial x java 
       int                              c_y1=0;                 // initial y java 
       int                              c_x2=0;                 // final x java 
       int                              c_y2=0;                 // final y java 

} 

09-20-2005 at 01:38 PM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts Quote Reply
Schik
Level: Legendary Smitemaster
Avatar
Rank Points: 5383
Registered: 02-04-2003
IP: Logged
icon Re: Java programming problem with transparency flashing. (+1)  
Let me preface this by saying that I've never done anything serious in Java....

But, it looks to me like you're drawing directly to the screen buffer. So, you basically draw the background, then draw lines and a rectangle over it. That's all done on the visible screen, so it makes sense that it flickers. Sometimes there will be a refresh between drawing the background and drawing the rectangle.

What you probably want to do is render to an off-screen buffer, then blit that to the visible screen. How to go about doing that in Java is an exercise I'll leave to you. :P

____________________________
The greatness of a nation and its moral progress can be judged by the way it treats its animals.
--Mahatma Gandhi
09-20-2005 at 01:47 PM
View Profile Send Private Message to User Send Email to User Show all user's posts High Scores Quote Reply
bradwall
Level: Smiter
Avatar
Rank Points: 423
Registered: 02-12-2003
IP: Logged
icon Re: Java programming problem with transparency flashing. (0)  
I will see what I can do with that.
Thanks
09-20-2005 at 01:50 PM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts Quote Reply
Schik
Level: Legendary Smitemaster
Avatar
Rank Points: 5383
Registered: 02-04-2003
IP: Logged
icon Re: Java programming problem with transparency flashing. (0)  
I guess I could have been helpful enough to mention that this is called double buffering. Googling for java double buffer brings up this page, with which even I could write something double buffered in Java.... I think ;)

____________________________
The greatness of a nation and its moral progress can be judged by the way it treats its animals.
--Mahatma Gandhi
09-20-2005 at 02:04 PM
View Profile Send Private Message to User Send Email to User Show all user's posts High Scores Quote Reply
bradwall
Level: Smiter
Avatar
Rank Points: 423
Registered: 02-12-2003
IP: Logged
icon Re: Java programming problem with transparency flashing. (0)  
Thanks for your help.
I figured out what I was doing wrong. It had to do that I wasn't using paintComponent() correctly.
Thanks again.
09-20-2005 at 03:28 PM
View Profile Send Private Message to User Send Email to User Visit Homepage Show all user's posts Quote Reply
New Topic New Poll Post Reply
Caravel Forum : Other Boards : Anything : Java programming problem with transparency flashing.
Surf To:


Forum Rules:
Can I post a new topic? No
Can I reply? No
Can I read? Yes
HTML Enabled? No
UBBC Enabled? Yes
Words Filter Enable? No

Contact Us | CaravelGames.com

Powered by: tForum tForumHacks Edition b0.98.8
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team and the Caravel team.