![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
|
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. |
|
|
|
|
|
#2 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#3 |
|
Programmer
|
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();
__________________
countdown++; |
|
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
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(); |
|
|
|
|
|
#5 |
|
Programmer
|
yep. This code is ion a loop. Thanx for the switch command. I knew that before but i never use it.
__________________
countdown++; |
|
|
|
|
|
#6 |
|
Programmer
|
yep. This code is on a loop. Thanx for the switch command. I knew that before but i never use it.
__________________
countdown++; |
|
|
|
|
|
#7 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8
![]() |
Can we see the loop?
|
|
|
|
|
|
#8 |
|
Programmer
|
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++; |
|
|
|
|
|
#9 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 730
Rep Power: 4
![]() |
Do you add new items to the beginning or end of the arrays?
|
|
|
|
|
|
#10 |
|
Programmer
|
at the beginning
__________________
countdown++; |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|