This site requires JavaScript, please enable it in your browser!
Greenfoot back
CooliMC
CooliMC wrote ...

2014/8/10

How to pain a half transparent Image on a Jframe

CooliMC CooliMC

2014/8/10

#
Hey i have a halftransparent image and i want to paint it in a jframe without any borders but the problem is i always see the background of the frame throught the picture not the screen behind the frame like my desktop . So here is my code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.net.*;
import java.io.*;
import java.awt.Graphics2D;
import javax.swing.*;
import java.awt.*;
public class test extends Actor
{
    public static void test1() throws Exception
    {
        BufferedImage Image = ImageIO.read(new File("C:\\Users\\Marc\\Desktop\\Bilder\\Kreis.png"));
        JFrame frame = new JFrame();
        frame.setUndecorated(true);
        frame.setOpacity(1.0f);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel(new ImageIcon(Image)));
        frame.pack();
        frame.setVisible(true);
    }
     
    public static void test2() {
        JPanel p = new JPanel();
        p.setLayout(null);
        p.setForeground(Color.blue);
        p.setBackground(Color.black);
        p.setOpaque(true);
        p.setBounds(0, 0, 100, 100);
        JFrame f = new JFrame("Two Panels");
        f.setContentPane(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setUndecorated(true);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
bothe methodes doesnt work so please help me :D
danpost danpost

2014/8/10

#
Painting an image, no matter how transparent, on an opaque image will always result in an opaque image. Unless you can change the background color of the JFrame itself to something translucent or transparent, you will not be able to make it at all transparent.
CooliMC CooliMC

2014/8/12

#
ok so when i want to paint for example a rectangleframe on a picture i must draw 4 parts of the frame around the image i want in the midd ???
danpost danpost

2014/8/12

#
CooliMC wrote...
ok so when i want to paint for example a rectangleframe on a picture i must draw 4 parts of the frame around the image i want in the midd ???
Not necessarily. You can (1) use the 'drawRect' method; (2) use 'fillRect', (or 'fill') then 'drawImage(picture...)'. For example:
1
2
3
4
GreenfootImage picture = new GreenfootImage("myPicture.jpg");
GreenfootImage framed = new GreenfootImage(picture.getWidth()+8, picture.getHeight()+8);
framed.fill(); // paints entire image black
framed.drawImage(picture, 4, 4);
'framed' is now 'picture' with a frame.
CooliMC CooliMC

2014/8/12

#
ok thx
You need to login to post a reply.