2015-06-29 26 views
0

我試圖通過單行構建它來繪製圖形。這對我來說已經有一段時間了,但是在添加了幾個文本框和一個按鈕來控制某些參數後,它開始瘋狂地繪製我的圖形區域按鈕周圍的區域。我嘗試掃描源代碼,它看起來像Repaint()方法和方法的實際內容之間的差異,因爲即使我清空方法drawComponent()時該錯誤仍然會發生,但是每當我刪除調用repaint()。觸發重繪時,首先在框架上繪製按鈕及其周圍的小區域(重疊一些文本區域),然後在該區域上繪製實際的圖形。完整代碼段如下所列:錯誤的繪製方法繪製部分幀

基類角度:

package customUtilitys; 

public class Angle { 
    private double xRot,yRot,zRot; 
    public Angle(){} 
    public Angle(double x,double y,double z){ 
     xRot = x; 
     yRot = y; 
     zRot = z; 
     } 
    public Angle(Angle ang){ 
     setAngle(ang);} 
    public void setAngle(double x,double y, double z){ 
     while(x >= 360){ 
      x = x - 360; 
     } 
     while(y >= 360){ 
      y = y - 360; 
     } 
     while(z >= 360){ 
      z = z - 360; 
     } 
     xRot = x; 
     yRot = y; 
     zRot = z; 
    } 
    public void setAngle(Angle ang){ 
     xRot = ang.getXAngle(); 
     xRot = ang.getYAngle(); 
     zRot = ang.getZAngle(); 
    } 
    public double getXAngle(){ 
     return xRot; 
    } 
    public double getYAngle(){ 
     return yRot; 
    } 
    public double getZAngle(){ 
     return zRot; 
    } 
} 

基類3D點:

package customUtilitys; 

import java.awt.Point; 

public class Point3D extends Point { 
    private static final long serialVersionUID = 1242102585768332716L; 
    public int x,y,z; 
    public Point3D(){}; 
    public Point3D(double x,double y,double z){ 
     setLocation(x,y,z); 
    } 
    public Point3D(int x, int y,int z){ 
     setLocation(x,y,z); 
    } 
    public void setLocation(int x,int y,int z){ 
     this.x = x; 
     this.y = y; 
     this.z = z; 
    } 
    public void setLocation(double x,double y,double z){ 
     this.x = (int)Math.round(x); 
     this.y = (int)Math.round(y); 
     this.z = (int)Math.round(z); 
    } 
    public Point3D add(Point3D P){ 
     return new Point3D(x+P.x,y+P.y,z+P.z); 
    } 
    public Point3D rotate(Angle A){ 
     double xA = A.getXAngle(); 
     double yA = A.getYAngle(); 
     double zA = A.getZAngle(); 
     Matrix TransformationMatrixX = new Matrix(new double[][]{{1,0,0},{0,Math.cos(xA),-Math.sin(xA)},{0,Math.sin(xA),Math.cos(xA)}}); 
     Matrix TransformationMatrixY = new Matrix(new double[][]{{Math.cos(yA),0,-Math.sin(yA)},{0,1,0},{Math.sin(yA),0,Math.cos(yA)}}); 
     Matrix TransformationMatrixZ = new Matrix(new double[][]{{Math.cos(zA),-Math.sin(zA),0},{Math.sin(zA),Math.cos(zA),0},{0,0,1}}); 
     Matrix Pos = new Matrix(new double[][]{{x,y,z}}); 
     Pos = Pos.times(TransformationMatrixX); 
     Pos = Pos.times(TransformationMatrixY); 
     Pos = Pos.times(TransformationMatrixZ); 
     Point3D P = new Point3D(Pos.getData()[0][0],Pos.getData()[0][1],Pos.getData()[0][2]); 
     return P; 
    } 
    public Point transform(){ 
     int x2D = (int)Math.round(x/(z+4)); 
     int y2D = (int)Math.round(y/(z+4)); 
     Point P = new Point(x2D,y2D); 
     return P; 
    } 
} 

Importand代碼:該圖的

代碼(未完Cube):

package customUtilitys; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Point; 
import javax.swing.JPanel; 

@SuppressWarnings("serial") 
public class Cuboid extends JPanel { 
    Angle angle = new Angle(); 
    public Point3D A=new Point3D(),B=new Point3D(),C=new Point3D(),D=newPoint3D(),E=new Point3D(),F=new Point3D(),G=new Point3D(),H=new Point3D(); 
    public Point3D center = new Point3D(8,8,8); 
    public Cuboid(){} 
    public void setCorners(Point3D A,Point3D B,Point3D C,Point3D D,Point3D E,Point3D F,Point3D G,Point3D H){ 
     this.A=A; 
     this.B=B; 
     this.C=C; 
     this.D=D; 
     this.E=E; 
     this.F=F; 
     this.G=G; 
     this.H=H; 
    } 
    public void setCenter(Point3D P){ 
     center = P; 
    } 
    public void setAngle(double X,double Y, double Z) { 
     angle.setAngle(X,Y,Z); 
    } 

    //The paint method of the Cube. Possible source of the problem 

    public void paintComponent(Graphics g){ 
     Point a2 = center.add(A.rotate(angle)).transform(); 
     Point b2 = center.add(B.rotate(angle)).transform(); 
     Point c2 = center.add(C.rotate(angle)).transform(); 
     Point d2 = center.add(D.rotate(angle)).transform(); 
     Point e2 = center.add(E.rotate(angle)).transform(); 
     Point f2 = center.add(F.rotate(angle)).transform(); 
     Point g2 = center.add(G.rotate(angle)).transform(); 
     Point h2 = center.add(H.rotate(angle)).transform(); 
     g.setColor(Color.BLACK); 
     g.drawLine(a2.x*10,a2.y*10,b2.x*10,b2.y*10); 
     g.drawLine(b2.x*10,b2.y*10,c2.x*10,c2.y*10); 
     g.drawLine(c2.x*10,c2.y*10,d2.x*10,d2.y*10); 
     g.drawLine(d2.x*10,d2.y*10,a2.x*10,a2.y*10); 
     g.drawLine(e2.x*10,e2.y*10,f2.x*10,f2.y*10); 
     g.drawLine(f2.x*10,f2.y*10,g2.x*10,g2.y*10); 
     g.drawLine(g2.x*10,g2.y*10,h2.x*10,h2.y*10); 
     g.drawLine(h2.x*10,h2.y*10,e2.x*10,e2.y*10); 
     g.drawLine(a2.x*10,a2.y*10,e2.x*10,e2.y*10); 
     g.drawLine(b2.x*10,b2.y*10,f2.x*10,f2.y*10); 
     g.drawLine(c2.x*10,c2.y*10,g2.x*10,g2.y*10); 
     g.drawLine(d2.x*10,d2.y*10,h2.x*10,h2.y*10); 
    } 
} 

主要類:

package customUtilitys; 

import java.awt.EventQueue; 

import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JSpinner; 
import javax.swing.JTextField; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

@SuppressWarnings("serial") 
public class UtilityTest extends JFrame { 

    private JPanel contentPane; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        UtilityTest frame = new UtilityTest(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the frame. 
    */ 
    public UtilityTest() { 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setBounds(100, 100, 450, 354); 
     contentPane = new JPanel(); 
     contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
     setContentPane(contentPane); 
     contentPane.setLayout(null); 

     JPanel panel = new JPanel(); 
     panel.setBounds(5, 5, 424, 310); 
     contentPane.add(panel); 
     panel.setLayout(null); 

     //{...Shortened Code...} 

    //Definition of the Cube object and following repaint call. Hightest chance of problem here 

     final Cuboid Cube = new Cuboid(); 
     panel.add(Cube); 
     Cube.setBounds(50,50,160,160); 
     Cube.invalidate(); 
     Cube.repaint(); 

     JButton btnDraw = new JButton("Draw"); 
     btnDraw.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 
      //{...Shortened Code...} 
       Cube.invalidate(); 
       Cube.repaint(); 
      } 
     }); 
     btnDraw.setBounds(240, 241, 80, 23); 
     panel.add(btnDraw); 
    } 
} 
+2

哇靠!這是很多代碼!你可以給我們少一點代碼嗎?嘗試找出可能存在問題的地方,並讓我們的工作更輕鬆! [這裏有一些幫助](http://stackoverflow.com/help/mcve) –

+0

LayoutManagers也很有幫助,你可以爲自己節省很多工作。 – maraca

+0

我知道這很多。我其實只是想顯示最後2個代碼框,但添加了前2個,所以所有的自定義方法等都清楚了。問題的根源在最後2個框中,其中包含繪製圖形的代碼和繪圖方法。 – FRA32

回答

0

你打電話cube.invalidate()其無效的成分

公共無效無效()

使容器失效。
如果安裝在此容器上的LayoutManager是LayoutManager2接口的一個實例,那麼將調用LayoutManager2.invalidateLayout(Container)方法,以提供此容器作爲參數。
此後此方法將此容器標記爲無效,並使其祖先無效。

你必須調用revalidate()

Cube.revalidate();//not invalidate() 
+0

我嘗試了這些更改,但問題仍然存在。我的猜測是,這是重繪方法或類似的調用。 – FRA32