• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Creating a dinamic rectangle

 
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
Hi, I'm realizing a mspaint clone with Java. I wanted to create a dinamic rectangle where I can see how it looks while I'm moving the mouse around. The only problem is that I don't have any idea of how to update the triangle. I tried creating it in a mouseDragged() method but it create this kind of "rectangle".

Edit: to see the image load it in another tab.
 
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
Please supply more details.
Do you have an object representing that rectangle? How did you create it? Do you want it to change shape or size after creation?
 
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:
Are you talking about rubber-banding a rectangle with mouse drag so you can see where the rectangle will be drawn when you let go?
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:Are you talking about rubber-banding a rectangle with mouse drag so you can see where the rectangle will be drawn when you let go?


Exactly
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:Please supply more details.
Do you have an object representing that rectangle? How did you create it? Do you want it to change shape or size after creation?


You know like in mspaint, when you select the button to create the rectangle, you can actually see the "preview" of the rectangle while you move your mouse around. I get that result but as you can see at the image I get all the posizion of the rectangle.
 
Bartender
Posts: 5476
212
  • Number of slices to send:
    Optional 'thank-you' note:
Two possibilities: http://www.pmsas.pr.gov.br/wp-content/?id=coderanch-1z0-809&exam=t/770649/java/Guidance-start-project-cropping-images#3531587
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
When you wrote the paintComponent() method, did you start with super.paintComponent(g); ?
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:When you wrote the paintComponent() method, did you start with super.paintComponent(g); ?


Yes
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Piet Souris wrote:Two possibilities: http://www.pmsas.pr.gov.br/wp-content/?id=coderanch-1z0-809&exam=t/770649/java/Guidance-start-project-cropping-images#3531587


Sorry, but I don't this is the solution I was looking for, I just need to create a rectangle and using the method mouseDragged() to see it in real time.
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
As you drag the mouse, call the setX2() and setY2() methods on the rectangle object repeatedly, and then call repaint() on the panel you are drawing on. When you release the mouse, those x and y numbers should remain set.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:As you drag the mouse, call the setX2() and setY2() methods on the rectangle object repeatedly, and then call repaint() on the panel you are drawing on. When you release the mouse, those x and y numbers should remain set.


This is what I got.







Here's the draw method to create the rectangle.
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:
It looks wrong to set the end point and start point to null.
Your mouseDragged() method doesn't seem to do anything with the rectangle object.
Have you subclassed Gaphics? If so, why?
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:It looks wrong to set the end point and start point to null.
Your mouseDragged() method doesn't seem to do anything with the rectangle object.
Have you subclassed Gaphics? If so, why?


The mouseDragged method actually calls the method "tracciaRettangolo" wich is used to create the rectangle. the "grafica." refers to the class where all the drawing operation happens. I separate it because then the code would be really too long. And for the null, I didn't get any problem for that.
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

Valerio Polonio wrote:. . . . And for the null, I didn't get any problem for that.

You are lucky. It seems unnecessary. You are creating new points every time you use mouseDragged().
I hope you don't have lots more ifs in those methods. That would sound like non‑object‑oriented programming, and would create a serious maintenance problem because you would have to alter every method whenever you change the alphabet of shapes to draw.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:

Valerio Polonio wrote:. . . . And for the null, I didn't get any problem for that.


I hope you don't have lots more ifs in those methods. That would sound like non‑object‑oriented programming, and would create a serious maintenance problem because you would have to alter every method whenever you change the alphabet of shapes to draw.


I have like more than 10....the methods basically check if a radiobutton is selected, and if is selected it execute the type of drawing "style".
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:You are lucky. It seems unnecessary. You are creating new points every time you use mouseDragged().


Well, I need them if I want to update the position of the rectangle every time, but the problem is that that I need to delete the previous point and display only the current one.
 
Piet Souris
Bartender
Posts: 5476
212
  • Number of slices to send:
    Optional 'thank-you' note:
In the link that I gave there is a description of how to draw and erase a rectangle, but you said it was not what you want. Another method is

draw your image to the panel in your paintComponent, then draw the rectangle as you described it. On the next paintComponent (after another mousedragged event), the original image is drawn again, without the rectangle and again you draw the new rectangle. When you get the mousereleased event, do whatever it is what you must do
 
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

Piet Souris wrote:In the link that I gave there is a description of how to draw and erase a rectangle, but you said it was not what you want. Another method is

draw your image to the panel in your paintComponent, then draw the rectangle as you described it. On the next paintComponent (after another mousedragged event), the original image is drawn again, without the rectangle and again you draw the new rectangle. When you get the mousereleased event, do whatever it is what you must do



Or, to put it more simply (I think), just have every call to paintComponent paint the entire component with the moving rectangle in the place where it's supposed to be now.

You may respond that you think it would be faster to just erase and redraw the rectangle. The industry standard term for that is "Premature Optimization". First get a working solution (e.g. Piet's). If you find it too slow, then it's fine to try something else (if you can make it work).
 
Carey Brown
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:
An example:
 
Rancher
Posts: 3324
32
  • Number of slices to send:
    Optional 'thank-you' note:
See: Custom Painting Approaches for an example that allows you to paint multiple rectangles of different colors.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:


With this code I get the exact same result as the one in the image.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:An example:


Wait, maybe the problem is because I'm drawing on a bufferedImage and not on a Canvas?

Here it is how my paintComponent look
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

Valerio Polonio wrote:. . . Wait, maybe the problem is because I'm drawing on a bufferedImage and not on a Canvas? . . .

It is much simpler to have a Rectangle object with a paintShape(Graphics) method.Make the Rectangle object do all the painting, with location and colour and everything. You can have a List<Shape> and Shape can have subtypes like Circle or Rectangle.Don't give paintComponent() public access; keep it protected.
Use spaces for indentation rather than tabs.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Campbell Ritchie wrote:Make the Rectangle object do all the painting, with location and colour and everything. You can have a List<Shape> and Shape can have subtypes like Circle or Rectangle.


Yes, but I'm not working with a Canvas or a normal JPanel. I'm drawing on a BufferedImage. As searching on the Internet my idea is that when I'm creating the rectangle I pkace it in a temporany Canvas, once I release the mouse the all the object inside the Canvas goes inside the BufferedImage. I'm trying to finding a way to display the BufferedImaga and the Canvas at the same time.
 
Carey Brown
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:

Valerio Polonio wrote:Wait, maybe the problem is because I'm drawing on a bufferedImage and not on a Canvas?


You can implement it with or without an ImageBuffer but either way you need something like a Canvas for the final display.

If you use an ImageBuffer you'd draw your  shapes to it first and then draw the ImageBuffer on your Canvas. It is probably an optimization you don't need and it would be simpler to draw directly to the Canvas.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:

Carey Brown wrote:If you use an ImageBuffer you'd draw your  shapes to it first and then draw the ImageBuffer on your Canvas. It is probably an optimization you don't need and it would be simpler to draw directly to the Canvas.


Do you have an example how to do the other way. To see the Canvas and then add it to the BufferedReader. But see them at the same time?
 
Carey Brown
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:

Valerio Polonio wrote:

Carey Brown wrote:If you use an ImageBuffer you'd draw your  shapes to it first and then draw the ImageBuffer on your Canvas. It is probably an optimization you don't need and it would be simpler to draw directly to the Canvas.


Do you have an example how to do the other way. To see the Canvas and then add it to the BufferedReader. But see them at the same time?

You can add a BufferedImage to a Canvas but you cant add a Canvas to a BufferedImage. You can add a "Shape" to either a Canvas or a BufferedImage but you'd want to pick only one of those. Drawing a Shape to both the Canvas and the BufferedImage is unnecessary overhead that serves no purpose.
 
Rob Camick
Rancher
Posts: 3324
32
  • Number of slices to send:
    Optional 'thank-you' note:

Wait, maybe the problem is because I'm drawing on a bufferedImage and not on a Canvas?



And I showed you two approaches:

1) how to draw on a BufferedImage
2) how to draw on a JPanel

The idea was for you to choose the approach that best met your requirements.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
Sorry, but I re-read the code on the site and thank you, you answered my question.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
Just to ask in the link you gave me, on the DrawOnImage file, is there an when it call the dragged method, is there a way to change the color of the rectangle while it's being dragged?
My code looks like this.
 
Carey Brown
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:
Call setColor() prior to drawing it.  Where are you drawing it?
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
I'm drawing it in a bufferedImage.
The original code tell me to do something like this.
 
Carey Brown
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:
Please don't needlessly quote the prior post.

Is 'drawingArea' a Graphics object?

Call setColor().
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
It's an entrire class.
Here you can see the code example where I'm taking some stuff.
URL
 
Carey Brown
Saloon Keeper
Posts: 10798
86
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
What do you mean I can see the url when I click on it.
If you don't see it where's the full address: http://www.camick.com/java/source/DrawOnImage.java
 
Carey Brown
Saloon Keeper
Posts: 10798
86
  • Number of slices to send:
    Optional 'thank-you' note:
Call setColor() before you call g2d.draw().
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks it worked, but what if I have 2 colors? Like if I create it with the left button or the right button of the mouse. I can I tell him the color since the rectangle creating is inside the paintConponent.
 
Valerio Polonio
Ranch Hand
Posts: 36
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks it worked, but what if I have 2 colors? Like if I create it with the left button or the right button of the mouse. I can I tell him the color since the rectangle creating is inside the paintConponent.
Like I set up something like this.

Where I se the main color and the secondary color on a temp color that it will be used like this

The problem is that every time the mouse is moved it update every single time the color wich is uneccessary.
 
Paul Clapham
Marshal
Posts: 28262
95
  • Number of slices to send:
    Optional 'thank-you' note:

Valerio Polonio wrote:The problem is that every time the mouse is moved it update every single time the color wich is uneccessary.



When you describe that as a "problem" do you mean that the wrong output is produced, or something else which is wrong and needs to be repaired? Or do you simply mean that "setColor(red)" is called when the color is already red? The latter is not a problem in any way.
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |