2

I am fairly new to coding in general; however, am very adaptive. Here is the code. The background graphics and sound loads fine, but the JButton has a short delay before loading. Any help is appreciated!

public Window() {

        startButton = new JButton();
        initialPanel = new JPanel();
        startButtonLabel = new JLabel("Start");

        add(initialPanel, BorderLayout.SOUTH);
        initialPanel.add(startButton);
        startButton.add(startButtonLabel);


        setSize(800, 600);
        setLocationRelativeTo(null);
        setUndecorated(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

    public void paint (Graphics g) {

        ImageIcon panel = new ImageIcon ("NOWNEW2.png");
        g.drawImage(panel.getImage(), 0, 0, null);

        try {   

            File sound = new File("bensound-newdawn.wav");
            AudioInputStream ais = AudioSystem.getAudioInputStream(sound);
            Clip clip = AudioSystem.getClip();
            clip.open(ais);
            clip.start();
            }

        catch (Exception e) {

            System.out.println(e);
            }
        }

    public static void main (String [] args) {
        new Window();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Cthulhu
  • 73
  • 6
  • That might depend on various things, starting with file sizes for example, and potential scaling of that image. Have you tried pre-loading the data *before* you put up the ui? – GhostCat Dec 16 '19 at 04:11
  • No, thank you so much! You have helped me in knowing what to research. Truly grateful!! – Cthulhu Dec 16 '19 at 04:30
  • You are welcome. In case you find the exact reason for your issue, you could write a self answer then. – GhostCat Dec 16 '19 at 04:31
  • 1) For better help sooner, [edit] to add 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). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Don't paint in a top level container such as a `JFrame` or a `JWindow`. Instead add a `JPanel` and do custom painting using the `paintComponent(Graphics)` method. .. – Andrew Thompson Dec 22 '19 at 05:14
  • And on the paint method.. 4) Always call the `super` method first (to clear the BG and paint borders etc) 5) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 6) `ImageIcon panel = new ImageIcon ("NOWNEW2.png");` This is a poor name for something that is an `ImageIcon` 7) The `String` based constructor for .. – Andrew Thompson Dec 22 '19 at 05:17
  • .. an `ImageIcon` does not throw exceptions if the image cannot be found. Better to use `ImageIO` (which does). 8) `g.drawImage(panel.getImage(), 0, 0, null);` All instances of a `JComponent` (like `JPanel` mentioned above) implement `ImageObserver`. So once that is changed, it should be `g.drawImage(panel.getImage(), 0, 0, this);` 9) Don't try to load images or clips in a paint method. They should be loaded on construction and used when needed. 10) `System.out.println(e);` better would be `e.printStackTrace();` which gives more information about what went wrong. .. – Andrew Thompson Dec 22 '19 at 05:21
  • 11) `startButton = new JButton(); .. startButtonLabel = new JLabel("Start"); .. startButton.add(startButtonLabel);` This is a mess! Replace it with `startButton = new JButton("Start");`! 12) It seems you have recieved some poor information on coding. Please do the Java Tutorial for each part of the code of relevance. – Andrew Thompson Dec 22 '19 at 05:26
  • @AndrewThompson I am truly new to coding. I want to learn the better practices—which is why I chose stackoverflow—however, my ignorance will not surpass my will to learn. Thank you! – Cthulhu Dec 22 '19 at 06:26
  • Also, the label on the button was something I noticed (but was unsure other methods, or the reason why) had an effect on when the button was selected. E.g. without the label on the button provides a box around the text "Start" when the button is selected. Whereas, if the label was placed on the button the box around the text was not there when the button is selected. I am researching a lot and have not gone back to looking for an answer to that. – Cthulhu Dec 22 '19 at 06:38

0 Answers0