Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, there is a lot of problems in your code...</p> <p><strong>First of all</strong> - look @ your MainScreen class - "MainScreen extends JFrame" - you don't use it as main frame. You create new separate JFrame instead inside its constructor:</p> <pre><code>public MainScreen() { JFrame frame = new JFrame(); frame.setTitle("Project");//Title of the screen frame.setSize(800,600);//Size of the window frame.setResizable(false);//Is the window resizable? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//Exit the frame when the default red cross is clicked frame.setVisible(true);//is the frame visible? frame.getContentPane().add(cardContainer, BorderLayout.CENTER);//add the cardcontainer to flip panels frame.setLocationRelativeTo(null); } </code></pre> <p>Do this instead:</p> <pre><code>public MainScreen () { super(); setTitle ( "Project" );//Title of the screen setSize ( 800, 600 );//Size of the window setResizable ( false );//Is the window resizable? setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );//Exit the frame when the default red cross is clicked setVisible ( true );//is the frame visible? getContentPane () .add ( cardContainer, BorderLayout.CENTER );//add the cardcontainer to flip panels setLocationRelativeTo ( null ); repaint ( ); } </code></pre> <p>And your "paint()" method (not paintComponent()) will work if you override it.</p> <p><strong>Next problem</strong> - Never ever use repaint() method inside of paint* methods... never! It might cause an interface deadlock/stuck. If you need to call repaint - do it outside of the paint methods.</p> <p>I was talking about that part of code:</p> <pre><code>//Here the i try to paint the image. No error messages show. public void paintComponent(Graphics g){//&lt;-------------HERE g.drawImage(background, 0, 0, null);//&lt;-------------HERE repaint();//&lt;-------------HERE } </code></pre> <p>Just paint whatever you need inside paint methods, not more. All repaint logic should be moved outside of them. Just think about it - you are calling repaint method from paint - that will lead to paint again and than to repaint again e.t.c.</p> <p><strong>One more</strong> - your "paintComponent" method won't work on JFrame since JFrame does NOT extend JComponent. So this method has no use at all. There is a paint method, but you shouldn't use it either. To set a background image - use some frame-wide panel and paint background on it. After that you can put any content over that panel (look at the example at the end).</p> <p><strong>And more</strong> - you have debugPanel with BLACK background over cardContainer - it will hide anything you draw on the container or the frame (i will say that once more - do not paint on the frame itself, thats a bad habbit). So you have to disable any backgrounds in the components that lies over the background by setting opaque (setOpaque) to false. You can set cardContainer background to BLACK instead and paint image over it.</p> <p><strong>And last one</strong> - be sure that "data/images/title.png" path is relative to your application working directory. I always use another way of loading images:</p> <pre><code>Toolkit.getDefaultToolkit () .createImage ( MainScreen.class.getResource ( "some/path/image.png" ) ); </code></pre> <p>This code will load image placed relative to class location inside packages. This way you can have any image inside the final application jar without any problems. But thats just an advice.</p> <p><strong>And the final code that paints background inside frame:</strong></p> <pre><code>public class MainScreen extends JFrame { public JPanel comboPanel; public JComboBox cardCombo; public CardLayout cardLayout; public JPanel cardContainer; public static ImageIcon background = new ImageIcon ( MainScreen.class.getResource ( "icons/title.png" ) ); public MainScreen () { super (); setTitle ( "Project" ); comboPanel = new JPanel (); cardCombo = new JComboBox (); cardLayout = new CardLayout (); cardContainer = new BackgroundPanel ( cardLayout ); cardContainer.setOpaque ( true ); cardContainer.setBackground ( Color.BLACK ); getContentPane ().add ( cardContainer, BorderLayout.CENTER ); initializeGUI (); setSize ( 800, 600 ); setResizable ( false ); setLocationRelativeTo ( null ); setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE ); setVisible ( true ); } public void initializeGUI () { JPanel debugPanel = new JPanel ( new BorderLayout () ); debugPanel.setOpaque ( false ); String debug = "Debug Panel"; cardContainer.add ( debugPanel, debug ); cardCombo.addItem ( debug ); JPanel titlePanel = new JPanel ( new BorderLayout () ); titlePanel.setOpaque ( false ); String title = "Title Panel"; cardContainer.add ( titlePanel, title ); cardCombo.addItem ( title ); cardLayout.show ( cardContainer, title ); } private class BackgroundPanel extends JPanel { public BackgroundPanel ( LayoutManager layout ) { super ( layout ); } protected void paintComponent ( Graphics g ) { super.paintComponent ( g ); g.drawImage ( background.getImage (), 0, 0, BackgroundPanel.this ); } } public static void main ( String[] args ) { new MainScreen (); } } </code></pre> <p>I have also used ImageIcon instead Image to ensure that image is loaded before we show the frame. Otherwise you have to manually check image load state and update the background when it is loaded.</p> <p>P.S. Do not overload your code with comments - they are not needed if the code is clean enough :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload