Reputation:
I try to build my personal JFrame from a PNG picture. But there is different behavior between Mac OSX 10.8 and Windows 7. (I have to use JDK 6)
Here is my code :
[...]
public Fenetre()
{
this.setLocationRelativeTo(null);
this.setUndecorated(true);
this.setBackground(new Color(0,0,0,0));
try {
image = ImageIO.read(this.getClass().getResource("/Images/frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.setSize(image.getWidth(),image.getHeight());
this.setLayout(null);
panel = new JPanel();
JButton quit = new JButton("Quitter");
panel.add(quit);
Dimension size = panel.getPreferredSize();
panel.setBounds(67, 45, size.width, size.height);
this.add(panel);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setVisible(true);
}
public void paint(Graphics g) {
Graphics2D g2 =(Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); // SRC_ATOP > Windows
g2.drawImage(image, 0, 0, this);
panel.update(panel.getGraphics());
}
[...]
The result on Mac OSX 10.8 (AlphaComposite = SRC) :
http://imageshack.us/photo/my-images/15/maczr.png/
Then, on Windows 7 (AlphaComposite = SRC_ATOP), at start up and when I move it, I can see :
http://imageshack.us/photo/my-images/16/windowsqu.jpg/
How do it ?
Upvotes: 1
Views: 215
Reputation: 347204
You are not honoring the paint chain
public void paint(Graphics g) {
// You must call super.paint somewhere here...
Graphics2D g2 =(Graphics2D) g;
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); // SRC_ATOP > Windows
g2.drawImage(image, 0, 0, this);
panel.update(panel.getGraphics());
}
You should NEVER call Component#update
.
panel.update(panel.getGraphics());
This is called on you behalf by the repaint manager, which calls paint
.
paint
calls paintComponent
, paintBorder
and paintChildren
failing to honor the paint chain means that not of these methods are been called and they are very important
As camickr has pointed out, you should never need to overrride the paint
method of a top level container, like JFrame
.
Instead, create a custom component that is capable of performing you painting for you and set it as the frames content pane...
this.setLocationRelativeTo(null);
this.setUndecorated(true);
this.setBackground(new Color(0,0,0,0));
setContentPane(new FancyPaintPane());
pack();
And the FancyPaintPane
public class FancyPaintPane extends JPanel {
private BufferedImage image;
public FancyPaintPane() {
try {
image = ImageIO.read(this.getClass().getResource("/Images/frame.png"));
} catch (IOException e) {
e.printStackTrace();
}
setOpaque(false);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(image.getWidth(), image.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); //To change body of generated methods, choose Tools | Templates.
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); // SRC_ATOP > Windows
g2.drawImage(image, 0, 0, this);
g2.dispose();
}
}
You should also never modify a Graphics
context without reverting it. These are shared resources across all components within the same top level container. A better approach is to create a copy which you can manipulate and dispose of when you're done...
You are also trying to show a component with transparent elements without first marking the component as transparent. This can produce nasty paint artifacts. You should call JComponent#setOpaque
passing it a false
value.
I would also strongly discourage you from using null
layouts. They have a nasty habit of coming back and biting you.
Updated with simple example
public class CirclePaneTest {
public static void main(String[] args) {
new CirclePaneTest();
}
public CirclePaneTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
CirclePane circlePane = new CirclePane();
circlePane.setLayout(new BorderLayout());
circlePane.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
System.exit(0);
}
}
});
JLabel label = new JLabel("Look Ma, I'm a circle");
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
JFrame frame = new JFrame("Test");
frame.setUndecorated(true);
frame.setBackground(new Color(0, 0, 0, 0));
frame.setContentPane(circlePane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(label);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class CirclePane extends JPanel {
public CirclePane() {
setOpaque(false);
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected int getRadius() {
return Math.min(getWidth(), getHeight()) - 1;
}
@Override
public Insets getInsets() {
int radius = getRadius();
int xOffset = (getWidth() - radius) / 2;
int yOffset = (getHeight() - radius) / 2;
Insets insets = new Insets(
radius / 6,
radius / 6,
radius / 6,
radius / 6);
return insets;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int radius = getRadius();
int xOffset = (getWidth() - radius) / 2;
int yOffset = (getHeight() - radius) / 2;
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(getBackground());
g2d.fillOval(xOffset, yOffset, radius, radius);
g2d.setColor(Color.GRAY);
g2d.drawOval(xOffset, yOffset, radius, radius);
g2d.dispose();
}
}
}
Updated with code for Java 7
The following code should be used to set up the frame to be transparent under Java 6.
JFrame frame = new JFrame("Test");
frame.setAlwaysOnTop(true);
frame.setUndecorated(true);
try {
Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
if (awtUtilsClass != null) {
Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
method.invoke(null, frame, false);
}
} catch (Exception exp) {
}
//frame.setBackground(new Color(0, 0, 0, 0));
frame.setContentPane(circlePane);
Upvotes: 4
Reputation: 324118
Your code is incomplete, but it seems to me like you are overriding the paint() method of a JFrame. You should never do this (unless you know what you doing and you invoke super.paint(..))!
If you want to display an image in the frame then either:
a) add a JLabel with an image to the frame
b) or do custom painting on a JPanel by overriding the paintComponent() method and then add the panel to the frame.
Upvotes: 5