-1

**When I try to change location of any component such as Button Label TextField some part of my shapes becomes invisible.When I delete the code where I set location to component(in this case it is TextField) shapes becomes to normal. **

public class Line {


    public static void main(String[] args) {
       JFrame frame = new JFrame("JFrame Example");
       frame.setSize(1366, 768);
       frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setLayout(null);

       frame.setVisible(true);
       JButton button=new JButton("Show lines");
       frame.add(button);
       button.setBounds(60, 400, 220, 30);
       button.setVisible(true);
       JTextField txtf=new JTextField();
       frame.add(txtf);
       txtf.setVisible(true);
       txtf.setSize(50, 100);

       button.addMouseListener(new MouseListener() {
           @Override
           public void mouseClicked(MouseEvent me) {
              button.setVisible(false);
              Graphics2D grf= (Graphics2D) frame.getGraphics();

        txtf.setVisible(false);
        txtf.setText("APPLE");
        txtf.setLocation(600, 600);
        txtf.setVisible(true);
        grf.fillOval(600, 600, 10, 10);

        grf.fillOval(190, 600, 10, 10);
        grf.fillOval(900, 650, 10, 10);
        grf.fillOval(750, 160, 10, 10);
        grf.fillOval(600, 400, 10, 10);
        grf.fillOval(1139, 266, 10, 10);
        grf.drawLine(1144, 271, 605, 405);
        grf.drawLine(195, 605, 605, 405);
        grf.drawLine(755, 165, 605, 405);
        grf.drawLine(755, 165, 1144, 271);
        grf.drawLine(905, 655, 1144, 271);
        grf.drawLine(905, 655, 605, 405);
        grf.drawLine(205, 205, 605, 405);
        grf.drawLine(205, 205, 755, 165);


}

           @Override
           public void mousePressed(MouseEvent me) {
           }

           @Override
           public void mouseReleased(MouseEvent me) {
           }

           @Override
           public void mouseEntered(MouseEvent me) {
           }

           @Override
           public void mouseExited(MouseEvent me) {
           }
       });

       button.setVisible(true);

    }

} 
Young Emil
  • 2,220
  • 2
  • 26
  • 37
Silver
  • 33
  • 5
  • 1
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Please use code formatting for code and code snippets, structured documents like HTML/XML or input/output. To do that, select the text and click the `{}` button at the top of the message posting/editing form. – Andrew Thompson Dec 18 '16 at 21:30
  • 2
    3) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Dec 18 '16 at 21:31

1 Answers1

1

When i try to change location of any compoenet such as Button Label TextFielt some part of my shapes becomes invisiable

You custom painting is all wrong. You should NOT be using the getGraphics() method of the frame to get a Graphics object. Any painting using this approach will be lost as soon as Swing determines a component needs to be repainted.

Whenever you change a property of a Swing component the component is repainted. Therefore you loose any painting associated with the above Graphics object.

Instead you should be overriding paintComponent(...) of a JPanel and add your painting logic to that method. Then you add the JPanel to the frame.

Read the section from the Swing tutorial on Custom Painting for more information and working examples. Download the examples and customize them for your actual requirement.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Apoligaise for distrubing.But i use netbeans.and for that reason my frame otomatically extends JFrame.But to use paintComponent the class must extend JPanel.I try to create JPanel in other class and then call it.But shapes dosn t become visiable.Can you send some sort of code to help me ? But consider that my frame extends JFrame. – Silver Dec 19 '16 at 14:51
  • @Silver, I did send your code. There is code in the tutorial. You can also search the forum for all kinds of examples that implement the paintComponent() method. `But i use netbeans.` - then don't use Netbeans to generate the GUI. You can use the IDE to write the code and debug, but don't generate the form using the IDE. Instead spend time learning Java not the IDE. I have never used an IDE to generate a form so I have no ideas how you would do it. – camickr Dec 19 '16 at 15:55