In Java, is it possible to use paintIcon to put an icon into a canvas, and then move that icon around using keyboard events like you would a game character?
Once you paint something on the canvas, it stays there, and what was there beneath that location is lost. To "move" the icon, you need to repaint the canvas (or at least what was originally at the location of the icon) and draw the icon at its new position.
This would work better with an applet by registering keylisteners.
and overridding the paint method
Related
I want to re-draw my screen (JForm) on button click. Due to some logic its not possible to do with repaint. Is there any other way to do so ?
When i Will click Save Pattern button. Shape should be redrawn.
If any one want to know why i cant do with paint the see this question
Painting is done by overriding one of the paint methods of a component (preferably a component that extends from JComponent and by overriding paintComponent).
One possible solution is to render output to a BufferedImage, which allows you to do offscreen rendering and which you can control and when you want to, simply use Graphics#drawImage or even JLabel to render the BufferedImage to the screen
I'm making a game and when I add my sprites to my screen, naturally, they are behind my mouse. But I want to add a custom mouse image to my screen, over the mouse.
I already have sprites for the mouse, and I know to to make the image appear wherever my mouse is. The only thing is that I already know its going to appear underneath my mouse. Any suggestions?
Set the image as a custom Cursor. See Toolkit.createCustomCursor(Image,Point,String) for turning the image into a cursor.
I am working on a small project which requires me to load images into a window and then move them around at will.
Thus far I can load images onto a JPanel simply by using a graphics object to draw them to the JPanel.
Now I'm faced with the challenge of figuring out how to differentiate between the various images I've loaded when I click on them so I can drag them around the screen.
Any ideas?
If you use a JLabel for each image, and make the JLabels subcomponents of the JPanel, it'll automatically draw the image, and you can add MouseListeners to each of them to be able to drag them around the screen.
Use the Component Mover to drag any component around the screen.
I'm building a domino game in java and I am using modified rectangle2d's to draw my tiles. To drag a tile I use mouse events to change the tiles coordinates and redraw the JPanel.
This all works great and very smooth, until I start using the frames glassPane, I use the glassPane to be able to drag a tile from one JPanel to another.
It works, but rendering is quite slow when I paint on the glassPane. I've tried to use clipping when repainting, but it makes no difference.
Does anyone have an idea?
thnx.
It seems when a glassPane is visible on your RootPaneContainer, all repaint events behind the GlassPane have their clip set to fill the entire RootPaneContainer. This may be overriding your manually specified clip rect.
I try to create a hotspot by Extends of canvas and I try to add it on a panel which is painted by images. So I must draw an icon (image) instead of clear rectangle of the screen. To do that I override the paint method to draw the icon I want to use. So far there is no problem; the hotspot works correctly and the icon is painted in the true size I want (32 x 24 pixels).
I try to add this hotspot after painting an image on the my panel in mypanel.paint(g) that override too.
The problem is that I use a car icon that has no background! (I hope you can understand me) Just the car icon must be shown on the panel that is painted with my images.
But an unwanted rectangle is created around the icon and makes a bad view.
How I can paint my icon on the panel without that background?
Please help me.
the problem solve by using JLable component, i extends JLabeles and set an icon to it, then i add a mouse listener to get mouse clicked event and then set Hand curser to it,
so the hotspot as a true HotSpot !!