[java] Component Images

Started by
13 comments, last by joeG 24 years, 1 month ago
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
joeG
Advertisement
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.
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
joeG
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*
*oof*
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*
*oof*
I forgot about XOR, I''ll try to see if I can find it.

Thanks,
JoeG
joeG
I tried it out. It won''t help, but looks cool. Any more ideas?

JoeG
joeG
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.
-Pasi Keranen
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.
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.
joeG

This topic is closed to new replies.

Advertisement