Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

debugging -- repaint() problem

Posted on 2004-03-29
14
Medium Priority
?
534 Views
Last Modified: 2008-03-10
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);
  }
}

0
Comment
Question by:vijaysaravanan
  • 6
  • 6
  • 2
14 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10702393
>> 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
 

Author Comment

by:vijaysaravanan
ID: 10702407
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
 
LVL 37

Expert Comment

by:zzynx
ID: 10702452
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10702565
If you are not repainting on min/max, then you will get a blank frame.
0
 

Author Comment

by:vijaysaravanan
ID: 10702644
>>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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10702662
even your buttons won't appear
0
 

Author Comment

by:vijaysaravanan
ID: 10702669
Thanks for ur reply
buttons appear but the graphics components are missing !!!
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10702682
Can you tell me your exact requirement
0
 

Author Comment

by:vijaysaravanan
ID: 10702697
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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10702829
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
 

Author Comment

by:vijaysaravanan
ID: 10702875
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
 
LVL 9

Accepted Solution

by:
mmuruganandam earned 800 total points
ID: 10702955
You can try with Shape object

clipRect, setClip

0
 

Author Comment

by:vijaysaravanan
ID: 10702999
>>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
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 10703029
Thanks Man!

It is always been a pleasure to answer tricky questions.


Cheers.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question