Solved

debugging -- repaint() problem

Posted on 2004-03-29
14
529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 264
how to see all occupied ports on windows 10 laptop 15 94
runtime exception 2 67
Running JavaFX on the Raspberry Pi 27 288
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

734 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