painting on glassPane is slow - java

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.

Related

Java2D using buttons and Graphics2D

I want to create a tile editor sort of program where I have a viewport for rendering on the left side of the program, and then a panel of buttons on the right to use for opening files, saving the tiles etc... I was going to use LWJGL to do this, but it seems theres no good way to do it.
Essentially I guess I'm asking, how do I have a viewport for rendering using g.DrawImage or something, and then also have a panel of buttons next to that viewport?
You can do this using jME3 as they support rendering in a canvas on a Swing window. There is even a tutorial on doing it in the tutorials section of the website.
You can also do it using a fully rendered window and then putting the buttons inside the scene. I'm surprised LWJGL doesn't support this though as jME3 is built on top of LWJGL.

How to solve graphical glitching with JDialog, JPanel?

So the intial setup for this issue is that there's a JDialog, and inside that I've placed a JPanel that would house the rest of the components (since painting the JDialog itself is apparently a bad idea). This JPanel has an overriden paintComponent(Graphics g) method that only paints the background and adds a faint border for aesthetic purposes.
Now inside that is a series of JPanels that categorize the contained form components, and each JPanel has an overridden paintComponent(Graphics g) as well, painting a semi-transparent background.
Inside each of those JPanels is where I start to have some issues, presumably with transparency. I have JTextFields, JCheckBoxes, JLabels, JSliders, etc inside these panels, and when you interact with one (hover, click, etc), the background goes from transparent to opaque, with an occassional ghosted image from another field (which appears slightly random sometimes). I'm using a custom LAF called Web, but I tested with other built-in LAFs and the same thing happens.
Is this a glitch with Java or did I mess something up? If so, how can I patch this up? I can paste code fragments later if necessary, but I've used several custom classes and nine-patch style image stitching which may make the code fragments hard to follow. Thanks in advance!
If you are painting components with a transparent background it is very important that the component is marked as transparent (setOpaue(false)) so that the repaint manager knows that it must paint the components below it.
It is also very important that when you are performing custom painting that you call super.paintComponent first.
This is especially important in the case of transparent components, as this prepares the Graphics context for painting.
Graphics is a shared resource. All the components painted in your window will share the same Graphics object, meaning that if you don't allow paintComponent to first prepare it, then you will see what was previously painted on it.

Setting Java 2D drawing start after TitleBar

I have stepped in working with Java 2D and initially i have been stucked how to shift my drawing location after Title Bar.
If i start drawing x,y (0,0). my Shapes are that is under Title bar hides.
Wha tis the best way to set the Drawing Location after Title Bar.
It sounds like you've overridden paint on a top level container, like JFrame? This is your problem. A JFrame's decoration is actually painted inside the window.
You should create a custom component, say from JPanel for example, override it's paintComponent method and perform your custom painting there.
You can then add this to your frame or replace the frames content pane.
Take a closer look at Performing Custom Painting
You can to a look at
How to get the EXACT middle of a screen, even when re-sized
Graphics rendering in title bar
How can I set in the midst?
For more examples...

Java Layer JPanels

I need to layer JPanels but I don't know how. I have 3 panels, the first is for the background, the second is for a Character/Sprite to move around and layers the first panel(background, and the third is a bar off to the side (Used for buttons and has nothing to do with they layers). How do I layer panel 1 and panel 2?
edit: The background is made up of a grid of 25x25 labels with an icon in each.
Some options:
Use a JLayeredPane which can layer components using a z-order Integer constant. Remember that when doing this, you are also essentially using a null layout, and so you will be fully responsible for setting the size and position of all components added to the JLayeredPane.
If all the background is doing is painting an image, you could use a single JPanel, and then simply paint the image as a BufferedImage that is displayed in the JPanel's paintComponent method. The sprite would also be painted but its location would vary.
See How to Use Layered Panes.
Don't forget to use:
panel.setOpaque(false);
Or you don't need to layer panels. You can just paint a background image on the panel. See Background Panel for an example of this.
We just recently worked on a top-down video game for my CSC class. All we did was draw the background and then all the sprites after it in the paint() method on the JPanel. We also used a Timer and an ActionListener to constantly update the JPanel.

Select an image from JPanel

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.

Categories