Solved

debugging -- repaint() problem

Posted on 2004-03-29
14
516 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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
 
LVL 9

Expert Comment

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

Author Comment

by:vijaysaravanan
Comment Utility
>>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
Comment Utility
even your buttons won't appear
0
 

Author Comment

by:vijaysaravanan
Comment Utility
Thanks for ur reply
buttons appear but the graphics components are missing !!!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 9

Expert Comment

by:mmuruganandam
Comment Utility
Can you tell me your exact requirement
0
 

Author Comment

by:vijaysaravanan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You can try with Shape object

clipRect, setClip

0
 

Author Comment

by:vijaysaravanan
Comment Utility
>>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
Comment Utility
Thanks Man!

It is always been a pleasure to answer tricky questions.


Cheers.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 46
wordappend challenge 8 83
eclipse formatting 6 38
ArrayIndexOutOfBoundException 9 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now