Painting a rounded border onto a JScrollPane - java

I am trying to paint a rounded rectangle around a JScrollPane. For the life of me I can't figure out how to do this! No matter what I try, the border is not visible. I have figured out that it is drawing BEHIND the contents and not over them. The only thing inside the scroll pane is a JPanel with some graphics painted onto it. Does anyone know how to fix this?
Here is the code I have tried to paint the border on the scroll pane:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.black);
g2.setStroke(new BasicStroke(1));
g2.draw(new RoundRectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1, 10, 10));
}
I have also tried using paint instead of paintComponent but with no such luck!

You are actually painting outside of the components bounds, which is a big no-no, and is why you are having this problem. You should consider creating a custom Border or extending the component insets so that you have room to paint your outline

This needs nothing custom. Simply use a LineBorder(lineColor,thickness,roundedCorners)..
Creates a line border with the specified color, thickness, and corner shape.
Where..
roundedCorners - whether or not border corners should be round

Related

Java Graphics2D Image Moving

So in this block of code, a 40x40 square can move across a window by calling directional methods, and I'm trying to get a spaceship to appear instead of the square. No matter what I try, it just isn't working.
public void paintComponent (Graphics g) {
ImageIcon wallpaper = new ImageIcon("images/JGalagawallpaper.png");
image = wallpaper.getImage();
g.drawImage(image, 400, 400, null);
ImageIcon ship = new ImageIcon("images/galaga.png");
galaga = ship.getImage();
super.paintComponent(g);
Graphics2D graphic = (Graphics2D) g;
graphic.fill(new Rectangle.Double(x, y, 40, 40));
//graphic.drawImage(galaga, x, y, 40, 40);
}
My question is, how do I get that thing to appear? I already tried tinkering with graphic.drawImage, however that didn't really work out as well as I hoped. That's what the commented out code is.
g.drawImage(image, 400, 400, null);
First you draw the image.
super.paintComponent(g);
Then you invoke the above code which is used to pint the background color of the panel, thus overwriting the image. The above statement should be the first statement of the painting method.
ImageIcon wallpaper = new ImageIcon("images/JGalagawallpaper.png");
A painting method is for painting only. Don't do I/O in the method. The image should be read in the constructor of your class so that it is only read once, not every time the component is repainted.
You also need to look at the coordinates of where you paint the image. Maybe the panel is not that big?
Did you verify that the image was read properly by display its size?

How to fade out the edges of a Java2D Area object?

I have a method that creates shadows based on where things are on the screen and the final product of that method is an Area, which I then draw onto the screen and it contains all shadows on screen in the same Area. This is because the drawn shadows are a low opacity so if it is not all one thing they will overlap and the opacity will make it look weird.
The issue that I want the shadows to look like they fade out, but I have absolutely no idea how, or if that would even be possible. Is there a way to soften the edges of the Area or make them gradient fade out? I tried to do graphics2D.setPaint(new GradientPaint[a gradient effect]) but it did nothing.
Thanks in advance
EDIT: Here is a screenshot of the program making a shadow around a 'building' rectangle. The green rectangle is to show the effect of the shadow. The end result I want is instead of the shadow abruptly ending, it should fade out.
i guess you have problems setting up the proper colors for the gradient:
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.GREEN);
g.fillRect(30, 30, 50, 20);
Graphics2D g2d = (Graphics2D) g;
Polygon p = new Polygon(new int[]{10,20,50,50,40,10}, new int[]{10,10,40,50,50,20}, 6);
Area a = new Area(p);
Color b = new Color(0xFF000000, true); //using colors with transparency!!!
Color w = new Color(0x00FFFFFF, true); //using colors with transparency!!!
//try to use proper values for the x/y values - both, start and end
GradientPaint gradient = new GradientPaint(0, 0, b, 40, 40, w);
g2d.setPaint(gradient);
g2d.fill(a); // fill your area!
}
result is a gradient with alpha (Transparency) ...
Area drawn in red <==> Area drawn with Gradient
don't forget to set the alpha value 0xAARRGGBB to FF for 100% opaque (0xFF000000 is black 100%opaque) to 0 (0x00FFFFFF white, 100% transparent)

Improper formatting with fillRect

I want to make a background for a game I have created.
I'm having some issues with fillRect.
Does getHeight and getWidth have to be in a certain order or should getX/Y/Height/Width be used at all?
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Graphical loop start
Rectangle rect = new Rectangle(0,0,1440,900) ;
g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
//Graphical loop end
}
Firstly, you are using the Graphics object instead of Graphics2D. You need to set the paint colour first then use the fill method.
g2d.setPaint(Color.BLUE);
g2d.fill(new Rectangle2D.Double(0, 0, screenWidth, screenHeight));
Rectangle2D constructor arguments are: x-coordinate of top-left, y-coordinate of top-left, width, height
It is good practice to use the Graphics2D.fill() method which will accept any object that implements the Shape interface. This makes it easier to change a shape to a different one should you decide to do so.

I have painted a panel but when the program starts panel shows with delay. what should I do?

I have painted a panel but when the program starts panel shows with delay. what should I do?
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage(ResourceLoader.class.getResource("wood3.jpg"));
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
int width = getWidth();
int height = getHeight();
Graphics2D graphics = (Graphics2D) g;
graphics.setStroke(new BasicStroke(1));
graphics.drawImage(img, 0, 0, width, height, null, null);
this.updateUI();
repaint();
}
You are calling repaint() inside paintComponent(Graphics g) function: you understand that it is going to be a recursive painting-stack(request) call. Try printing a string inside your code and set your eyes on the console.
Use a Thread to read the image and let the swing run in EDT using SwingUtilities.invokeLater(Runnable). That way you won't have to await your application for the image to load.
As MadProgrammer has suggested, use Graphics.drawImage(x, y, width, height, ImageObserver) function. Try to set this as the Observer instead of null. #AndrewThompson had an example to show the usecase of ImageObserver. i have forgot the link however :P

Why gif animation doesn't animate when using it in paintComponent()?

I'm using paintComponent() to paint a gif animated image at the backgound of JPanel.
It shows up the gif but doesn't animate.
I use java 1.5 and i know that i can use label with icon.
Does any body know why and how to fix it?
private static class CirclePanel extends JPanel {
ImageIcon imageIcon = new ImageIcon(BarcodeModel.class.getResource("verify.gif"));
Point point = f.getLocation();
protected void paintComponent(Graphics g) {
Graphics gc = g.create();
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
g2d.setColor(Color.BLUE);
g2d.drawImage(imageIcon.getImage(), getWidth() / 2, getHeight() / 2, null);
g2d.drawRect(0, 0, getWidth(), getHeight());
g2d.setStroke(new BasicStroke(10f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
g2d.setFont(g.getFont().deriveFont(Font.BOLD | Font.ITALIC,15f));
g2d.drawString("Wait Please ...",getWidth()/2-imageIcon.getIconHeight()/3,getHeight()/2+imageIcon.getIconHeight()+15);
g2d.dispose();
}
This is the gif image.
Edited: just add image observer to the g2d.drawImage() method.
g2d.drawImage(imageIcon.getImage(), getWidth() / 2, getHeight() / 2, this);
The reason is that the standard Java ImageIO API only loads the first image of the gif.
How to fix? Google for a Gif Loader for Java, which loads every image of the gif. Then you have to paint the right image at the right time. An alternative way would be to have different png files representing each time one frame of the animation.
Update: Well... Actually, after doing some research, it looks like the way you did it actually loads all the frames of the animated gif. The reason for it is that the ImageIcon's method getImage() always returns the first image.
To fix it, you can try this (I'm not sure if it will work...)
Instead of using Grahpics.drawImage(), use ImageIcon.paintIcon(). Like this:
imageIcon.paintIcon(this, g2d, getWidth() / 2 - imageIcon.getIconWidth() / 2, getHeight() / 2);
Image observer will take care of it.
You should just pas the observer to the g2d.drawImage(); as below.
g2d.drawImage(imageIcon.getImage(), getWidth() / 2, getHeight() / 2, this);
In my case 'this' refer to CirclePanel which extends JPanel, it can be any thing for example if you are using gif as a icon for a button, you should use button as image observer.

Categories