debugging -- repaint() problem

Hi

I have one bug in my java program, when I minimize or maximize my frame window. It repaints....I have tried many ways to solve this issue.. but i could n't

if some of you have ideas for solving this problem kindly coment on it
I have pasted the code below


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

public class Screen extends JFrame {
  int X,Y;
  int Angle_value,Width_value,Height_value;
 JPanel contentPane;
 boolean draw_flag=false;
PaintPanel p1;
 JSlider X_Size = new JSlider();
 JSlider Y_Size = new JSlider();
 JSlider Angle = new JSlider();
  JButton Line = new JButton();
  JButton Rectangle = new JButton();
  JLabel jLabel1 = new JLabel(new ImageIcon("./icons/track.jpg"));
 /**Construct the frame*/
 public Screen() {
   enableEvents(AWTEvent.WINDOW_EVENT_MASK);
   try {
     jbInit();
   }
   catch(Exception e) {
     e.printStackTrace();
   }
 }
 /**Component initialization*/
 private void jbInit() throws Exception  {

   p1= new PaintPanel();
   setContentPane(p1);
   contentPane = (JPanel) this.getContentPane();
   contentPane.setLayout(null);

   this.setSize(new Dimension(1034, 679));
   this.setTitle("Frame Title");
    contentPane.setEnabled(false);
    contentPane.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
      public void mouseDragged(MouseEvent e) {
        contentPane_mouseDragged(e);
      }
    });
    contentPane.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        contentPane_mouseClicked(e);
      }
      public void mousePressed(MouseEvent e) {
        contentPane_mousePressed(e);
      }
    });
    X_Size.setMajorTickSpacing(1);
    X_Size.setMaximum(700);
    X_Size.setMinimum(1);
    X_Size.setPaintLabels(false);
    X_Size.setBounds(new Rectangle(35, 73, 197, 20));
    X_Size.setValue(X_Size.getMaximum()/2);
    Y_Size.setMajorTickSpacing(1);
    Y_Size.setMaximum(200);
    Y_Size.setMinimum(1);
    Y_Size.setPaintLabels(false);

    Y_Size.setBounds(new Rectangle(36, 112, 197, 20));
    Y_Size.setValue(Y_Size.getMaximum()/2);
    Angle.setMajorTickSpacing(1);
    Angle.setMaximum(360);
    Angle.setMinimum(0);
    Angle.setPaintLabels(false);
    Angle.setPreferredSize(new Dimension(200, 42));
    Angle.setBounds(new Rectangle(39, 147, 190, 24));
    Angle.setValue(Angle.getMaximum());
    Line.setBounds(new Rectangle(284, 19, 48, 40));
    Line.setText("L");
    Line.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Line_actionPerformed(e);
      }
    });
    Rectangle.setBounds(new Rectangle(349, 17, 49, 43));
    Rectangle.setText("R");
    Rectangle.addActionListener(new ActionListener(this));
    Rectangle.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Rectangle_actionPerformed(e);
      }
    });



    Angle.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent e) {

       Angle_actionPerformed();

                }  });


    X_Size.addChangeListener(new javax.swing.event.ChangeListener() {
    public void stateChanged(javax.swing.event.ChangeEvent e) {
        X_Size_actionPerformed();   }  });


      Y_Size.addChangeListener(new javax.swing.event.ChangeListener() {
      public void stateChanged(javax.swing.event.ChangeEvent e) {
        Y_Size_actionPerformed();  }  });



    jLabel1.setBackground(Color.white);
    jLabel1.setForeground(Color.red);
    jLabel1.setSize(300,300);


    jLabel1.setBounds(new Rectangle(514, 35, 33, 39));
    contentPane.add(X_Size, null);
    contentPane.add(Y_Size, null);
    contentPane.add(Angle, null);
    contentPane.add(Line, null);
    contentPane.add(Rectangle, null);
    contentPane.add(jLabel1, null);

 }



 /**Overridden so we can exit when window is closed*/
 protected void processWindowEvent(WindowEvent e) {
   super.processWindowEvent(e);

   if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
   }
 }






 class PaintPanel extends JPanel {
    public void paintComponent(Graphics g) {

     super.paintComponent(g);

    }

 }



  void Line_actionPerformed(ActionEvent e) {

  }

  /* when the rectangle clicked */
  void Rectangle_actionPerformed(ActionEvent e) {

  Graphics gg=this.getContentPane().getGraphics();
  gg.clearRect(300,300,700,200);
  gg.drawRect(300,300,700,200);
  I5_shapeLibrary sl=new I5_shapeLibrary(gg,X,Y,Angle_value,Width_value,Height_value);

  }

  /* when the angle is changed */
  void Angle_actionPerformed()
   {
     Angle_value=Angle.getValue();
     Rectangle.doClick();
   }


void Y_Size_actionPerformed()
   {
     Height_value=Y_Size.getValue();Rectangle.doClick();
         }

void X_Size_actionPerformed()
   {Width_value=X_Size.getValue();Rectangle.doClick();}


  void contentPane_mouseClicked(MouseEvent e) {
    X=e.getX();
          Y=e.getY();
          Rectangle.doClick();

    }

  void contentPane_mouseDragged(MouseEvent e) {

  }

  void contentPane_mousePressed(MouseEvent e) {

  }

}

class ActionListener implements java.awt.event.ActionListener {
  Screen adaptee;

  ActionListener(Screen adaptee) {
    this.adaptee = adaptee;
  }
  public void actionPerformed(ActionEvent e) {
    adaptee.Rectangle_actionPerformed(e);
  }
}

vijaysaravananAsked:
Who is Participating?
 
mmuruganandamCommented:
You can try with Shape object

clipRect, setClip

0
 
zzynxSoftware engineerCommented:
>> when I minimize or maximize my frame window. It repaints
Do you mean it *doesn't* repaint?
Otherwise I don't see the problem.
0
 
vijaysaravananAuthor Commented:
Thanks for your reply !

My requirements is when i do minimize or maximize my window it should not repaint()

But actually in the above code it repaints().....I want to stop repainting when i do this event ( that is window max and window mini )
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
zzynxSoftware engineerCommented:
When you maximize your window it's a completely normal behaviour that it repaints.
What should appear at the extra space created by maximizing it? Nothing?
When you maximize a window you want it to use the extra space. No?
0
 
mmuruganandamCommented:
If you are not repainting on min/max, then you will get a blank frame.
0
 
vijaysaravananAuthor Commented:
>>If you are not repainting on min/max, then you will get a blank frame.

you are right all the old graphics components which i have drawned before repainting is blank ??

so i want to keep those components
0
 
mmuruganandamCommented:
even your buttons won't appear
0
 
vijaysaravananAuthor Commented:
Thanks for ur reply
buttons appear but the graphics components are missing !!!
0
 
mmuruganandamCommented:
Can you tell me your exact requirement
0
 
vijaysaravananAuthor Commented:
In my frame I have buttons, slider etc , and  if i press the buttons some graphics componts are drawned into my window ( for instance rectangle )

But the problem is if I minimize my window and maximize all the graphics shapes like rectangle all are missing ??



0
 
mmuruganandamCommented:
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Draw extends JFrame implements ActionListener
{
      private JButton rectangle, line;
      private Object lastAction;
      private boolean isDeiconified = false;

      public Draw()
      {
            super();

            initialize();
      }

      private void initialize()
      {
            JPanel panel = (JPanel) getContentPane();
            panel.setLayout(new FlowLayout());

            rectangle = new JButton("Rectangle");
            line = new JButton("Line");

            panel.add(rectangle);
            panel.add(line);

            rectangle.addActionListener(this);
            line.addActionListener(this);

            setSize(500, 500);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            addWindowListener(new WindowAdapter()
            {
                  public void windowDeiconified(WindowEvent e)
                  {
                        isDeiconified = true;
                  }
            });
      }

      public void actionPerformed(ActionEvent e)
      {
            lastAction = e.getSource();
            performAction(lastAction);
      }

      private void performAction(Object source)
      {
            Graphics g = getGraphics();

            if (source == rectangle)
            {
                  g.drawRect(50, 50, 100, 50);
            }
            else if (source == line)
            {
                  g.drawLine(200, 100, 300, 100);
            }
      }

      public void paint(Graphics g)
      {
            super.paint(g);

            if (isDeiconified)
            {
                  isDeiconified = false;
                  performAction(lastAction);
            }
      }

      public static void main(String[] args)
      {
            new Draw();
      }
}



This would help you... I hope.
0
 
vijaysaravananAuthor Commented:
Hi Muruga ! :))

Thanks for your code !

I have seen your code and  have understood that your are  redrawing by using some flags (boolean check) after window event changes !

By the way if we have 1000 of lines and rectangle with different coodinates  !  what we  will do !

I think you got the point !

I am in search of  some idea where the components should remain in the window without coding it to redraw again!

Frriendly,
Vijay

0
 
vijaysaravananAuthor Commented:
>>You can try with Shape object

>>clipRect, setClip


Thanks Muruga !

The Last idea sounds great !
I will implement the last one !
Thanks for your pain in answering my questions !

Friendly,
Vijay

0
 
mmuruganandamCommented:
Thanks Man!

It is always been a pleasure to answer tricky questions.


Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.