Java: JPanel, Graphics, textArea question

Discussion in 'OT Technology' started by morgue, Dec 3, 2006.

  1. morgue

    morgue New Member

    Joined:
    Jul 10, 2004
    Messages:
    8,278
    Likes Received:
    0
    How can I get the JTextArea to display without having to resize the window? And is there a way I can position it?

    Oval.java
    Code:
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    public class Oval extends JPanel {
        
        public void paintComponent( Graphics g ) {
            super.paintComponent( g );
            
            Random randomNumbers = new Random();
            int width = 1 + randomNumbers.nextInt( 200 );
            int height = 1 + randomNumbers.nextInt( 200 );
            
            double area = Math.PI * (width/2) * (height/2);
            
            String text = "Area = " + area + "\n";
            JTextArea textArea1 = new JTextArea( text, 10, 10 );
            
            add (textArea1);
          
            //( int x, int y, int width, int height )
            g.drawOval( 10, 10, width, height );
        }
    }
    
    OvalTest.java
    Code:
    import javax.swing.JFrame;
    
    public class OvalTest {
            
        public static void main(String args[]) {
            JFrame frame = new JFrame( "Oval of random size" );
            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setSize( 600, 210 );
            
            Oval oval = new Oval();
            
            frame.add(oval);
            
            frame.setVisible( true );
        }
    }
    
     

Share This Page