2010-11-18 94 views
1

我有一個JFrame,有兩個面板,在一個面板中畫一條線,因爲我在工作我最小化了我正在做的Java程序的窗口,當我最大化它時我的線條DREW是不同的,它在一個不同的地方重新粉刷它!最小化窗口時自動重繪

有沒有人有關於如何鎖定繪畫的想法,以便當我最小化它不會螺絲圖?

謝謝!

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(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 { 

    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){} 
} 
+0

好,我已通過添加合適的paint方法內該行的初始座標解決它,所以它繪製線在相同的位置後,我窗口最大化,所以它使它看起來像如果有人知道如何鎖定它,我仍然想知道 – bb2 2010-11-18 21:07:46

+0

你是不是很清楚你想要什麼?你是什麼意思的'鎖定'? – jjnguy 2010-11-18 21:10:04

+0

當我最小化窗口時不重畫。如果我最小化窗口然後使其最大化,我沒有看到最初的繪圖,這條線出現在不同的地方,我畫了線li這個x1 + = 20; \t \t x2 + = 20; \t \t y2 + = 50; g。 \t \t g。的drawLine(X1,Y1,X2,Y2);所有座標的初始值都是0.這些x1等是全局變量,所以這就是爲什麼當我將它最小化時,它在不同的位置重新繪製它...我認爲程序自動繪製時不是正常的,當我最小化它,然後最大化它。 – bb2 2010-11-18 21:12:38

回答

1

我認爲這是不正常的程序,自動噴漆時,我最小化,然後最大化它。

這是很正常的事情發生。只要系統需要重繪窗口的一部分(有關具體細節,請參閱Painting in AWT),paint方法可在任何時候在處調用。當你最小化窗口時,無論你畫的是什麼,都不會在任何地方保存,所以當你最大化窗口時,所有的內容都需要再次繪製,所以paint會自動調用。

因此,您不應對paint中的數據執行任何更新。您應該使用單獨的方法執行這些操作,將結果存儲在變量中,並且只使用這些現有結果來繪製內容。

由於以下幾個部分不改變:

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

它們移動到一個單獨的方法,說updatePoints。然後在您的actionPerformed方法中,首先撥打電話updatePoints(),然後撥打repaint()

0

當觸發resize/maximize/whatever事件時,您可以向您的jframe添加一個窗口偵聽器,該窗口偵聽器調用面板繪製方法。

0

基本上,只是爲了增加卡薩布蘭卡所說的,發生的事情是,因爲你的x1,x2,y1,y2變量是靜態的,所以每次執行繪畫時都會更新,然後保持該值直到下一次重畫,然後再次更新,等等...

我的建議實際上是將您的數據與顯示代碼分開。因此,將x1,x2,y1和y2聲明爲靜態全局變量,而不是讓它們成爲內部類的一部分(您稱之爲ContentComponent,並將x1,x2,y1,y2的開始值作爲全局變量,就像這樣的:

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) {} 
}