-1

I have a desktop application in swing. I have a JPanel in which the image as the background and in it two buttons and a JScrollPane as shown in the picture Frame with JPanel. I have a function (showLabel()) which, when JScrollPane the end, add JLabel with transparent images and disappear a few seconds. The problem is that when you add JLabel. JLabel bad shows as seen in Fig Bad shows. Can you help me with my problem?

 public class MainWindow {

 private JFrame frame;
 private PanelPopis panelPopis = new PanelPopis(this);
 private MyPaint myPaint;

 public MainWindow {

    setWindow():
    BufferedImage image1 = ImageIO.read(getClass().getClassLoader().getResource("poz.png"));

    this.myPaint = new MyPaint(image1);
    this.frame.add(myPaint);

    this.myPaint.add(panelPopis.setPanel());

}


 private void setWindow() {
    this.frame = new JFrame("DD");
    this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.frame.setSize(400, 680);
    this.frame.setResizable(false);
    this.frame.setLocationRelativeTo(null);
 }

 private void showLabel(){

   JLabel label = new JLabel();

   label.setIcon(new ImageIcon(new ImageIcon(getClass().getClassLoader().getResource("postEn.png")).getImage().getScaledInstance(395, 653, Image.SCALE_DEFAULT)));
   label.setBackground(new Color(0, 0, 0, 10));
   label.setOpaque(true);

   this.frame.invalidate();
   this.frame.add(label);
   this.frame.revalidate();

    int delay2 = 3000; // milliseconds
    ActionListener taskPerformer2 = new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            frame.remove(label);
            frame.revalidate();
            frame.repaint();
        }

    };
    Timer myTimer2 = new Timer(delay2, taskPerformer2);
    myTimer2.setRepeats(false);
    myTimer2.start();

}

}

public class MyPaint extends JPanel {

private static final long serialVersionUID = 1L;
BufferedImage image;

public MyPaint(BufferedImage image) {
    setOpaque(false);
}


public void paintComponent(Graphics g) {
    super.paintComponent(g);
        g.drawImage(image, 0, 0, 395, 653, this);

}

}

public class PanelPopis extends JPanel {

private static final long serialVersionUID = 7676683627217636485L;

private JButton setLanguage;
private JButton cont;
private JScrollPane scrolPanel;
private JTextArea popis;
private MainWindow mainWindow;

public PanelPopis(MainWindow mainWindow) {
    this.mainWindow = mainWindow;

}

public JPanel setPanel() {

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setOpaque(false);

    JPanel panel2 = new JPanel();
    Border border = panel2.getBorder();
    Border margin = new EmptyBorder(0, 0, 4, 0);
    panel2.setBorder(new CompoundBorder(border, margin));

    panel2.setOpaque(false);
    panel2.add(this.scrolPanel = new JScrollPane(popis));

    panel.add(this.setLanguage = new JButton("language settings"),   BorderLayout.NORTH);
    panel.add(this.cont = new JButton("CONTINUE"), BorderLayout.SOUTH);
    panel.add(panel2, BorderLayout.CENTER);

    return panel;

}

}

  • 2
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jul 19 '16 at 17:50
  • you have some other things in MyPaint - a simple photo on top of photo wouldnt be a serious thing - show the full code – gpasch Jul 19 '16 at 18:02
  • @gpasch *"show the full code"* Disagree. An MCVE would be the ***most*** optimal form of code to present. Note that `[mcve]` in a comment will auto-expand to [mcve]. Branislav Pažický - please don't put code in a comment where it is largely unreadable. It should be included in the question as an edit. – Andrew Thompson Jul 19 '16 at 22:22
  • Sorry, I changed it. – Branislav Pažický Jul 20 '16 at 12:49
  • 1) Tip: Add @gpasch (or whoever, the `@` is important) to *notify* the person of a new comment. 2) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. But also note that a) the different code sctions will 'run together' unless they have a 'no code formatted' section between them. I usually use the name of the class as a title - that does the trick. b) But to make a proper MCVE, it should only be a single copy/paste. That can be achieved here by leaving .. – Andrew Thompson Jul 20 '16 at 16:15
  • .. the class with `main(String[])` as `public` while reducing the other classes to default access (remove the `public` identifier). It would also be handy to include the import statements, and and actually check that you can copy/paste the code to be posted into a new project and compile/run it to see the problem. – Andrew Thompson Jul 20 '16 at 16:17

1 Answers1

0

I would suggest to use the getResource() method instead of the getResourceAsStream() and have the path of both images inputted there this way.

The classLoader could behave differently (in your case due to the differences between the two OS's) so doing it this way would guarantee that you application is always getting the correct resources.

More on the getResource here:

https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

akortex
  • 5,067
  • 2
  • 25
  • 57