• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Component Images

This topic is 6543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, is there any possible way to get a graphical representation of a component''s context into an Image. What I am doing is creating a stretch rectangle (you know the kind where the user clicks their mouse and stretches a rectangle to make a selection) class. I have available to me repaint(x,y,width,height), but that has proved very laborious, as there are many different cases to cover. What is the best way to do this minus flicker. I''m kind of stuck here on this part, and it would be nice to get going on some of the cooler stuff yet to do. Thanks, JoeG

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
what are you trying to do exactly, get a picture of a component? I''ve never actually done this but i would try component.getImage() or component.getGraphics() and see what i got from those.

Share this post


Link to post
Share on other sites
Say, you had a panel (or component in my case) that the user could drag a selection rectangle on. The class responsible for the selection rectangle responds to the mouseDrag event, and for each mouseDrag event I update the rectangle. To make this look good I can't leave the previous rectangle on the component. I originally wanted the ability to grab a picture of the component in its original state, and paste it on and then draw the new rectangle (in response to the mouseDrag event like I said above). But what I then realized I could do (and at this moment appears to be my only option) was use the component's repaint(x,y,width,height) to restore the component to its original appearance and then paint the new rectangle. But then I realized that you had to account for when the delta x was negative and the delta y was positive, and all the combinations that arise from that. That solution is reallyl not an elegant one, so, in short, I'm searching for a better solution.

Here's a pic of what's going on (don't know about the use of pics here yet, but from what I've seen this one is innocent enough )



JoeG


Edited by - joeG on 2/22/00 10:36:26 AM

Share this post


Link to post
Share on other sites
You shouldn''t need to copy the background at all, if you do the selection rectangle correctly. (Assuming I understand what you''re trying to do)

A good way to do the drawing of the rectangle is to use the built-in DrawDragRect() function. It automatically erases the previous drawn rectangle before drawing a new one. You do have to keep track of the last position, but this should be easy to do. YOu also have to check for which quadrant your selection is in; you get some weird effects if you don''t. Its simple to use.. just play around with it and see if its what you want. The default behavior draws a selection rectangle seen throughout the windows interface.

Also, if you draw the lines yourself, you might want to look at using XOR operations in drawing. that way, you just draw over the last rectangle you drew with XOR, and it magically disappears. You''ll have to research this method though, haven''t done it that way in years.

good luck, and hope this was some help.



*oof*

Share this post


Link to post
Share on other sites
Argh. I just realized you''re using Java (my fault for following the discussion link in the index page and not checking to see where it is)... I''m not sure how well the previous reccomendations work there... but you might want to see if Java provides similar functionality somewhere.

sorry if I confused ya.


*oof*

Share this post


Link to post
Share on other sites
I forgot about XOR, I''ll try to see if I can find it.

Thanks,
JoeG

Share this post


Link to post
Share on other sites
I tried it out. It won''t help, but looks cool. Any more ideas?

JoeG

Share this post


Link to post
Share on other sites
This shouldn''t be too difficult. You''ll just have to store the previous rectangle (propably at the end of the mouseDragged() method) and draw it away using this XOR method. Something like this:

int beginX;
int beginY;
int endX;
int endY;
boolean fIsSelectingArea = false;

public void mouseDragged(MouseEvent event)
{
if ((beginX != endX) && (beginY != endY))
{
paintSelectionRect(beginX, beginY, endX, endY);
}

endX = event.getX();
endY = event.getY();

// Paint the selection
paintSelectionRect(beginX, beginY, endX, endY);
}

public void paintSelectionRect
(int left, int top, int right, int bottom)
{
// Order the coordinates
if (right < left)
{
int temp = left;
left = right;
right = temp;
}

if (bottom < top)
{
int temp = top;
top = bottom;
bottom = temp;
}

Graphics g = getGraphics();
g.setColor(Color.green);
g.setXORMode(Color.red);
g.drawRect(left, top, right-left, bottom-top);
g.setPaintMode();
}


And that''s it. I''ve used this code in one bitmap editor I made and it worked beautifully.

Share this post


Link to post
Share on other sites
I see what you''re saying, you can''t just keep painting rectangles because there would be a ton of rectangles. Here''s something i thought of immediatley but have never implemented myself. Glass panes. Here''s a line from the tutorial "The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components." Just catch the mouse drag events, clear out the glass pane, then paint over it. And you can set it''s visibility to false when you''re done using it. Basically glass panes are just components that are layered on top of everything.

Share this post


Link to post
Share on other sites
Jim, Have any idea on how to do that in JDK 1.1?

Javanerd, that''s what I eventually want to end up with, a selection rectangle over an image (but not for editing though ), but I don''t see any code to undo the last rectangle drawn, so I''m thinking that there wouldn''t be any change between what you see in the pic above.

Share this post


Link to post
Share on other sites
very doable under 1.1. Just make a class
MyGlassPane extends Component { 

....

paint (Graphics g) {
g.setColor(Color.OPAQUE);
g.drawRect(dimensions, of , entire, MyGlassPane);
g.setColor(Color.black);
g.drawRect(dimensions, sent, from ,dragEvent);
}
}

Now, you might have to do something fancy with the event listeners, like onmousedown, set the glasspane's visibility to true, then give the glass pane a listener for mouse drags. See I don't know if the mouse drags will still get passed to the underlying component on wich the mouse was originally clicked or not. Just include printlns in your event listener to print out the event's source.

Edited by - Jim_Ross on 2/23/00 1:36:32 PM

Share this post


Link to post
Share on other sites
quote:
Original post by joeG

Jim, Have any idea on how to do that in JDK 1.1?

Javanerd, that''s what I eventually want to end up with, a selection rectangle over an image (but not for editing though ), but I don''t see any code to undo the last rectangle drawn, so I''m thinking that there wouldn''t be any change between what you see in the pic above.


Ah, but there you are wrong my friend...

The XOR painting mode takes care of the painting AND the removing. The idea is that if you paint a rectangle first time using XOR mode it''ll alter the colors so that the rectangle is allways visible no matter what the background color is. Then when you paint the rectangle the second time using XOR mode it''ll erase whatever was painted the last time (assuming of course that you draw the exact same rectangle).

Try it!

Share this post


Link to post
Share on other sites
But, when will the rectangle ever be drawn twice over itself? As you can see in the above pic, there are rectangles all over. Trying XOR, I would get the same lines, but with weird colors. Do you have tha old app(let) with you still. I'd like to see the source to see what your talking about.

Thanks though

JoeG

And one thing more, these rectangles are not solid rectangles

Edited by - joeG on 2/25/00 8:25:54 AM

Share this post


Link to post
Share on other sites
quote:
Original post by javanerd

[quote] Original post by joeG

Jim, Have any idea on how to do that in JDK 1.1?

Javanerd, that's what I eventually want to end up with, a selection rectangle over an image (but not for editing though ), but I don't see any code to undo the last rectangle drawn, so I'm thinking that there wouldn't be any change between what you see in the pic above.


Ah, but there you are wrong my friend...

The XOR painting mode takes care of the painting AND the removing. The idea is that if you paint a rectangle first time using XOR mode it'll alter the colors so that the rectangle is allways visible no matter what the background color is. Then when you paint the rectangle the second time using XOR mode it'll erase whatever was painted the last time (assuming of course that you draw the exact same rectangle).

Try it!




Will that work even with the color black? I guess i don't fully understand the internals of a XOR color mode (model?)


Edited by - Jim_Ross on 2/25/00 1:00:23 PM

Share this post


Link to post
Share on other sites
I imagine Java, if working in palette mode, would ''dereference'' both color''s indices and then peform the bitwise XOR. False (XOR) Anything = ThatAnything. Translation: 000 000 000 (XOR) 111 000 000 (red for example) = 111 000 000. I think it looks pretty cool. But it doesn''t solve my problem either

Share this post


Link to post
Share on other sites

  • Advertisement