-4

I am making a paint application for a project and running into an issue.

My model stores two ArrayLists of circle and square objects. (Every time one is drawn, it is stored)

I also have a JSlider on my GUI to change the thickness of the stroke using g2d.setStroke(new BasicStroke(sliderValue));

Now, the problem is that, when the thickness is changed, and a new shape is drawn, the thickness of any existing shapes on the canvas gets changed as well.

How can I personalize a stroke thickness to a shape and make sure that it doesn't change after it's drawn?

Sorry, if it's kind of vague. I would like to refrain from posting code online since it's an assignment.

Thank you!

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

1

My model stores two ArrayLists of circle and square objects.

You don't need two ArrayLists.

You only need one then you can store a Shape object, which be a Circle, Square or any other Shape.

Check out Playing With Shapes for more general information on this concept including a suggestion for the basic painting code.

when the thickness is changed, and a new shape is drawn, the thickness of any existing shapes on the canvas gets changed as well.

This implies that you are repainting the Shapes from your ArrayLists every time the component is repainted.

Therefore in your ArrayList you need to store a Custom object with two pieces of information: 1) the Shape to paint and 2) the Stroke used to paint the Shape. Then as you iterate through the ArrayList you reset the Stroke for each Shape you paint.

Check out the Draw On Component example from Custom Painting Approaches for an example of this approach.

Or the Draw On Image approach shows how to paint to a BufferedImage so you doen't even need the ArrayLists to track the shapes painted.

The approach you use depends on your exact requirement.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

How can I personalize a stroke thickness to a shape and make sure that it doesn't change after it's drawn?

You could store the old Stroke before changing it, change the Stroke for the Graphics object before drawing the newest item, and then revert the Stroke back to the original Stroke.

Or you could create a copy of the Graphics object, change its Stroke, and draw the new item with it, disposing it after you're done with it.

Or you could draw the old items, the ones whose Stroke shouldn't change to a BufferedImage that is displayed by the same Graphics object.

Sorry, if it's kind of vague. I would like to refrain from posting code online since it's an assignment.

No problem -- but if you want more specific code help though, you'll want to show pertinent code, preferably your Minimal, Complete, and Verifiable Example Program (please check the link).

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373