java - JPanel: What is the simplest way to display an image? -
i working on project , need display image have drawn. need background image , several foreground images can move around. easiest way this? haven't been able find clear answer.
imageicon image = new imageicon("imagename.jpg"); jlabel label = new jlabel("", image, jlabel.center); jpanel panel = new jpanel(new borderlayout()); panel.add( label, borderlayout.center ); return panel;
the answer depends. want resize background image meet requirements of client or not? if so, want "fill" or "fit" image area.
take @ java: maintaining aspect ratio of jpanel background image lengthier discussion on topic.
the next question need ask, want paint animation on surface or use existing components , move them instead...
you check out...
- swing animation running extremely slow
- https://stackoverflow.com/questions/15858623/how-to-use-a-swing-timer-to-animate/15859932#15859932
- drawing 2 balls move in different direction on java 1 disappeared
- java bouncing ball
- multiple bouncing balls thread issue
- i trying make ball gradually move
- the images not loading
which uses paintcomponent or direct paint method.
this approach relatively common , easy control. problem if want perform sub animation (ie animate element separately main animation...think walking or spinning), becomes more difficult.
check out
which uses components instead. method if want provide sub animation, has complexity of requiring size , position components within container.
just side note. jlabel container ;)
Comments
Post a Comment