Results 1 to 3 of 3
  1. #1
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default Cant have these random numbers on JFrame

    I have the following code which am trying to have run random numbers onto a main class, but whenever I call that class, the frame does not show at all. Where am I going wrong? You can come up with a full reconstruct of the classes if possible. Thanks in advance

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.*;
    import javax.swing.*;
    import java.io.*;
    import java.util.Random;
    // this is for trials
    
    public class Draw10 extends JPanel {
    	String string;
    	String phrase = "";
    	Font fFont = new Font ("Serif", Font.BOLD, 58);
    
    	Draw10() {
    		super();
    			setBorder(BorderFactory.createLoweredBevelBorder());
    			  setBackground (Color.blue);
    			  setForeground(Color.blue);
    			  setPreferredSize(new Dimension(100,150));
    			  setVisible(true);
    
    //this.string = string;
    	}
    
    void display1(String string) {
    	this.string = string;
    	repaint();
    	}
    
    	public void paintComponent(Graphics comp) {
    		comp.setFont (fFont);
    		Graphics2D g2d = (Graphics2D) comp;
    		g2d.drawString(string, 10,30);
    
    			}
    
    
    void display(String string) {
    					phrase = string;
    					display1(phrase);
    					//add(pan);
    					repaint();
    					}
    
    
    		void go(){
    		String is = "";
    //Draw9 me=new Draw9();
    
    
    int i = 1;
    while (i > 0) {
    Random generator=new Random();
    int rand1=generator.nextInt(10)+90;				
    is = String.valueOf(rand1);
    display(is);							try{						Thread.sleep(2000);}					catch(Exception ex){ }//Thread.sleep(10 secs);
    		i++;
    
    	}//while
    }
    	}//class
    
    
    
    
    class callDraw{
    	public JPanel jp;
    
    	public static void main(String[] args)throws InterruptedException{
    
    	JFrame fr=new JFrame();
    	fr.setTitle("");
    	fr.setLocation(100,100);
    	fr.setPreferredSize(new Dimension(350,400));
    	fr.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    	JPanel j=new JPanel();
    	j.setPreferredSize(new Dimension(120,120));
    	j.setMaximumSize(new Dimension(120,120));
    	j.setBackground(Color.blue);
    
    	//Draw10 jp;
    	//jp=new Draw10();
    	//jp.go();
    
    	fr.getContentPane().add(j, BorderLayout.CENTER);
    	//fr.getContentPane().add(jp, BorderLayout.EAST);
    	fr.setVisible(true);
    	fr.pack();
    
    		}
    	}

    I will really appreciate your help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to avoid the creative code indentation here as it makes your code very difficult to read. Some of your problems:

    1) You never initialize the String variable, string. Give it something, at least = "";
    2) By calling Thread.sleep(2000) on the EDT (the event dispatch thread), you're putting it to sleep. Since this is the thread that draws your app and allows your app to interact with the user, this effectively puts the whole app to sleep. So don't do this, but rather, use a Swing Timer instead. You can find out how in the Sun Swing tutorials.
    3) Even if you did put text in the JPanel, you likely won't see it with this:
    Java Code:
        setBackground(Color.blue);
        setForeground(Color.blue);
    Since the foreground and background colors are the same. Solution, make one different from the other.
    4) If you don't call super.paintComponent(comp) as the first method in your JPanel's paintComponent method, your old numbers will not erase, and so you'll just over-write them.

    e.g.,
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class Draw10b {
      private static final Dimension SIZE = new Dimension(200, 150);
      private static final Font NUMBER_LABEL_FONT = new Font("Serif", Font.BOLD, 58);
      private static final Color LABEL_COLOR = Color.white;
      private static final Color BACKGROUND = Color.blue;
      private static final int TIMER_DELAY = 2000;
      private JPanel mainPanel = new JPanel();
      // jlabel to hold the random number.  Centers the text in the label.
      private JLabel numberLabel = new JLabel("", SwingConstants.CENTER) {
        // this method is not necessary, but draws smoother text in the JLabel 
        protected void paintComponent(Graphics g) {
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
              RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
          super.paintComponent(g);
        }
      };
    
      public Draw10b() {
        mainPanel.setPreferredSize(SIZE);
        mainPanel.setBackground(BACKGROUND);
        mainPanel.setLayout(new BorderLayout());
        mainPanel.add(numberLabel, BorderLayout.EAST);
        numberLabel.setFont(NUMBER_LABEL_FONT);
        numberLabel.setForeground(LABEL_COLOR);
        
        // Swing Timer that changes displayed number every TIMER_DELAY msecs.
        Timer timer = new Timer(TIMER_DELAY, new ActionListener() {
          private Random generator = new Random();
    
          // this method gets called every TIMER_DELAY msecs
          public void actionPerformed(ActionEvent e) {
            int rand1 = generator.nextInt(10) + 90;
            numberLabel.setText(String.valueOf(rand1));
          }
        });
    
        timer.start();
      }
    
      public JComponent getPanel() {
        return mainPanel;
      }
      private static void createAndShowGUI() {
        JFrame frame = new JFrame("Draw10b");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new Draw10b().getPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
    }
    Last edited by Fubarable; 12-28-2009 at 06:59 PM.

  3. #3
    thestarncy is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    Thank Fudarable, you have really moved me alot. But, could it just import into another JPanel that is in another JFrame with more other JPanels?

    If so, can i do away with createAndShowGui method so that i can call the getPanel() method from my other JPanel in the other JFrame/

    Thanks again, I know i sound raw.

    I may not post the whole other JFrame here as I created it usinng netbeans, and it has a hell of a code.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. Help with random numbers
    By checkmylongboarding in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 06:47 AM
  3. Random numbers
    By jithan in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2008, 03:04 PM
  4. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 11:44 PM
  5. random numbers
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 03:56 AM

Posting Permissions

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