Wednesday, 15 May 2013

java - How to set a background to JFrame by over-riding paintComponent? -


I have just started a Java GUI and I set up a background image in the Gefram by riding more of the Paint Component (Graphics G) method. So that I can combine baby components on the image. I have looked at other answers but the code is very complex for beginners

How can it be understood that use the following code:

  public class employee GUI Enhances JFrame {public employee GUI () {super ("employee management"); This.setVisible (true); This.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); This.setLocationRelativeTo (zero); This.setResizable (wrong); This.pack (); }}  

public class staffGUI JFrame {public staffGUI} {super ( "staff manager"); This.setContentPane (new MyContentPane ("C: //somePath//image.jpg")); This.setVisible (true); This.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); This.setLocationRelativeTo (zero); This.setResizable (wrong); This.pack (); } Private class MyContentPane JPanel {Private BufferedImage Image; Public MyContentPane (string path) {try {image = imageIO.read (new file (path)); } Hold (IOException e) {e.printStackTrace (); Image = new buffard image (100, 100, buffard image. TEP_INARG); Graphics G = image.getGraphics (); G.setColor (Color.blue); G.fillRect (0, 0, 100, 100); G.dispose (); } Public Zero Paint Component (Graphics G) {Superpact Component (G); G.drawImage (image, 0, 0, getWidth (), getHeight (), empty); }}}

The frame is always done on the content pane of the frame. There is also the content panel where components are added. MyContentPane loads a BufferedImage with a given path If it is loading, then a blue rectangular image is created as. By setting up the content pane in your example code for an example of MyContentPane , you create the full internal area of ​​the MyContentPane frame.


No comments:

Post a Comment