JFrame - Draw graphics over a transparent background? - java

I am trying to make an undecorated transparent JFrame, and then paint some graphics over it. If I extend JFrame, set undecorated to true, and override paint with, I can make a transparent JFrame. Like this:
public class MainFrame extends JFrame {
public static void main(String[] args) throws Exception {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainFrame frame = new MainFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MainFrame() {
setTitle("ASDF");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setUndecorated(true);
setBounds(0, 0, 200, 200);
}
public void paint(Graphics g){
g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
g.drawString("ASDF", 100, 100);
}
}
The problem is that when I try drawing something on it, I can't clear what is drawn for another repaint. Like in this example, the text retains the background that was there when it was drawn. So if I move a a window behind the frame, it looks weird, because the frame itself has the old background. I tried AlphaComposite.Clear, but that only made a black background. What can I do?

If I extend JFrame, set undecorated to true, and override paint with, I can make a transparent JFrame.
I don't think so. You just paint without caring about the background, which is quickly lost. If you want to know how to make transparent windows in java use Stackoverflow: search for [java] transparent window. This should help you creating such a window, but this is quite a complicated task: Transparent Window

Related

Java repaint() doesn't call paintComponent() for drawing image

I have read a lot of answers about this problem but I can't manage to find my error even on a simple code. Here is the problem : I'd like to draw an Image in a JLabel which is in a JPanel, but the paintComponent() method of the JLabel isn't called.
Here is the code :
The ImagePainter class should draw an image
public class ImagePainter extends JLabel{
private Image image;
public ImagePainter(){
try {
image = ImageIO.read(new File("src/testgui/image.png"));
} catch (IOException exception) {
exception.printStackTrace();
}
}
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
System.out.println("in paintComponent");
}
}
Here is a standard JFrame. I took care to add the JPanel to the contentPane
public class Display extends JFrame{
public Display(){
JPanel jp = new JPanel();
ImagePainter i = new ImagePainter();
getContentPane().add(jp);
jp.add(i);
jp.repaint();
setSize(800, 800);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
And finally the main. I instanciate Display on the EDT like everyone tell to do :
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
#Override
public void run(){
Display d = new Display();
}
});
}
}
Finally, I observed that if I directly add the ImagePainter to the contentPane, the image is drawn correctly. It's probably a stupid error but I spend literally hours trying to find it and really can't see it. Thank you in advance !
The label does not account for the preferred size of the image when the image is custom painted! The panel by default has a flow layout. A flow layout does not stretch components to fit. So that label would have size of 0 x 0 pixels. You can confirm that by adding a visible border to the label.
But given the image is displayed in a label, why not just set the image as the icon of the label?
Also, the jp.repaint() statement in the Display constructor is useless, since you have not yet set the frame visible.

Jframe goes black before displaying contents? [duplicate]

I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen.
Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
1) you have three general issues
never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)
your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater() meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
hey i had he same problem and the screen turns black every time i run the program.
in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.

Java - Screen turns black, when setting a JFrame to Fullscreen

I'm trying to draw something on a Canvas, add it to a JFrame and then set this JFrame to Fullscreen. My problem is: in fullscreenmode I only see a black screen.
Before the screen turns black I shortly can see the pink background of the canvas.
Drawing directly on a JFrame and then setting it to fullscreen works perfectly fine and I can see the testtext. I assume there is a problem with displaying the Canvas properly.
Here is my code:
public class FullscreenTest extends Canvas {
private JFrame mainFrame;
public FullscreenTest(){
this.mainFrame = new JFrame();
JPanel contentPane = (JPanel) mainFrame.getContentPane();
contentPane.add(this);
}
public void run(DisplayMode dm){
setBackground(Color.PINK);
setForeground(Color.WHITE);
setFont(new Font("Arial", Font.PLAIN, 24));
Screen s = new Screen();
s.setFullScreen(dm, this.mainFrame);
try {
Thread.sleep(5000);
} catch (InterruptedException exc) { exc.printStackTrace(); }
s.closeFullScreenWindow();
}
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
public static void main(String[] args){
DisplayMode dm = new DisplayMode(800, 600, 32, DisplayMode.REFRESH_RATE_UNKNOWN);
FullscreenTest test = new FullscreenTest();
test.run(dm);
}
}
Here is what the Screen.setFullScreen(DisplayMode dm, JFrame window) method does:
//graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment()
// .getDefaultScreenDevice();
public void setFullScreen(DisplayMode dm, JFrame window){
window.setUndecorated(true);
window.setResizable(false);
graphicsDevice.setFullScreenWindow(window);
if(dm != null && graphicsDevice.isDisplayChangeSupported()){
graphicsDevice.setDisplayMode(dm);
}
}
Does anyone have a clue why I don't see the JFrame's content in fullscreen?
1) you have three general issues
never block EDT by using Thread.sleep(5000); use Swing Timer instead, demonstrations here
(if aren't there really important reasons) don't mixing AWT with Swing the rest is here, and use JPanel instead of Canvas (for Canvas is there paint method, for JPanel is there paintComponent)
your public void paint(Graphics g){ is to the JFrame instead of Canvas and locked by Thread.sleep(5000);
2) Swing GUI rellated should be wrapped into invokeLater() meaning
public static void main(String[] args){
more in the Initial Thread
3) in linked code example you can find out demostrations how to use background thread in the Swing
I agree with mKorbel (actually, I have your code working with corrections he suggest). Just one hint to achieve more predictable results further: take control on colors in paint() method. Default color for background may vary on different systems. On my system it draws white text on light-red background. But if it will draw black text on black background, test will look like "not working".
hey i had he same problem and the screen turns black every time i run the program.
in the part of the paint method , you wrote, which i think that it is from Bucky tutorial which is amazing by the way :
public void paint(Graphics g){
g.drawString("This is some testtext", 200, 200);
}
all you have to do is to use "super"
public void paint(Graphics g){
super.paint(g);
g.drawString("This is some testtext", 200, 200);
}
I tried it myself and it is working just fine.

How to paint an invisible JFrame elsewhere?

I want to paint the contents of a JFrame onto another frame. Currently, I only get it to work if the JFrame is visible.Is there a way to paint a hidden JFrame?
Additional info:In my project I need to be able to rotate and scale windows. I do not want to write my own window-api, so I thought I might be able to just paint JFrames or similar container classes in a rotated way (which the Graphics2D-API supports perfectly well). It would be awesome to be able to use standard JFrames for that, but a custom frame extending a JFrame would also be OK..
public class JFTest extends JFrame {
private JFrame frameToPaint = null;
public static void main (String[] args) {
new JFTest ();
}
public JFTest () {
// some basic initialization
super ("Container");
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setExtendedState (JFrame.MAXIMIZED_BOTH);
add (new JPanel () {
#Override public void paintComponent (Graphics g) {
super.paintComponent (g);
// painting the child frame's contents onto this frame
if (frameToPaint != null) frameToPaint.getRootPane().paintAll (g);
}
});
setVisible (true);
// initializing some test-frame that will get painted onto the container
frameToPaint = new JFrame ("child");
frameToPaint.setSize (200, 100);
frameToPaint.add (new JLabel ("test"));
frameToPaint.addComponentListener (new ComponentAdapter() {
#Override public void componentResized (ComponentEvent e) { repaint (); }
#Override public void componentHidden (ComponentEvent e) { repaint (); }
});
// magic line. an invisible frame will not get painted! why??
frameToPaint.setVisible (true);
}
}
Hint 1: JFrame's setDefaultLookAndFeelDecorated(false)/setUndecorated(true) might be of use for a window without caption and borders;
Hint 2: as setGlassPane/setLayeredPane/setOpaque(false) might be of use for a second "layer".
I want to get the graphical contents of a frame without having to make the frame visible for the user
The Screen Image class should help. Although I think it will only work for the "content pane" of the frame and not the entire frame (with the title bar and borders) unless you use a decorated frame.
1) you have to use proper LayoutManager, not setSize() or setBounds()
2) if is there null LayoutManager used then Container returns any size after setVisible(true);
3) if is there used proper LayoutManager, then Container return its Size after call pack();, in other hands this container couldn't be visible on the screen ( meaning setVisible(true); )
4) JComponents must to returns PrefferedSize for example

JFrame image display at frame resize

I have this JFrame containing a children of JPanel wherein it displays the image which is declared in this manner.
BufferedImage image = ImageIO.read(filename);
The program displays the image properly. But the only thing is, it requires to resize the frame to display the image.
Is there a possible way to display the image once the frame appears?
You should override paintComponent(Graphics g) and draw the image therein. In this case, you should do this for the JPanel component (I think? If not, do this for the JComponent(s) you're referring to). Also, since Swing is not thread-safe, ensure these modifications are performed in the EDT.
EXAMPLE
public class Demo{
private static BufferedImage bi;
public static void main(String[] args){
try{
loadImage();
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
createAndShowGUI();
}
});
}
catch (IOException e){
// handle exception
}
}
private static void loadImage() throws IOException{
bi = ImageIO.read(new File("src/resource/braveheart.PNG"));
}
private static void createAndShowGUI(){
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel(){
#Override
protected void paintComponent(Graphics g){
Graphics g2 = g.create();
g2.drawImage(bi, 0, 0, getWidth(), getHeight(), null);
g2.dispose();
}
#Override
public Dimension getPreferredSize(){
return new Dimension(bi.getWidth(), bi.getHeight());
}
};
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
OUTPUT
It's important to keep in mind that this example ignores rendering hints, so when you maximize the JFrame, the image quality will be very poor. :)
EDIT
When answering this question, I assumed you had a basic understanding of Swing. I suppose I assumed too much. It is important to mention that all components should be added to the top-level container before it's been realized (i.e. made visible). This will ensure that everything is rendered without having to resize your frame. As others have suggested, you could have simply used a JLabel to render the image, and then added it to your JPanel. Instead, I promoted custom painting, which is perfectly acceptable, and to me, cleaner.
for dispaly Image or ImageIcon is better look for JLabel (basic stuff)

Categories