Results 1 to 3 of 3
- 12-28-2009, 02:15 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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.
-
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:
Since the foreground and background colors are the same. Solution, make one different from the other.Java Code:setBackground(Color.blue); setForeground(Color.blue);
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 05:59 PM.
- 12-29-2009, 09:46 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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
-
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
Help with random numbers
By checkmylongboarding in forum New To JavaReplies: 2Last Post: 01-12-2009, 05:47 AM -
Random numbers
By jithan in forum Advanced JavaReplies: 3Last Post: 06-14-2008, 02:04 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM -
random numbers
By carlos123 in forum New To JavaReplies: 1Last Post: 12-22-2007, 02:56 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks