开发者

automatic repaint when minimizing window

I have a JFrame, with two panels, in one panel I draw a line, as I was working I minimized the Window of the java program i'm doing, when i maximized it THE LINE THAT I DREW WAS DIFFERENT, it repainted it in a different place!

Does anyone have an idea of how to lock the painting so that when i minimize it doesn't screw the drawing??

Thank you!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;


class JFramePaint1 extends JFrame implements ActionListener /*implements ActionListener*/{

    public static int activa = 0;
    public static JButton drawing = new JButton("drawing");
    public static JButton erase = new JButton("erase");
    public static int x1=0, y1=0,x2=0,y2=0;

   public JFramePaint1(){
                                                    //row column
      JPanel container = new JPanel(); //new JPanel(new GridLayout(2,1));
      JPanel header = new JPanel();
    //  header.setLayout(new GridLayout(50, 2));
    // header.setLayout(new FlowLayout());
     header.setLayout(new BoxLayout(header, BoxLayout.LINE_AXIS));
     container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); //lo quite ahorita
     // container.setLayout(new FlowLayout());

     ContentComponent c = new ContentComponent();

      drawing.addActionListener(this);
      erase.addActionListener(this);

    //header.setSize(30,30);

     //drawing.setAlignmentY(Component.BOTTOM_ALIGNMENT);
     container.add(Box.createRigidArea(new Dimension(100, 0)));

     header.add(drawing); //lo quite ahorita
     header.add(erase);
     container.add(hea开发者_运维知识库der);
    //container.add(Box.createRigidArea(new Dimension(5,0)));
    //header.add(drawing);

      // container.add(header);
      container.add(c);   //lo quite ahorita

      add(container);

    //  add(c);
       //add(c);
   }
   public static void main(String[] a) {

        JFramePaint1 VentanaDiverti = new JFramePaint1();
        VentanaDiverti.setSize(800, 700);
        VentanaDiverti.setLocation(200, 0);
        VentanaDiverti.setTitle("Diverti");
        VentanaDiverti.setResizable ( false );
        VentanaDiverti.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        VentanaDiverti.setVisible(true);

    /* JFrame f = new JFrame();
    // JFramePaint1 f = new JFramePaint1();
       f.setTitle("Drawing Graphics in Frames");
        f.setSize(800, 650);
        f.setLocation(200,50);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        f.setContentPane( new ContentComponent());
        f.getContentPane().add(b);

        //f.addWindowListener(this);
        //b.addActionListener(this);


        f.setVisible(true);*/

   }
   static class ContentComponent extends JPanel {

     public void paint(Graphics g) {

         BufferedImage image;
         /*reset the variables, this makes the repaint look the same! it's as comments so that you can see what happens
         x1=0;
            y1=0;
            x2=0;
            y2=0;
        */
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
            // handle exception...
       }


         g.setColor (Color.RED);
         // void    fillRect(int x, int y, int width, int height)
         // Fills the specified rectangle.
         g.fillRect(0, 0, 800, 600);
         if( activa == 1){

         g.setColor(Color.BLACK);
        // g.drawRect(40, 40, 150, 80);
         int x = 40;
         int y= 40;
         for(int i = 0; i< 4; i++){

            //g.drawRect(x+10, y+10, 150, 80);
            x = x+10;
            y = y+10;
         }
         //drawLine(int x1, int y1, int x2, int y2)
         //Draws a line, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.
           x1+=20;
           x2+=20;
           y2+=50;
           g.drawLine(x1,y1,x2,y2);
           x2+=30;
           y1=y2;
           g.drawLine(x1,y1,x2,y2);
          // g.drawLine(20,0,20,50);

         }
        // activa = 0;

      }//del paint
   }
    public void actionPerformed(ActionEvent e)
      {
          if(e.getSource()== drawing)
        {   System.out.println("entro");
           if(activa==0){
            activa = 1;

            repaint();}

        }
        if(e.getSource()==erase){
            activa = 0;
            x1=0;
            y1=0;
            x2=0;
            y2=0;
            repaint();
        }
      }
    public void windowClosing(WindowEvent e) 
     {

        System.exit(0);

     }
     public void windowOpened(WindowEvent e){}

     public void windowClosed(WindowEvent e){}

     public void windowActivated(WindowEvent e){}

     public void windowDeactivated(WindowEvent e){}

     public void windowIconified(WindowEvent e){}

     public void windowDeiconified(WindowEvent e){}  
}


I think it is not normal for the program to auto paint when i minimized it and then maximized it.

It is very normal for this to happen. The paint method can be called at any time, whenever the system needs to redraw a portion of your window (see Painting in AWT for specific details). When you minimize the window, whatever you drew into it before is not saved anywhere, so when you maximize the window, all of the contents need to be drawn again, so paint will be called automatically.

For this reason, you should not perform any updates to your data in paint. You should do these in a separate method, store the results in your variables and only use these existing results to draw the content.

Since the following parts do not change:

image = ImageIO.read(new File("image name and path"));
x1+=20;
x2+=20;
y2+=50;
x2+=30;

move them into a separate method, say updatePoints. Then in your actionPerformed method, first call updatePoints() and then repaint().


You could add a window listener to your jframe that calls the panels paint method when a resize/maximize/whatever event is triggered.


Basically, just to add to what casablanca said, what is happening is that since your x1, x2, y1, y2 variables are static, they are getting updated every time you perform a paint, and then staying at that value until the next repaint, at which time they are updated again, etc...

My recommendation would actually be to separate your data from your display code. So, instead of having x1, x2, y1, and y2 declared as static global variables, have them part of the inner class (what you call ContentComponent, and have a starting x1, x2, y1, y2 values as your global variables, like this:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

class JFramePaint1 extends JFrame implements ActionListener /*implements ActionListener*/{

  public static int activa = 0;
  public static JButton drawing = new JButton("drawing");
  public static JButton erase = new JButton("erase");
  public static final int sx1 = 0, sy1 = 0, sx2 = 0, sy2 = 0;

  public JFramePaint1() {
    //row column
    JPanel container = new JPanel(); //new JPanel(new GridLayout(2,1));
    JPanel header = new JPanel();
    //  header.setLayout(new GridLayout(50, 2));
    // header.setLayout(new FlowLayout());
    header.setLayout(new BoxLayout(header, BoxLayout.LINE_AXIS));
    container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS)); //lo quite ahorita
    // container.setLayout(new FlowLayout());

    ContentComponent c = new ContentComponent();

    drawing.addActionListener(this);
    erase.addActionListener(this);

    //header.setSize(30,30);

    //drawing.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    container.add(Box.createRigidArea(new Dimension(100, 0)));

    header.add(drawing); //lo quite ahorita
    header.add(erase);
    container.add(header);
    //container.add(Box.createRigidArea(new Dimension(5,0)));
    //header.add(drawing);

    // container.add(header);
    container.add(c); //lo quite ahorita

    add(container);

    //  add(c);
    //add(c);
  }

  public static void main(String[] a) {

    JFramePaint1 VentanaDiverti = new JFramePaint1();
    VentanaDiverti.setSize(800, 700);
    VentanaDiverti.setLocation(200, 0);
    VentanaDiverti.setTitle("Diverti");
    VentanaDiverti.setResizable(false);
    VentanaDiverti.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    VentanaDiverti.setVisible(true);

    /* JFrame f = new JFrame();
    // JFramePaint1 f = new JFramePaint1();
       f.setTitle("Drawing Graphics in Frames");
        f.setSize(800, 650);
        f.setLocation(200,50);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        f.setContentPane( new ContentComponent());
        f.getContentPane().add(b);

        //f.addWindowListener(this);
        //b.addActionListener(this);


        f.setVisible(true);*/

  }
  static class ContentComponent extends JPanel {

    static int x1, x2, y1, y2;

    public void paint(Graphics g) {
      x1 = sx1;
      x2 = sx2;
      y1 = sy1;
      y2 = sy2;

//      BufferedImage image;
      /*reset the variables, this makes the repaint look the same! it's as comments so that you can see what happens
      x1=0;
         y1=0;
         x2=0;
         y2=0;
      */
//      try {
//        image = ImageIO.read(new File("image name and path"));
//      } catch(IOException ex) {
//        // handle exception...
//      }

      g.setColor(Color.RED);
      // void    fillRect(int x, int y, int width, int height)
      // Fills the specified rectangle.
      g.fillRect(0, 0, 800, 600);
      if(activa == 1) {

        g.setColor(Color.BLACK);
        // g.drawRect(40, 40, 150, 80);
        int x = 40;
        int y = 40;
        for(int i = 0; i < 4; i++) {

          //g.drawRect(x+10, y+10, 150, 80);
          x = x + 10;
          y = y + 10;
        }
        //drawLine(int x1, int y1, int x2, int y2)
        //Draws a line, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.
        x1 += 20;
        x2 += 20;
        y2 += 50;
        g.drawLine(x1, y1, x2, y2);
        x2 += 30;
        y1 = y2;
        g.drawLine(x1, y1, x2, y2);
        // g.drawLine(20,0,20,50);

      }
      // activa = 0;

    }//del paint
  }

  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == drawing) {
      System.out.println("entro");
      if(activa == 0) {
        activa = 1;

        repaint();
      }

    }
    if(e.getSource() == erase) {
      activa = 0;
      repaint();
    }
  }

  public void windowClosing(WindowEvent e) {

    System.exit(0);

  }

  public void windowOpened(WindowEvent e) {}

  public void windowClosed(WindowEvent e) {}

  public void windowActivated(WindowEvent e) {}

  public void windowDeactivated(WindowEvent e) {}

  public void windowIconified(WindowEvent e) {}

  public void windowDeiconified(WindowEvent e) {}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜