Search code examples
javaswingdrawpaint

Java swing: i called once drawString and it printed my string multiple times



I'm new in java swing programming. What i'm trying to do is paint a string to a specific location in a JPanel. The JPanel is very large and so i add it to a JScrollpane, but when i draw the string it is printed not just in the specified location but also in others.
The first image represents the bottom of the panel where i decided to draw the string and this is correct. But if you observe whole the panel you can find the string too in others locations (see second image).
Can someone tell me why this happen? How can i prevent it? enter image description here

enter image description here

import java.util.*; 
import java.io.*;
import javax.swing.*;
import java.awt.*;
class Example extends JFrame
{
    private MyPanel gg=new MyPanel();
    Example(){
        add(new JScrollPane(gg));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(MAXIMIZED_BOTH);
    }
    public static void main(String argv[]){
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                Example test=new Example();
                test.setVisible(true);
            }
        });
        return;
    }
}
class MyPanel extends JPanel
{
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d=(Graphics2D)g;
        g2d.drawString("HI I LOVE ELON MUSK", 90, 300035);
        return;
    }
    public Dimension getPreferredSize() {
        return new Dimension(500, 300060);
    }
}

Solution

  • Your code ran fine on my Windows 10 system. I have a Java 13 JDK that I compile at Java 8.

    I made a few changes to your main class. Maybe these changes stabilized the display. Run my code on your system and see.

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    
    public class LongJPanelExample {
    
        public LongJPanelExample() {
            JFrame frame = new JFrame("Long JPanel Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            MyPanel gg = new MyPanel();
            frame.add(new JScrollPane(gg));
    
            frame.pack();
            frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            frame.setVisible(true);
        }
    
        public static void main(String argv[]) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new LongJPanelExample();
                }
            });
        }
    }
    
    class MyPanel extends JPanel {
        private static final long serialVersionUID = 1L;
    
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.drawString("HI I LOVE ELON MUSK", 90, 300035);
            return;
        }
    
        public Dimension getPreferredSize() {
            return new Dimension(500, 300060);
        }
    }