2012-07-25 49 views
0

這裏是每5秒產生一個四氨基數字並繪製其中一個的代碼。 但是,這個圖被淹死了兩次,因此扭曲了原始圖片。圖形對象被繪兩次

這裏的四氨基數字由10x10個像素塊組成。 構造函數圖告訴塊的位置和顏色。方法paint(圖形g)逐塊地逐漸變暗。

import java.applet.*; 

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.awt.event.ActionListener; 
import java.awt.geom.Rectangle2D; 


import javax.swing.Timer; 
public class Tetris extends Applet { 

    Dimension size; 
    int x1,y1; 
    private int xs=0,ys=0; 


    public int getXs() { 
     return xs; 
    } 
    public void setXs(int xs) { 
     this.xs = xs; 
    } 
    public int getYs() { 
     return ys; 
    } 
    public void setYs(int ys) { 
     this.ys = ys; 
    } 
    public void rotate(){ 

    } 
    public Figure generate(){ 
     Figure i = new Figure(new int[][]{new int[]{1},new int[]{1},new int[]{1},new int[]{1}},Color.CYAN); 
     Figure j = new Figure(new int[][]{new int[]{1,0,0}, new int[]{1,1,1}},Color.BLUE); 
     Figure l = new Figure(new int[][]{new int[]{0,0,1}, new int[]{1,1,1}},Color.ORANGE); 
     Figure o = new Figure(new int[][]{new int[]{1,1}, new int[]{1,1}},Color.YELLOW); 
     Figure s = new Figure(new int[][]{new int[]{0,1,1}, new int[]{1,1,0}},Color.GREEN); 
     Figure t = new Figure(new int[][]{new int[]{1,1,1}, new int[]{0,1,0}},Color.PINK); 
     Figure z = new Figure(new int[][]{new int[]{1,1,0}, new int[]{0,1,1}},Color.RED); 

     Figure[] genSet = {i,j,l,o,s,t,z}; 
     int index =((int) (Math.random() * 7)) ; 
     //System.out.println(index); 
     return genSet[index]; 
    } 
    public void drop(){ 

    } 
    public void shift(){ 

    } 
    public void movecheck(){ 

    } 
    public void actiondelay(){ 
     ActionListener actionlistener = new ActionListener() { 
      public void actionPerformed(ActionEvent actionevent){ 
       repaint(); 
      } 

     }; 
     Timer timer = new Timer(5000,actionlistener); 
     timer.start(); 
    } 
    public void init(){ 
     setSize(200,400); 
     setBackground(Color.black); 
     size = getSize(); 

    } 

    public void paint(Graphics g){ 

     super.paint(g); 
     System.out.println("________________"); 
     Graphics2D g2d = (Graphics2D) g.create(); 
     Figure f = generate(); 
     int length = f.getX()[0].length; 
     for(int j =0; j<f.getX().length;j++){ 
      System.out.println(); 
      ys = 0;     
      for(int i=0;i<length;i++){      

       if (f.getX()[j][i] == 1){ 
        Rectangle2D p = new Rectangle2D.Double(xs,xs,xs+10,ys+10); 
        g2d.setColor(f.getY()); 
        g2d.draw(p); 
        //g2d.drawRect(p.x, p.y, p.width, p.height);  
        //g2d.fillRect(p.x, p.y, p.width, p.height);      
        //System.out.println("widnth: " +p.width + " | height: " + p.height + " end "); 
        System.out.print("*"); 
       } 
       else System.out.print(" "); 
       ys+=10;    
      } 
      xs+=10; 
     } 
     xs=0; 
     ys=0; 
     actiondelay();      
     //g.setColor(Color.white); 
     //g.drawRect(45, 95, 55, 105);   
    } 


} 


import java.awt.Color; 

public class Figure{ 
    int[][] x; 
    Color y; 
    public Figure(int[][] x , Color y){ 
     this.x=x; 
     this.y=y; 
    } 
    public int[][] getX() { 
     return x; 
    } 
    public void setX(int[][] x) { 
     this.x = x; 
    } 
    public Color getY() { 
     return y; 
    } 
    public void setY(Color y) { 
     this.y = y; 
    } 
} 

回答

0

您的圖片不會被扭曲,因爲數字是繪製兩次,而是因爲你有幾個失誤new Rectangle2D.Double(xs,xs,xs+10,ys+10);

  1. 在啓動xy位置你用xs兩次而不是xsys
  2. widthhight不是固定值,而是與xsys

當你去新的行時,你的循環也在增加xs而不是ys

試圖通過它來取代您的paint()方法:

public void paint(Graphics g){ 

    super.paint(g); 
    System.out.println("________________"); 
    Graphics2D g2d = (Graphics2D) g.create(); 
    Figure f = generate(); 
    int length = f.getX()[0].length; 
    for(int j =0; j<f.getX().length; j++){ 
     System.out.println(); 

     for(int i=0; i<f.getX()[0].length; i++){      

      if (f.getX()[j][i] == 1){ 
       Rectangle2D p = new Rectangle2D.Double(xs, ys, 10, 10); 
       g2d.setColor(f.getY()); 
       g2d.draw(p); 
       //g2d.drawRect(p.x, p.y, p.width, p.height);  
       //g2d.fillRect(p.x, p.y, p.width, p.height);      
       //System.out.println("widnth: " +p.width + " | height: " + p.height + " end "); 
       System.out.print("*"); 
      } 
      else System.out.print(" "); 
      xs+=10; 
     } 
     xs = 0; 
     ys+=10;    
    } 
    xs=0; 
    ys=0; 
    actiondelay();      
    //g.setColor(Color.white); 
    //g.drawRect(45, 95, 55, 105);   
} 

而且javax.swing.Timer(int delay, ActionListener listener)是循環線程的類型,所以如果你想每次執行一次它的作用不應該創建它的少數情況設置在delay。在你的地方,我會將代碼從actiondelay移動到init

0

我認爲你缺少你paint()方法g2d.dispose()的調用。我不確定這是否能解決問題,但嘗試它並不會有什麼壞處。

+0

nope,g2d.dispose()沒有幫助 – Yarh 2012-07-25 23:52:10

+0

hrmm ...不幸的是,我不在一臺我可以測試的機器上。我會盡可能仔細觀察。在此之前,我希望別人能提供進一步的幫助。 – 2012-07-25 23:55:39

0

問題在於您如何使用Timer。我不知道你爲什麼堅持在每個「繪畫」循環上創建一個新的定時器。

Java不會控制繪製週期。所以基本上,發生了什麼事情是,每次繪製組件時(可能是您的Timer事件之間的次數),您正在創建一個新的Timer,其中每5秒重複。這意味着你的跑步ň定時器,這將導致你很多有趣的東西...

相反,創建一個定時器:

@Override 
public void start() { 

    EventQueue.invokeLater(new Runnable() { 

     @Override 
     public void run() { 

      timer = new Timer(5000, new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 

        repaint(); 

       } 
      }); 

      timer.setInitialDelay(5000); 
      timer.setCoalesce(true); 
      timer.start(); // Repeats by default... 

     } 
    }); 

} 

不會改變在內線方法的任何事情。只是不要觸發更多的繪畫請求,最終會使EDT過載,並在不短的時間內將CPU運行到100%。