我试图通过用单行构造它来绘制一个图形。这对我来说已经有一段时间了,但是在添加了一些文本字段和一个按钮来控制一些参数后,它开始变得疯狂并在我的图形区域上绘制按钮周围的区域。我尝试扫描源代码,看起来它是BETWEEN的Repaint()方法和方法的实际内容,因为即使我清空方法drawComponent(),bug也会继续发生,但每当我删除它时都不会发生调用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;
}
}
导入和代码:
绘制的图形代码(未完成的立方体):
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);
}
}
答案 0 :(得分:0)
您正在调用使cube.invalidate()
无效的组件
public void invalidate()
使容器无效 如果此容器上安装的LayoutManager是LayoutManager2接口的实例,则会调用LayoutManager2.invalidateLayout(Container)方法,并将此Container作为参数。
之后,此方法将此容器标记为无效,并使其祖先无效。
您必须致电revalidate()
Cube.revalidate();//not invalidate()