Search code examples
javaswingpaintcomponent

JTextFild Glitched Out


OK, So I have a program that displays a a String on a JPanel and it draws the lines of where the Ascents are, it draws a line at the Descent and halfway between, and a quarter way between. I have a combo box to change the font and the size of the String, and i have a JTextField to change the Text itself. I used actionListeners to update the String. Whenever i update the text of the String via the JTextField, the program glitches out, and shows a copy of the image of the JTextField on the top right corner of the JFrame.

Code:

package com.Patel.RichClients;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

//a class to demonstrate 
public class StringAscent extends JFrame {
    private static Font font;
    private int fontSize = 50; 
    private StringPanel panel; 
    private JComboBox fontOptions; 
    private JComboBox fontSizeOptions; 
    private JTextField text; 


    // constructor
    public StringAscent() {
    // set the initial font to Times new Roman
    font = new Font("Agency FB", Font.PLAIN, fontSize);
    setVisible(true);
    setSize(520, 170);
    setTitle("String Ascents");
    setLayout(new BorderLayout());

    //set up the components 
    GraphicsEnvironment ge= GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontNames = ge.getAvailableFontFamilyNames();
    fontOptions = new JComboBox(fontNames);
    text = new JTextField(22);
    text.setText("Swing");
    String[] sizeOptions = new String[50];
    for (int i = 0; i < sizeOptions.length; i ++){
        sizeOptions[i] = Integer.toString(i + 1);
    }
    fontSizeOptions = new JComboBox(sizeOptions); 
    panel = new StringPanel();

    //set up actionListeners 
    fontOptions.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
        JComboBox ref = (JComboBox) e.getSource();
        font = new Font(fontNames[ref.getSelectedIndex()], Font.PLAIN, fontSize);
        panel.repaint();
        }    
    });
    fontSizeOptions.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
        JComboBox ref = (JComboBox) e.getSource();
        fontSize = Integer.parseInt(sizeOptions[ref.getSelectedIndex()]);
        font = new Font(font.getName(), Font.PLAIN, fontSize);
        panel.repaint();
        }    
    });
    text.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
        panel.repaint();
        }  

    });

    //add components
    add(panel, BorderLayout.NORTH);
    add(fontOptions, BorderLayout.WEST);
    add(text, BorderLayout.CENTER);
    add(fontSizeOptions, BorderLayout.EAST);
    }


    public static void main(String[] args) {
    Runnable showGui = new Runnable() {
        public void run() {
        StringAscent gui = new StringAscent();
        }
    };
    SwingUtilities.invokeLater(showGui);
    }

    // inner JPanel class
    private class StringPanel extends JPanel {

    // constructor
    public StringPanel() {

    }

    public  Dimension getPreferredSize() {
       return new Dimension(400, 100);
    }

    public void paintComponent(Graphics g) {
        g.setColor(Color.black);

        // set up the string
        g.setFont(font);
        // FontMetric is an object that holds information relevant to the
        // rendering of the font
        FontMetrics metric = g.getFontMetrics();
        int ascent = metric.getAscent();
        String string = text.getText();
        g.drawString(string, 100, 50);

        int x = 50;
        // draw Ascent line
        g.drawLine(x, 50 - ascent, x + 180, 50 - ascent);

        // draw Ascent/2 line
        g.drawLine(x, 50 - (ascent / 2), x + 180, 50 - (ascent / 2));

        // draw Ascent/4 line
        g.drawLine(x, 50 - (ascent / 4), x + 180, 50 - (ascent / 4));

        // draw baseline line
        g.drawLine(x, 50, x + 180, 50);

        // draw Descent line
        g.drawLine(x, 50 + metric.getDescent(), x + 180, 50 + metric.getDescent());

    }
    }
}

Solution

  • Add super.paintComponent(g) to the start of your paintComponent method

    public void paintComponent(Graphics g) {
        super.paintComponent(g)
        g.setColor(Color.black);
        //...
    

    Basically, one of the jobs of paintComponent is to prepare the Graphics context for painting the current component. In Swing, the Graphics is a shared resource which is used by all the components within a window when been painted, so it's important to make sure that context is prepared properly before you paint on it

    Take a look at Performing Custom Painting and Painting in AWT and Swing for more details about how painting works