I am trying to implement Connected Component Labeling algorithm using java swing library.
First, my code reads any B/W image from computer. Then, it iterates through image pixels (x,y) and returns specified pixel color.
The new image will be drawn on a JPanel. using Graphics library.
The code to draw a pixel is: g.fillRect(x, y, width, height);
The problem is that I am willing to get background color of the JPanel that I am drawing on it in order to fully implement this algorithm.
Related
I have a quadrilateral drawn in Path2D, and I would like for there to be an image on it. More specifically, I am trying to draw an image of my choice to 4 different points on a quadrilateral. In my case, it is a parallelogram. I do not want the image to go over the paralellogram. A better way to see what I am trying to say is to see the screenshot below.
I would like the image to be transformed to fit the green area. Not clipped.
I want the image to be pinned over the green paralellogram. However. I do not want the image to go over into the blue paralellogram, or the white space foe that matter.
So far I have tried
Researching for a way to place images directly onto Path2D.Double() objects. No answer
Rotating the image to fit the paralellogram. Didnt work.
Using AffineTransform in java. Dont get it ;-;
Thanks. I am new to java so do try to be lenient?
One way is to:
create a separate BufferedImage.
Apply a transform to the new image.
Draw your image to that new image.
Use the Shape object for the green area as a clip on the main drawing area
Draw the transformed image onto the main drawing area.
It's been a while since I have done transformations. You may have to set the transformation first and then draw the image after. Transformation has to come first.
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.transform(AffineTransform.getShearInstance(1.0, 0));
g2.drawImage(image, 0, 0, this);
}
Here is a simple example of how transforms work. You will have to spend some time on figuring out what values you need to make it work or if you might need to manually create a transformation matrix yourself.
In J2ME, only a screen or a canvas can be displayed at a time. The screen can have multiple objects inside it (textfield, form, etc.) while a canvas can only hold a gamecanvas.
The question is: Is it possible to have multiple game canvases in one canvas?
I'm trying to display two at the same time, one at the top and one at the bottom.
I'd like to repaint the bottom canvas without repainting the top.
Thank you in advance! Any form of help will be appreciated! :)
No, you can only display one Canvas or GameCanvas object at a time.
But: If you're asking about having 2 Canvas objects, because you need to update 2 parts of the screen independent of each other, you can do that by using 2 Image objects.
Simply get the Graphics object of each Image with Image.getGraphics();
Then you can draw onto each image.
And finally draw both images on the canvas (or just one of them, if you only want to update one part of the screen).
Example using GameCanvas:
Image topImage = Image.createImage(width, height);
Image bottomImage = Image.createImage(width, height);
Graphics topG = topImage.getGraphics();
Graphics bottomG = bottomImage.getGraphics();
Graphics g = getGraphics(); // Get graphics of the GameCanvas
drawStuffOn-topG();
drawStuffOn-bottomG();
g.drawImage(topImage, 0, 0, g.TOP|g.LEFT);
g.drawImage(bottomImage, 0, halfScreenHeight, g.TOP|g.LEFT);
flushGraphics();
No, you use just one canvas, but repaint only the bits that have changed using
Canvas.repaint(int x, int y, int w, int h);
I have drawn any picture, used Graphics 2D. How do I get the colour of a pixel at x, y? getPixelColor don't work, because this method get pixel from screen, not applet viewer coordinates.
Draw the picture to the Graphics of a BufferedImage
Draw the image to the Graphics2D.
To get the color of any pixel, call BufferedImage.getRGB(x,y) or variants (check the docs.).
I am trying to implement the fade in/fade out animation in swing.
I am using a JPanel which does not have any components in it. It is completely drawn by paintComponent() method.
Now in one of the portion of this JPanel, I want to implement the fade in/fade-out animation. When I tried using AlphaComposite, the animation is being triggered for whole JPanel.
Can I limit this animation in a small clipped region in that panel?
Graphics2D g2d = (Graphics2D) g;
g2d.setComposite(AlphaComposite.getInstance(
AlphaComposite.XOR, alpha));
Have you tried using an Graphics object (like rectangle, circle etc..) for your fade in/out? That way it won't be triggered for the complete panel.
Good luck!
Perhaps, but that may be more difficult to achieve than what it's worth. Create a JComponent of the size you want to animate (or fade), add it to your JPanel, and have repaint() called on your smaller component during animation instead of the larger JPanel.
You can use setClip() before painting to restrict the fading area.
Suppose you want a small fading rectangle. Using Area class create 2 Shapes. Intersection of original clip and fading rect and subtraction (subtract the fading rectangle from the original clip).
Call super.paintComponent() twice with 2 different clips. For the second paint you can set your alpha filter.
We have an old (more than 10yrs old) Java Swing applicatin which draws lots of circles and connections (lines) between those circles on a JCanvas (a subclass of JComponent) based on lab data.
Because the data becames bigger and bigger, we cannot display the entire drawing now. We have put the JCavans into a JScrollPane but it is not convenience to scroll the drawing.
Can we add zoom in zoom out for it? if yes, how? I know we can zoom image but the drawing on Canvas is an image?
thanks,
EDIT:
we draw those circles and line with Graphics within paintComponent(Graphics g) method.
You could apply a scaling Transform to the Graphics2D object passed to the paintComponent method. You can learn how to use it in the Java 2D programming trail.
Without knowing anything about your application it's hard to provide useful advice (adding a code snippet or better yet a cutdown example app would be helpful to show how things are being drawn), but I'll give it a shot:
Why don't you multiply the x,y and width,height values by a scaling factor before you draw each circle/line? I assume that somewhere your canvas is using a Graphics object to draw each shape?