JPanel on JWindow is not transparent if `set setOpaque(false);` - why? - java

How to make JPanel to be transparent in this example? The gradient background is not visible:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class PaintJPanelOnJWindow extends JWindow {
public PaintJPanelOnJWindow() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(350, 120));
panel.setMinimumSize(new Dimension(350, 120));
panel.setMaximumSize(new Dimension(350, 120));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(false);
JLabel someText = new JLabel("I'm not transparent and my JPanel too :(");
someText.setOpaque(false);
panel.add(someText);
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
#Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
try {
int w = getWidth(), h = getHeight();
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
g2d.fillRect(0, 0, w, h);
} finally {
g2d.dispose();
}
super.paint(g);
}
}

The immediate problem is that
super.paint(g);
is being called after the custom painting code in the paint method which will cause any previous painting to be lost. Calling panel.setOpaque(false) has no effect what is done in the paint method. Calling setOpaque for any of the components in the question is unnecessary - by default the backgrounds are displayed when custom painting is correctly implemented.
This should be done by overriding the paintComponent method. This means creating a new JPanel and placing the custom painting functionality there rather than in a top level container such as a JWindow.
Example:
public class PaintJPanelApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Gradient App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setLocationRelativeTo(null);
JLabel someText = new JLabel("I AM transparent and my JPanel too :)");
GradientPanel gradientPanel = new GradientPanel();
gradientPanel.add(someText);
frame.add(gradientPanel);
frame.pack();
frame.setVisible(true);
}
});
}
static class GradientPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
g2d.fillRect(0, 0, w, h);
}
}
}

From JavaDocs:
create(): Creates a new Graphics object that is a copy of this Graphics object.
Which means that the Graphics object g2d is not referring to the JWindow Graphics object, it is referring to a copy of the JWindow Graphics object.
You need to change
Graphics2D g2d = (Graphics2D) g.create(); //creates a copy, wrong object
To
Graphics2D g2d = (Graphics2D) g; //refers to the right Graphics object
UPDATE
However, this is not the right way to do it. You should override JPanel's paintComponent method instead of breaking the window's paint process. First, remove the paint() method override from your class. Then, initialize the JPanel as follows:
JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int w = getWidth(), h = getHeight();
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
g2d.fillRect(0, 0, w, h);
}
};

Related

How to set a gradient background to the form. Intellij

I am a beginner on the Intellij Idea GUI Form. And I don't know how to set a gradient background to the form. Can someone help me? Thanks.
My layout
This is my current layout. Instead of blue, I would like to use a gradient background, how can I do that?
Make the background of JFrame transparent first.
frame.setBackground(new Color(0,0,0,0));
Create a gradient paint, and fill the panel.
JPanel panel = new javax.swing.JPanel() {
protected void paintComponent(Graphics g) {
Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), getWidth(), getHeight(), new Color(R, G, B, 255), true);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
}
}
Assign the panel as a content pane to the frame.
frame.setContentPane(panel);
Example
See the example below of a window with gradient-based translucency.
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.Paint;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;
public class Tester {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createWindow();
}
});
}
private static void createWindow() {
JFrame frame = new JFrame("Translucent Window");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
createUI(frame);
frame.setVisible(true);
}
private static void createUI(JFrame frame){
frame.setLayout(new GridBagLayout());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setBackground(new Color(0,0,0,0));
JPanel panel = new javax.swing.JPanel() {
protected void paintComponent(Graphics g) {
if (g instanceof Graphics2D) {
final int R = 100;
final int G = 100;
final int B = 100;
Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0),
getWidth(), getHeight(), new Color(R, G, B, 255), true);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(p);
g2d.fillRect(0, 0, getWidth(), getHeight());
} else {
super.paintComponent(g);
}
}
};
panel.setLayout(new GridBagLayout());
panel.add(new JButton("Hello World"));
frame.setContentPane(panel);
}
}

Drawing a simple circle with Java swing does not work

I think I am missing something really obvious but somehow this code does give me an empty window but it does not paint the red oval. What am I missing?
public class Test extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g = this.getGraphics();
Graphics2D g2 = (Graphics2D) g;
// Anti-aliasing
g2.setColor(new Color(255, 0, 0));
g2.fillOval(0, 0, 20, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Ball");
Test panel = new Test();
frame.getContentPane().add(panel);
frame.setPreferredSize(new Dimension(250, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
the paintComponent is not correct, remove this g = this.getGraphics();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
Ellipse2D.Double circle = new Ellipse2D.Double(xR, yR, diameter, diameter);
g2d.fill(circle);
...
}

How to make the background gradient of a JPanel

I want to know how to make background gradient which is in another JPanel. Many articles found in internet,but all of them had demostrated how to overide the paintComponent() of the JPanel not how to do for a jPanel which is inside it.
I use Netbeans IDE. I created a new JPanel class and could overide its paintComponent(). I have another jpanel on it (dragged & dropped on to the parent JPanel). I want to make its background gradient.
Here is how I tried for parent. It worked. How can I overide this for child jpanel ?
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Color color1 = getBackground();
Color color2 = color1.darker();
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(
0, 0, color1,
0, h, color2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
If you are careful to invoke super.paintComponent(g), you can add the gradient directly to the panel as shown below.
For usability, I would resist the temptation to try making the individual components transparent. Note also that opacity is controlled by the Look & Feel.
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* #see http://stackoverflow.com/q/12220853/230513
*/
public class GradientPanel extends JPanel {
private static final int N = 32;
public GradientPanel() {
this.setBorder(BorderFactory.createEmptyBorder(N, N, N, N));
this.add(new JLabel("Test:", JLabel.CENTER));
this.add(new JTextField("This is a test."));
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
Color color1 = getBackground();
Color color2 = color1.darker();
int w = getWidth();
int h = getHeight();
GradientPaint gp = new GradientPaint(
0, 0, color1, 0, h, color2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
private void display() {
JFrame f = new JFrame("GradientPanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new GradientPanel().display();
}
});
}
}
I think this is what you were trying to do
jPanel1 = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
int w = getWidth();
int h = getHeight();
Color color1 = new Color(81,80,106);
Color color2 = new Color(165,164,241);
GradientPaint gp = new GradientPaint(0, 0, color1, 0, h, color2);
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
}
};

jpanel overlap other components

I have serious problem when I add JPanel to Oracle Forms Container (based-on AWT Container).
I add JPanel first, after that I add some VTextFields (Oracle Forms text field).
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import oracle.ewt.scrolling.scrollBox.ScrollBox;
import oracle.forms.ui.DrawnPanel;
import oracle.forms.ui.FScrollBox;
import oracle.forms.ui.VTextField;
public class OverlapTest {
int w = 800;
int h= 700;
public OverlapTest() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(w, h);
DrawnPanel main = new DrawnPanel();
main.setLayout(null);
main.setBounds(0, 0, w, h);
main.setVisible(true);
VTextField t1 = new VTextField();
t1.setBounds(100, 100, 130, 22);
VTextField t2 = new VTextField();
t2.setBounds(100, 150, 130, 22);
VTextField t3 = new VTextField();
t3.setBounds(100, 200, 130, 22);
final JPanel draw = new JPanel(){
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 100, 130, 200);
g2.dispose();
}
#Override
public void update(Graphics g) {
paint(g);
}
};
draw.setOpaque(false);
draw.setBounds(0, 0, w, h);
main.add(draw);
main.add(t1);
main.add(t2);
main.add(t3);
ScrollBox sBox = new ScrollBox(main);
sBox.setBounds(0, 0, w, h);
sBox.setVScrollInsets(1, 100);
oracle.forms.ui.FScrollBox fBox = new FScrollBox(sBox, 0, 0);
fBox.setVisible(true);
fBox.setBounds(0, 0, w - 50, h - 50);
main.setComponentZOrder(draw, 3);
main.setComponentZOrder(t1, 0);
main.setComponentZOrder(t2, 1);
main.setComponentZOrder(t3, 2);
f.add(fBox);
f.setVisible(true);
}
public static void main(String[] args) {
OverlapTest test = new OverlapTest();
}
}
As you see, I add the JPanel first, after that I setComponentZorder, that is because my requirement and in my real application I cannot add JPanel at the end of all component, so I did it follow that way.
But currently I have one problem, all other component overlap the JPanel but the background of JPanel (the red rectangle) always hide other components like that.
This is the image:
I don't know, but when I use JTextField, everything is OK.
I'm using components of Oracle Forms in frmall.jar
You can download frmall.jar from http://www.megafileupload.com/en/file/329640/frmall-jar.html
Do you have any solution to make the red rectangle is overlapped by other components?
Sorry for my terible English.
Thanks in Advance.
now I try to replace Jpanel by Forms DrawnPanel, but it still have the problem:
FormCanvas form = new FormCanvas(){
#Override
public void paint(Graphics g, Dimension paramDimension,
Rectangle paramRectangle) {
// TODO Auto-generated method stub
super.paint(g, paramDimension, paramRectangle);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
g2.fillRect(0, 100, 200, 200);
g2.dispose();
}
};
DrawnPanel draw = new DrawnPanel();
draw.setPainter(form);
draw.setVisible(true);
So now, every my components are Forms Component, but I still get this problem
The first advice I would offer is don't mix Swing & AWT components. Try updating the 'Oracle forms' jar to a Swing version, or failing that, use only AWT components in your code.

How do I GradientPaint on multiple jPanels?

I have a view object that is a jPanel and holds other jPanels which in turn hold jLabels. I'm wanting to paint a gradient overlay on the object to give it a nice sleek look rather than the boring plain look.
My attempt thus far is:
public class InfoDisplay extends javax.swing.JPanel {
#Override
public void paintComponent(Graphics g) {
UIDefaults uid = UIManager.getDefaults();
Graphics2D g2d = (Graphics2D)g;
int w = getWidth();
int h = getHeight();
Color lightBlue = new Color(41, 117, 200);
Color darkBlue = new Color(2, 47, 106);
if (!isOpaque()) {
super.paintComponent( g );
return;
}
GradientPaint gp = new GradientPaint(0, 0, lightBlue, 0, h, darkBlue );
g2d.setPaint(gp);
g2d.fillRect( 0, 0, w, h );
setOpaque( false );
super.paintComponent( g );
setOpaque( true );
}
}
This doesn't seem to change the objects background at all. I'm fairly new to messing with things that aren't related to the Gui defaults.
I used the Gui builder in Netbeans to create the object, so initComponents() is also in the class, but I posted only the source that is relevant to the question.
Perhaps someone can point me in the right direction?
If you want a background JPanel to use a gradient paint, then just use it. Don't do all that funny stuff in your code with setOpaque and super.paintComponent. e.g.,
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.*;
#SuppressWarnings("serial")
public class GradientPaintPanel extends JPanel {
private static final Color LIGHT_BLUE = new Color(41, 117, 200);
private static final Color DARK_BLUE = new Color(2, 47, 106);
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
GradientPaint gradPaint = new GradientPaint(0, 0, LIGHT_BLUE, 0, getHeight(), DARK_BLUE);
g2.setPaint(gradPaint);
g2.fillRect(0, 0, getWidth(), getHeight());
}
public GradientPaintPanel() {
}
private static void createAndShowUI() {
GradientPaintPanel gradPaintPanel = new GradientPaintPanel();
gradPaintPanel.setPreferredSize(new Dimension(400, 300));
JFrame frame = new JFrame("GradientPaintEg");
frame.getContentPane().add(gradPaintPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}

Categories

Resources