Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 5th, 2005, 3:53 PM   #1
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
Question Drawing using vectors

Hi all,
I am writing a program which is similar to Microsoft's Paint. In order to draw the shapes, I am using vectors. However, I have a problem because when I draw a line and then a circle over the line, after the panel repaints, the line comes over the circle.
Can anyone tell me how to avoid this. I mean that the object that is drawn first on the panel should stay in the back when other objects are drawn after it and not come to front.
HeX is offline   Reply With Quote
Old May 5th, 2005, 5:42 PM   #2
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4 Dameon is on a distinguished road
Look at your painting method carefully. What is likely happening is that when you add something to your list of things to paint, it is added to the end, but when you paint, you start at the beginning of the array. Make a minor change to reverse the direction of your iteration and see if that works.
Dameon is offline   Reply With Quote
Old May 6th, 2005, 6:58 AM   #3
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
here is the painting method,

  if( mode == 1 )
            g.drawLine( X1, Y1, X2, Y2 ); 
      
         if( mode == 2 )
            g.drawLine( LX1, LY1, LX2, LY2 );
         if( mode == 3 )    
             g.fillRect( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
        
         if( mode == 4 )      
             g.fillOval( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
         
         if( mode == 5 )           
            g.drawRect( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
            
         if( mode == 6 )
            g.drawOval( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));   

            
         repaint();
what should I change?
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 6th, 2005, 8:32 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
First of all, use a switch-case statement to replace all those ifs:
	switch (mode)
	{
		case 1:
			g.drawLine( X1, Y1, X2, Y2 );
			break;
		case 2:
			g.drawLine( LX1, LY1, LX2, LY2 );
			break;
		case 3:
			g.fillRect( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
			break;
		case 4:
			g.fillOval( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
			break;
		case 5:
			g.drawRect( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
			break;
		case 6:
			g.drawOval( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
	}
	
	repaint();
Is this code in a loop?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 6th, 2005, 8:46 AM   #5
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
yep. This code is ion a loop. Thanx for the switch command. I knew that before but i never use it.
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 6th, 2005, 8:47 AM   #6
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
yep. This code is on a loop. Thanx for the switch command. I knew that before but i never use it.
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 6th, 2005, 8:49 AM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
Can we see the loop?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 6th, 2005, 8:55 AM   #8
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
Actually, only the code for filling the objects has loops, and this not. However, here is the entire method:
public void paintComponent( Graphics g )
      {
         super.paintComponent(g);
          
         for( int i=0; i< fRectVec.size(); i++ )
         {
            g.setColor( ((Specific)fRectVec.elementAt(i)).gCol() );
            g.fillRect( Math.min(((Specific)fRectVec.elementAt(i)).gX1(),((Specific)fRectVec.elementAt(i)).gX2()), Math.min(((Specific)fRectVec.elementAt(i)).gY2(),((Specific)fRectVec.elementAt(i)).gY1()), Math.abs(((Specific)fRectVec.elementAt(i)).gX1() - ((Specific)fRectVec.elementAt(i)).gX2()), Math.abs(((Specific)fRectVec.elementAt(i)).gY1() - ((Specific)fRectVec.elementAt(i)).gY2()) );
         }
         for( int i=0; i< fOvalVec.size(); i++ )
         {
            g.setColor( ((Specific)fOvalVec.elementAt(i)).gCol() );
            g.fillOval( Math.min(((Specific)fOvalVec.elementAt(i)).gX1(),((Specific)fOvalVec.elementAt(i)).gX2()), Math.min(((Specific)fOvalVec.elementAt(i)).gY2(),((Specific)fOvalVec.elementAt(i)).gY1()), Math.abs(((Specific)fOvalVec.elementAt(i)).gX1() - ((Specific)fOvalVec.elementAt(i)).gX2()), Math.abs(((Specific)fOvalVec.elementAt(i)).gY1() - ((Specific)fOvalVec.elementAt(i)).gY2()) );
         }
         for( int i=0; i< OvalVec.size(); i++ )
         {
            g.setColor( ((Specific)OvalVec.elementAt(i)).gCol() );
            g.drawOval( Math.min(((Specific)OvalVec.elementAt(i)).gX1(),((Specific)OvalVec.elementAt(i)).gX2()), Math.min(((Specific)OvalVec.elementAt(i)).gY2(),((Specific)OvalVec.elementAt(i)).gY1()), Math.abs(((Specific)OvalVec.elementAt(i)).gX1() - ((Specific)OvalVec.elementAt(i)).gX2()), Math.abs(((Specific)OvalVec.elementAt(i)).gY1() - ((Specific)OvalVec.elementAt(i)).gY2()) );
         } 
         for( int i=0; i< RectVec.size(); i++ )
         {
            g.setColor( ((Specific)RectVec.elementAt(i)).gCol() );
            g.drawRect( Math.min(((Specific)RectVec.elementAt(i)).gX1(),((Specific)RectVec.elementAt(i)).gX2()), Math.min(((Specific)RectVec.elementAt(i)).gY2(),((Specific)RectVec.elementAt(i)).gY1()), Math.abs(((Specific)RectVec.elementAt(i)).gX1() - ((Specific)RectVec.elementAt(i)).gX2()), Math.abs(((Specific)RectVec.elementAt(i)).gY1() - ((Specific)RectVec.elementAt(i)).gY2()) );
         }   
         for( int i=0; i<LineVec.size(); i++ )
         {
            g.setColor( ((Specific)LineVec.elementAt(i)).gCol() );
            g.drawLine( ((Specific)LineVec.elementAt(i)).gX1(), ((Specific)LineVec.elementAt(i)).gY1(), ((Specific)LineVec.elementAt(i)).gX2(), ((Specific)LineVec.elementAt(i)).gY2());
         }     
         for( int i=0; i<freeVec.size(); i++ )
         {
            g.setColor( ((Specific)freeVec.elementAt(i)).gCol() );
            g.drawLine( ((Specific)freeVec.elementAt(i)).gX1(), ((Specific)freeVec.elementAt(i)).gY1(), ((Specific)freeVec.elementAt(i)).gX2(), ((Specific)freeVec.elementAt(i)).gY2());
         }

         g.setColor( color );

         if( mode == 1 )    
             g.fillRect( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
        
         if( mode == 2 )      
             g.fillOval( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
         
         if( mode == 3 )           
            g.drawRect( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));
            
         if( mode == 4 )
            g.drawOval( Math.min(X1,X2), Math.min(Y1,Y2), Math.abs(X1-X2),Math.abs(Y1-Y2));   
 
         if( mode == 5 )
            g.drawLine( X1, Y1, X2, Y2 ); 
      
         if( mode == 6 )
            g.drawLine( LX1, LY1, LX2, LY2 );
            
         repaint();
         
      }

   }
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 6th, 2005, 6:38 PM   #9
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4 Dameon is on a distinguished road
Do you add new items to the beginning or end of the arrays?
Dameon is offline   Reply With Quote
Old May 7th, 2005, 5:56 AM   #10
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
at the beginning
__________________
countdown++;
HeX is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC