AffineTransform 翻译不同的图形对象
AffineTransform translate differnet Graphics Object
我用这个方法调用我的抽奖类:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
DrawGradient gradient = new DrawGradient();
gradient.draw(g);
DrawCoordinateSystem coor = new DrawCoordinateSystem();
coor.draw(g);
}
我尝试变换 DrawGradient class 中的对象,但由于某种原因 class DrawCoordinateSystem 中的 Graphics 对象被变换了。为什么?
绘制坐标系class:
public class DrawCoordinateSystem {
public DrawCoordinateSystem() {
// TODO Auto-generated constructor stub
}
public void draw(Graphics g1){
Color customcolor = new Color(210,191,245);
int x0 = 15, y0 = 15;
int Xpoints[] = {x0+45, x0+40, x0+40};
int Ypoints[] = {y0, y0-5, y0+5};
int Xpoints1[] = {x0, x0+5, x0-5};
int Ypoints1[] = {y0+45, y0+40, y0+40};
g1.setColor(customcolor);
g1.drawPolygon(Xpoints,Ypoints, 3);
g1.fillPolygon(Xpoints,Ypoints, 3);
g1.drawPolygon(Xpoints1,Ypoints1, 3);
g1.fillPolygon(Xpoints1,Ypoints1, 3);
g1.drawLine(x0, y0, x0+40, y0);
g1.drawLine(x0, y0, x0, y0+40);
g1.setFont(new Font("TimesRoman", Font.PLAIN, 12));
g1.drawString("x", 52, 30);
g1.setFont(new Font("TimesRoman", Font.PLAIN, 12));
g1.drawString("y", 23, 57);
}
}
绘制渐变class:
public class DrawGradient {
public DrawGradient() {
}
private Color c1;
private Color c2;
public void draw( Graphics g ) {
Graphics2D g2d1 = (Graphics2D) g;
GradientPaint gp = new GradientPaint(0, 0, c2, 0, 100, c1);
g2d1.setPaint(gp);
Rectangle reckt = new Rectangle(0,0,100,100);
g2d1.fill(reckt);
AffineTransform move = new AffineTransform();
move.translate(200, 200);
g2d1.setTransform(move);
}
}
转换是复合的,因此,应用转换后,您需要在完成后将其反转。
现在,您可以手动执行此操作,或者您可以事先创建 Graphics
上下文的副本,并在完成后将其丢弃。这可以防止您对副本的属性所做的任何更改对原件进行(但您绘制的内容仍然存在)
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color c1 = new Color(0, 0, 255);
Color c2 = new Color(0, 255, 255);
GradientPaint gp = new GradientPaint(0, 0, c1, 0, 100, c2);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setPaint(gp);
g2d.setTransform(AffineTransform.getTranslateInstance(100, 100));
g2d.fill(new Rectangle(0, 0, 100, 100));
g2d.dispose();
g2d = (Graphics2D) g.create();
g2d.setPaint(gp);
g2d.fill(new Rectangle(0, 0, 100, 100));
g2d.dispose();
}
}
在你的情况下,我会在你将 Graphics
上下文传递给其他方法之前创建一个副本,例如
Graphics2D g2d =(Graphics2D)g.create();
DrawGradient gradient = new DrawGradient();
gradient.draw(g2d);
g2d.dispose();
g2d =(Graphics2D)g.create();
DrawCoordinateSystem coor = new DrawCoordinateSystem();
coor.draw(g2d);
g2d.dispose();
这样你就可以控制了。如果可以避免,我建议不要在 paintComponent
方法中创建新对象,特别是如果这些对象并没有真正从一个绘制周期变为另一个绘制周期,因为 paintComponent
可以被调用多次接二连三
我用这个方法调用我的抽奖类:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
DrawGradient gradient = new DrawGradient();
gradient.draw(g);
DrawCoordinateSystem coor = new DrawCoordinateSystem();
coor.draw(g);
}
我尝试变换 DrawGradient class 中的对象,但由于某种原因 class DrawCoordinateSystem 中的 Graphics 对象被变换了。为什么?
绘制坐标系class:
public class DrawCoordinateSystem {
public DrawCoordinateSystem() {
// TODO Auto-generated constructor stub
}
public void draw(Graphics g1){
Color customcolor = new Color(210,191,245);
int x0 = 15, y0 = 15;
int Xpoints[] = {x0+45, x0+40, x0+40};
int Ypoints[] = {y0, y0-5, y0+5};
int Xpoints1[] = {x0, x0+5, x0-5};
int Ypoints1[] = {y0+45, y0+40, y0+40};
g1.setColor(customcolor);
g1.drawPolygon(Xpoints,Ypoints, 3);
g1.fillPolygon(Xpoints,Ypoints, 3);
g1.drawPolygon(Xpoints1,Ypoints1, 3);
g1.fillPolygon(Xpoints1,Ypoints1, 3);
g1.drawLine(x0, y0, x0+40, y0);
g1.drawLine(x0, y0, x0, y0+40);
g1.setFont(new Font("TimesRoman", Font.PLAIN, 12));
g1.drawString("x", 52, 30);
g1.setFont(new Font("TimesRoman", Font.PLAIN, 12));
g1.drawString("y", 23, 57);
}
}
绘制渐变class:
public class DrawGradient {
public DrawGradient() {
}
private Color c1;
private Color c2;
public void draw( Graphics g ) {
Graphics2D g2d1 = (Graphics2D) g;
GradientPaint gp = new GradientPaint(0, 0, c2, 0, 100, c1);
g2d1.setPaint(gp);
Rectangle reckt = new Rectangle(0,0,100,100);
g2d1.fill(reckt);
AffineTransform move = new AffineTransform();
move.translate(200, 200);
g2d1.setTransform(move);
}
}
转换是复合的,因此,应用转换后,您需要在完成后将其反转。
现在,您可以手动执行此操作,或者您可以事先创建 Graphics
上下文的副本,并在完成后将其丢弃。这可以防止您对副本的属性所做的任何更改对原件进行(但您绘制的内容仍然存在)
public class TestPane extends JPanel {
public TestPane() {
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color c1 = new Color(0, 0, 255);
Color c2 = new Color(0, 255, 255);
GradientPaint gp = new GradientPaint(0, 0, c1, 0, 100, c2);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setPaint(gp);
g2d.setTransform(AffineTransform.getTranslateInstance(100, 100));
g2d.fill(new Rectangle(0, 0, 100, 100));
g2d.dispose();
g2d = (Graphics2D) g.create();
g2d.setPaint(gp);
g2d.fill(new Rectangle(0, 0, 100, 100));
g2d.dispose();
}
}
在你的情况下,我会在你将 Graphics
上下文传递给其他方法之前创建一个副本,例如
Graphics2D g2d =(Graphics2D)g.create();
DrawGradient gradient = new DrawGradient();
gradient.draw(g2d);
g2d.dispose();
g2d =(Graphics2D)g.create();
DrawCoordinateSystem coor = new DrawCoordinateSystem();
coor.draw(g2d);
g2d.dispose();
这样你就可以控制了。如果可以避免,我建议不要在 paintComponent
方法中创建新对象,特别是如果这些对象并没有真正从一个绘制周期变为另一个绘制周期,因为 paintComponent
可以被调用多次接二连三