Editind image (JLabel) inside JScrollPane - java

I am developing an image editor capable of drawing ovals on an image. I am able to successfully add an image into a JScrollPane and draw on it using the fillOval() function. But each and every time I move the scroll bar all the drawn ovals disappear. Since the image to be uploaded is often large in size scroll bars cannot be disabled. I have incorporated the image in a JLabel. Please help.

I am able to successfully add an image into a JScrollPane and draw on it using the fillOval() function. But each and every time I move the scroll bar all the drawn ovals disappear.
This suggests to me that you're not drawing correctly. Since you've not shown us how you're drawing, we can only guess, but perhaps you're calling getGraphics() on a component and using an unstable Graphics instance. If so, you'll be better off calling getGraphics() on a BufferedImage itself, and drawing on it. Either that or drawing in the paintComponent method of your JComponent.
If this doesn't help, please provide more information on exactly what you're doing, preferably by creating and posting a Minimal, Complete, and Verifiable Example Program. We don't want to see your whole program, but rather you should condense your code into the smallest bit that still compiles, has no extra code that's not relevant to your problem, but still demonstrates your problem.

Related

How to put paint component pieces on top of a background in Java?

We have a group project where we must create a turn based, 2 player, grid game for class, i.e. Chutes and Ladders. The whole project is done except for the fact that when we bring the background image up, the players pieces get painted behind the background, and are therefore not visible. Is there a way to get the pieces to show up on top of the background image? We are using imageIO for the image, GridLayout for the Grid, and then using PaintComponent to place the pieces on the grid. When we do not have the background image, the pieces show up on the Grid flawlessly.
using PaintComponent to place the pieces on the grid
Make sure to call the super's paintComponent(...) method first thing in your paintComponent override.
Make sure that your paintComponent method has no program logic inside of it.
Sometimes it's better to place your pieces in ImageIcons and the Icons in JLabels, and then place your JLabels on a Grid of JPanels. For example.
For more help, post code and give more details. To be honest, I'm a little surprised that you haven't even posted your paintComponent method code.

Using a paintComponent with layout

How do I convert a paintComponent to something that I can manipulate with a layout in a JFrame?
So, I'm running into an issue. I haven't really been taught (and don't have access to a book) how to use layouts/GUI stuff in my courses yet.
My issue is this: I have a program that the user inputs a number. Based on this number, the program calculates a circle and draws it out with a paintComponent method that has a for loop inside of it. The "pixels" that the circle is drawn with are actually fillRect methods. The current method of getting a user-input that I am using is a JOptionPane showInputDialog. This is MOSTLY fine, but I want the user to be able to select from a set of pre-defined numbers. Somebody suggested that I use a JComboBox, but I don't know how I would convert the paintComponent to something that would be usable by a layout manager (which a JComboBox must use, as far as I've learned). I know the dimensions of the paintComponent (805px by 805px) and there is no situation where it will change. If I could get some help with this bit, I am confident that I can figure out using a layout manager myself.
Another way to paint (besides custom painting) is to paint to a BufferedImage. The image can then be displayed in a JLabel.
Examples:
Painting in a BufferedImage inside Swing A fairly complicated one.
Dynamic Graphics Object Painting Another one.
Yet another one.
You don't know the dimensions of paintComponent because it's a method, and methods don't have dimensions. You probably know the dimensions of a JPanel or a JFrame or whatever your component is.
You should separate the panel where you do the painting, and a different panel which would contain any comboboxes or other inputs you decide to put in. That way you can keep your drawing panel as is, and they won't interfere with each other. You'll want to search for the tutorial on LayoutManagers.

Java Transparent Panel Over Another Panel

I am working with Java 2D graphics and having an issue.
I have a JPanel onto which I draw some images and also moving images with a Timer into the circles.
*Initially I draw the interface in paint() method. and upon button click I read data from file and then calling a function to display those images and strings in a timed controlled function.
* The issue is, My Drawing screen showing overlapping images, If I call repaint(), the screen start flicking.
I need help with adding the basic drawing as an image in the background Panel and then runtime drawing onto another overlapped but transparent panel so if I call repaint() screen behaves normally and no flickering occurs. I am attaching screenshots of the scenario.
Could anybody suggest how to add two panel so one serves a background and other works like runtime drawing onto background image panel?
I'm hope that there is swing.JPanel not awt.Panel, then to use paintComponent instead of paint
there are four ways
add any drawString or subImage/Image inside paintComponent(), prepare those Objects as local variable, inside paintComponent() only use value from these variables or loop inside prepared arrays of Objects
add JLabels (transparent, non_opaque by defaulr) with Icons/ImageIcons with text to JLabel, required to add grid of JLabels to JPanel, and on runtime to setIcon/setText to desired JLabel(s)
put JLabels to GlassPane, with rest to see in point 2nd
put JLabels to JLayer, with rest to see in point 2nd

Drawing over a BufferedImage. repaint()

I have a component on which I'm drawing a BufferedImage on all the surface.
I would like to draw something more over it, following the mouse when it passes over the area.
To do it, I'm adding a MouseMotionListener on the component and implement mouseMove method. Inside mouseMoved method I'm calling repaint() at the end of the drawing of the drawing of the cursor image. I would like to know if there is a better way to do it, cause the image following the cursor is really small, and I'm repainting every thing each time.
Add a JLabel containing an Icon to the panel with the buffered image.
Then when you move the mouse you just change the location of the label. Swing will repaint the last location so the buffered image shows through, then it will repaint the label at the new location. So let Swing manage the repaint.
Since you know the coordinate of your mouse and the small image you gonna paint over your background, you can optimize like this [pseudo-code]:
void mouseMoved(event) {
lastCoordinates = currentCoordinates;
currentCoordinates = event.coordinates;
image.repaint(lastCoordinates.x, lastCoordinates.y, smallImage.width, smallImage.height);
image.repaint(currentCoordinates.x, currentCoordinates.y, smallImage.width, smallImage.height);
}
that way you only repaint the two regions you actually care about instead of the whole background.
Also, reading the javadoc it seems the code above my actually trigger 2 separate calls to painting stuff, which would be inefficient. You may want to try to pass in a 10 milliseconds value or so to make sure the 2 paints execute together.
Check out javadoc for repaint() that takes 4 and 5 arguments:
4-argument version
5-argument version

Java sampling pixel color in swing

I have to create a special TextFieldUI that draws an image as the background. That image contains some alpha components. However, whenever a character is written in that text field, first it redraws the background and then draws the character. This is fine when the background contains no alpha components, but after a few characters have been typed, the alpha areas sum up to become black.
The only way I can see around this is in the paintBackground method of TextfieldUI (which I'm overriding), I have to first sample the color of the background at that location, paint the entire graphics component that color, and then paint my background.
Does anyone know how to sample the color of a pixel when all I have access to is the Graphics object?
Is there a better way to draw a custom image as the textfield background other than overriding paintBackground in TextfieldUI?
Thanks
I haven't tried it before, but Swing is built on top of AWT, and the Robot class had a way of sampling specific pixels in the AWT
Well, I don't know what your custom code looks like in the paintBackground method, but I would make sure you fill in the text field background before you draw the image.
I'll let you decide if its "better" or not, but you can use the Background Panel which allows you to add an image to a panel. Then you add the text field to the panel (the text field is automatically made non-opaque so the image shows through). Then you add the panel to the GUI.
If that doesn't work then it would be nice to have a demo of your code so we can see whats actually happening.
When you override paintBackground, you're calling the superclass version first, right? It already lays down a background-color rectangle that would give your image a fresh-start.
Rather than 'sampling' the background color, it's probably already correct (the superclass paintBackground code gets it from the parent component if not locally set). If that default is not correct, set it in initial interface construction. (Your field isn't being overlaid on other complicated arbitrary interface of unknown solid colors, is it?)

Categories