Drawing using Paintcomponent() method

From: Bartosz Wegrzyn (blwegrzyn_at_lexon.ws)
Date: 11/29/03


Date: Sat, 29 Nov 2003 02:45:13 GMT

hello,

My program whenever the user presses enter on the email address
textbox should display the information that was enter using the
paintcomoinent() method. Everything works fine but only
when user will resize the window after he presses the enter key.
Then the information will show up.

I thing that the way I wrote the program is wrong.
I thing that I should be able to create the Graphics2D object
in the keypressed() method and implement everything there.

But I am not sure how.
Please help.

/*
 * email.java
 *
 * Created on November 9, 2003, 9:09 PM
 */

/**
 *
 * @author  btgs
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.font.*;

public class email {
 
    /** Creates a new instance of email */
    public email() {
    }
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

 
    emailFrame frame = new emailFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
    
    
    }

}

class emailFrame extends JFrame {
 
    public emailFrame() {
    
        // get screen dimensions

        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;

        //center frame in screen

        setSize(screenWidth /2 , screenHeight /2);
        setLocation(screenWidth /4, screenHeight /4);

        //set title
        setTitle("e-mail-directory");

    Container contentPane = getContentPane();
    
          
       //add new panels
       JPanel panel = new JPanel();
       
       //set the layout
       panel.setLayout(new GridLayout(0,2));
       
        //add labels
       L1 = new JLabel("First Name");
       L2 = new JLabel("Last Name");
       L3 = new JLabel("E-mail address");
       
       //add text fields
       T1 = new JTextField("enter First Name",10);
       T2 = new JTextField("enter Last Name",10);
       T3 = new JTextField("enter your email",10);

        
       //add components to the panel
        panel.add(L1);
        panel.add(T1);
        panel.add(L2);
        panel.add(T2);
        panel.add(L3);
        panel.add(T3);

        //add panel to the frame
        contentPane.add(panel,BorderLayout.NORTH);
        
        
        KeyHandler listener = new KeyHandler();
        T3.addKeyListener(listener);
        
    }
       
private class KeyHandler implements KeyListener {
    
    
        public void keyPressed(KeyEvent e) {
            
            int keyCode = e.getKeyCode();
            if (keyCode == KeyEvent.VK_ENTER) {
                  
       
        
         emailPanel displaypanel = new emailPanel();
         getContentPane().add(displaypanel,BorderLayout.CENTER);
         repaint();
         
               
            }
        }
  
        public void keyReleased(KeyEvent e) {
            //we leave it as default
        }
        
        public void keyTyped(KeyEvent e) {
            //we leave it as default
        }
            

        

class emailPanel extends JPanel

{
    
  public void paintComponent(Graphics g) {
        
        
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        String message = "The Email information you entered is:";
        Font f = new Font("Serif", Font.BOLD,20);
        g2.setFont(f);
        
        //measure the size of the message
        
        FontRenderContext context = g2.getFontRenderContext();
        Rectangle2D bounds = f.getStringBounds(message,context);
        
        double x = bounds.getWidth();
        double y = bounds.getHeight();
        
        double width = getWidth();
        double height = getHeight();
        
       
        //display title
        g2.drawString(message, (((int)width /2) - ((int)x/2)), (int)height /2);
        
       
        
        //change the font size
        Font a = new Font("Times", Font.PLAIN, 12);
        g2.setFont(a);
        
        
        //put data into string
        String line1 = L1.getText() + " : " + T1.getText();
        String line2 = L2.getText() + " : " + T2.getText();
        String line3 = L3.getText() + " : " + T3.getText();
        
        //display information on the form
        g2.drawString(line1, (((int)width /2) - ((int)x/2)), ((int)height /2) + 20);
        g2.drawString(line2, (((int)width /2) - ((int)x/2)), ((int)height /2) + 40);
        g2.drawString(line3, (((int)width /2) - ((int)x/2)), ((int)height /2) + 60);
  
    }
    
    
}
        
        
        
        
        
        }
   

protected JLabel L1,L2,L3;
protected JTextField T1,T2,T3;

}