Results 1 to 3 of 3
  1. #1
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

    Default Heavy and light component mixing

    HI, i've got this problem.
    My program doesnt show button on startup(but it does show it when i move my cursor over it).
    What do i have to do that it will display button on startup?





    Java Code:
    /*
    
     * PrikazOzadja.java
    
     *
    
     * Created on Sreda, 18 julij 2007, 18:29
    
     *
    
     * To change this template, choose Tools | Template Manager
    
     * and open the template in the editor.
    
     */
    
    
    
    package knjiga;
    
    
    
    /**
    
     *
    
     * @author Bojevnik 
    
     */
    
    
    
    import java.awt.*;
    
    import java.awt.event.*;
    
    import java.applet.*;
    
    import java.util.*;
    
    import java.awt.geom.*;
    
    import javax.sound.sampled.*;
    
    import java.io.*;
    
    import javax.sound.midi.*;
    
    import java.net.*;
    
    import javax.swing.*;
    
    
    
    
    
    public class PrikazOzadja extends JApplet{
    
        int width;
    
        int height;
    
        Image img;
    
        JButton test =  new JButton("TEST");
    
        /** Creates a new instance of PrikazOzadja */
    
        public PrikazOzadja() {
    
        }
    
        public void init(){
    
          setSize(500,500);  
    
          img= getImage(getFURL("D:/ozadjeTest.jpg"));
    
          setLayout(null);
    
          test.setBounds(100,100,100,100);
    
          //test.grabFocus();
    
          add(test);
    
          test.setFocusPainted(true);
    
         
    
        }
    
        public void paint(Graphics g){
    
            width = getSize().width;
    
            height = getSize().height;
    
            Graphics2D g2d = (Graphics2D) g;
    
            g2d.drawImage(img,0, 0, width, height, this);
    
        }
    
        public URL getURL(String filename){
    
            URL u = null;
    
            try{
    
                u = getClass().getResource(filename);
    
                if(u==null) throw new Exception("url je null");
    
            }
    
            catch(Exception ex){
    
                JOptionPane.showMessageDialog(this,ex.getMessage());
    
            }
    
            return u;
    
        }
    
        private URL getFURL(String filename){
    
            File im=new File(filename);
    
            URL u=null;
    
            try{
    
                u=im.toURL();
    
            }
    
            catch(Exception ex){
    
                JOptionPane.showMessageDialog(this,ex.getMessage());
    
            }
    
            return u;
    
        }
    
        
    
    }
    I HATE SMURFS!!!!!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    The paint method is a Container method and is called (by java) to draw a container and its child components. Drawing in paint when you are also adding child components will draw on top of the components. Here's another way.
    Java Code:
    //  <applet code="PO" width="300" height="300"></applet>
    import java.awt.*;
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    public class PO extends JApplet{
        JButton test =  new JButton("TEST");
    
        public PO() {}
    
        public void init(){
            setSize(500,500);  
            Image img= getImage(//getFURL("D:/ozadjeTest.jpg"));
                          getURL("images/cougar.jpg"));
            // Use MediaTracker to load image data.
            ImagePanel imagePanel = new ImagePanel(img);
            add(imagePanel, BorderLayout.CENTER);
            imagePanel.setLayout(null);
            test.setBounds(100,100,100,100);
            //test.grabFocus();
            imagePanel.add(test);
            test.setFocusPainted(true);
        }
    
        private class ImagePanel extends JPanel {
            Image img;
    
            ImagePanel(Image img) {
                this.img = img;
            }
    
            public void paintComponent(Graphics g){
                super.paintComponent(g);
                int width = getSize().width;
                int height = getSize().height;
                Graphics2D g2d = (Graphics2D) g;
                g2d.drawImage(img,0, 0, width, height, this);
            }
        }
    
        public URL getURL(String filename){
            URL u = null;
            try{
                u = getClass().getResource(filename);
                if(u==null) throw new Exception("url je null");
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this,ex.getMessage());
            }
            return u;
        }
    
        private URL getFURL(String filename){
            File im=new File(filename);
            URL u=null;
            try{
                u=im.toURL();
            }catch(Exception ex){
                JOptionPane.showMessageDialog(this,ex.getMessage());
            }
            return u;
        }
    }

  3. #3
    Bojevnik is offline Member
    Join Date
    Aug 2007
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Same component on all JTabbedPane
    By java_novice in forum AWT / Swing
    Replies: 4
    Last Post: 08-06-2007, 09:09 AM
  2. Help with custom component
    By Falcon1 in forum AWT / Swing
    Replies: 8
    Last Post: 07-21-2007, 12:39 PM
  3. PoJoe Component Libraries 1.1
    By levent in forum Java Software
    Replies: 0
    Last Post: 06-08-2007, 10:23 AM
  4. Replies: 3
    Last Post: 06-07-2007, 05:52 PM
  5. Java Chart Component
    By Jamie in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •