Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2007, 04:26 PM
Member
 
Join Date: Aug 2007
Posts: 13
Bojevnik is on a distinguished road
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?





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!!!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-12-2007, 09:13 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,104
hardwired is on a distinguished road
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.
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; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-13-2007, 12:23 PM
Member
 
Join Date: Aug 2007
Posts: 13
Bojevnik is on a distinguished road
Thank you, it works.
__________________
I HATE SMURFS!!!!!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Same component on all JTabbedPane java_novice AWT / Swing 4 08-06-2007 10:09 AM
Help with custom component Falcon1 AWT / Swing 8 07-21-2007 01:39 PM
PoJoe Component Libraries 1.1 levent Java Announcements 0 06-08-2007 11:23 AM
Is it possible to disable drop in a component? Ada AWT / Swing 3 06-07-2007 06:52 PM
Java Chart Component Jamie AWT / Swing 1 05-31-2007 07:07 PM


All times are GMT +3. The time now is 04:48 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org