简体   繁体   中英

jpanel get messy when move the scroll

in one of my project i want to drew line in Jpanel, its work fine until i move scroll it become messy. i dont know how to refresh screen to remove this messy draws, and also from where they come ! thank you.

here is my code:

public class MainGui {

private JFrame frmGcodeplotter;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainGui window = new MainGui();
                window.frmGcodeplotter.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public MainGui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frmGcodeplotter = new JFrame();
    frmGcodeplotter.setTitle("GcodePlotter");
    frmGcodeplotter.setResizable(false);
    frmGcodeplotter.setBounds(100, 100, 942, 697);
    frmGcodeplotter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frmGcodeplotter.getContentPane().setLayout(null);


    try {
        javax.swing.UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Label label = new Label("Gcode Pannel");
    label.setBounds(10, 128, 103, 24);
    frmGcodeplotter.getContentPane().add(label);

    Label label_1 = new Label("X:");
    label_1.setBounds(10, 500, 25, 24);
    frmGcodeplotter.getContentPane().add(label_1);

    Label label_2 = new Label("Y:");
    label_2.setBounds(10, 530, 25, 24);
    frmGcodeplotter.getContentPane().add(label_2);

    Label label_3 = new Label("Z:");
    label_3.setBounds(10, 560, 25, 24);
    frmGcodeplotter.getContentPane().add(label_3);

    Label label_4 = new Label("Phi:");
    label_4.setBounds(10, 590, 25, 24);
    frmGcodeplotter.getContentPane().add(label_4);

    Label Xlabel = new Label("0.000 mm");
    Xlabel.setBounds(41, 500, 70, 24);
    frmGcodeplotter.getContentPane().add(Xlabel);

    Label YLabel = new Label("0.000 mm");
    YLabel.setBounds(41, 530, 70, 24);
    frmGcodeplotter.getContentPane().add(YLabel);

    Label Zlabel = new Label("0.000 mm");
    Zlabel.setBounds(41, 560, 70, 24);
    frmGcodeplotter.getContentPane().add(Zlabel);

    Label PhiLabel = new Label("0.000 mm");
    PhiLabel.setBounds(41, 590, 70, 24);
    frmGcodeplotter.getContentPane().add(PhiLabel);

    TextArea textArea = new TextArea();
    textArea.setBounds(10, 158, 203, 336);
    frmGcodeplotter.getContentPane().add(textArea);

    JButton btnStart = new JButton("Start");
    btnStart.setBounds(10, 13, 97, 25);
    frmGcodeplotter.getContentPane().add(btnStart);

    JButton btnPause = new JButton("Pause");
    btnPause.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnPause.setBounds(10, 51, 97, 25);
    frmGcodeplotter.getContentPane().add(btnPause);

    JButton btnStop = new JButton("Stop");
    btnStop.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
        }
    });
    btnStop.setBounds(10, 89, 97, 25);
    frmGcodeplotter.getContentPane().add(btnStop);

    JButton btnSend = new JButton("Send");
    btnSend.setBounds(116, 13, 97, 25);
    frmGcodeplotter.getContentPane().add(btnSend);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(225, 13, 699, 610);
    frmGcodeplotter.getContentPane().add(scrollPane);

//  JPanel panel = new JPanel(); //new Plotter(1000,1000);
    Plotter plotter = new Plotter(1000,1000);
    plotter.setForeground(Color.BLACK);
    plotter.setBackground(Color.WHITE);
    scrollPane.setViewportView(plotter);
    GroupLayout gl_plotter = new GroupLayout(plotter);
    gl_plotter.setHorizontalGroup(
        gl_plotter.createParallelGroup(Alignment.LEADING)
            .addGap(0, 1000, Short.MAX_VALUE)
    );
    gl_plotter.setVerticalGroup(
        gl_plotter.createParallelGroup(Alignment.LEADING)
            .addGap(0, 1000, Short.MAX_VALUE)
    );
    plotter.setLayout(gl_plotter);

    JButton btnConnect = new JButton("Connect");
    btnConnect.setBounds(116, 51, 97, 25);
    frmGcodeplotter.getContentPane().add(btnConnect);

    JMenuBar menuBar = new JMenuBar();
    frmGcodeplotter.setJMenuBar(menuBar);

    JMenu mnFile = new JMenu("File");
    menuBar.add(mnFile);

    JMenuItem mntmOpen = new JMenuItem("Open");
    mnFile.add(mntmOpen);

    JMenuItem mntmSave = new JMenuItem("Save");
    mnFile.add(mntmSave);

    JMenuItem mntmExit = new JMenuItem("Exit");
    mnFile.add(mntmExit);

    JMenu mnEdit = new JMenu("Edit");
    menuBar.add(mnEdit);

    JMenu mnCoonection = new JMenu("Coonection");
    menuBar.add(mnCoonection);

    JMenuItem mntmSerial = new JMenuItem("Serial");
    mnCoonection.add(mntmSerial);

    JMenu mnAbout = new JMenu("About");
    menuBar.add(mnAbout);

    JMenuItem mntmContactUs = new JMenuItem("Contact Us");
    mnAbout.add(mntmContactUs);
}

}

and here is plotter:

public class Plotter extends JPanel {


  /**
 * 
 */
private static final long serialVersionUID = -5142922023476436317L;
private int lines;
    private int lineGap;

    public Plotter(int lines, int lineGap)
    {
        // super();
        this.lines = lines;
        this.lineGap = lineGap;
    }

    @Override
    public Dimension getPreferredSize()
    {

        return new Dimension(lines, lineGap);
    }

    @Override
    public void paintComponent(Graphics g)
    {
       int width = getWidth();
       int height = getHeight();
       g.drawRect(0, 0, 200, 200);

        System.out.println("Width:"+width+" Height:"+ height);

    }     

}

and this is the screenshot of jpanel when i moved scroll 移动的滚动面板

Finnaly i got the problem. in paint component it must called "super.paintComponent(g)". the currect code is :

 public class Plotter extends JPanel {


  /**
 * 
 */

private static final long serialVersionUID = -5142922023476436317L;
private int lines;
    private int lineGap;

public Plotter(int lines, int lineGap)
{
    // super();
    this.lines = lines;
    this.lineGap = lineGap;
}

@Override
public Dimension getPreferredSize()
{

    return new Dimension(lines, lineGap);
}

@Override
public void paintComponent(Graphics g)
{
   super.paintComponent(g);
   int width = getWidth();
   int height = getHeight();
   g.drawRect(0, 0, 200, 200);

    System.out.println("Width:"+width+" Height:"+ height);

}     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM