0

Main Goal: Add a few ShapeIcons that I have made into a JPanel p1 so that they can be animated to move throughout the whole JPanel p1 and not disappear once they exceed the bounds of the label they are created in.

// Below is me turning a shapeIcon into a label so it can be added to Jpanel p1

final MoveableShape clock = new Clock(20,10, SHAPE_WIDTH);
final ShapeIcon clockIcon = new ShapeIcon(clock, ICON_WIDTH, ICON_HEIGHT);
final JLabel label3 = new JLabel(clockIcon);
...
p1.add(label3)

All my shapes are added fine to the JPanel but I have them animated and want them to be able to float throughout the whole panel, right now since I only know how to add them into p1 through a JLabel, once the label bounds are exceeded, the shape disappears. Maybe there is a way to add a ShapeIcon through something other than a JLabel?

Mitch Talmadge
  • 4,638
  • 3
  • 26
  • 44
user2855405
  • 495
  • 2
  • 7
  • 20
  • What's `ShapeIcons`? – MadProgrammer Oct 19 '14 at 23:46
  • similar to ImageIcon I suppose except using Graphics2D, you can create a shape using rectangles, circles etc.. and it will compose it into one shape, (I.E. creating a car using 2 circles for tires and rectangle body) – user2855405 Oct 19 '14 at 23:51
  • "Similar" isn't the same - going to need more context before it's possible to make an accurate suggestion. However, if you can apply it to a `JLabel`, then it must implement the `Icon` interface, which means there must be a `paintIcon(Component, Graphics, int, int)` method... – MadProgrammer Oct 19 '14 at 23:54
  • Ah yea, it does implement it, question: what would I put in the "Graphics" parameter of that method, I tried this and got stuck on that too. – user2855405 Oct 19 '14 at 23:57
  • 1
    Take a look at [Performing Custom Painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) – MadProgrammer Oct 19 '14 at 23:57
  • I did earlier and couldn't find an answer. – user2855405 Oct 19 '14 at 23:59
  • Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This will result in less confusion and better responses – MadProgrammer Oct 19 '14 at 23:59
  • I can't get much clearer, I add labels to my JPanel, add ShapeIcons to each label(1 per label) set them to move, they only move within the label's bounds, I need them to move through the whole JPanel, doesn't matter if they collide either. All I want to know is how to do this... – user2855405 Oct 20 '14 at 00:18
  • 1
    Without knowing how the `ShapeIcon`s work, it's impossible to make a reliable suggestion... – MadProgrammer Oct 20 '14 at 00:21
  • possible [duplicate](http://stackoverflow.com/a/26132743/2587435) – Paul Samsotha Oct 20 '14 at 01:27

2 Answers2

2

As shown here, you can render an Icon and display it in your implementation of paintComponent(). As your animation surface will not need a layout, extend JComponent. Each Icon may be an embedded-resource or a runtime- construct such as ColorIcon. Complete examples are seen in the KineticModel, cited here, and SimpleBalls, cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

once the label bounds are exceeded, the shape disappears.

You should not be playing with the labels bounds. The size of the label should be the size of the shape.

I have them animated and want them to be able to float throughout the whole panel,

Then the animation should be done by changing the location of the label on the panel.

the shape disappears

Then you need to check the location of the label in relation to the size of the panel. That is for horizontal movement the X location plus the label width cannot exceed the width of the panel.

camickr
  • 321,443
  • 19
  • 166
  • 288