Graphics2D的PostModern [英] Graphics2D does PostModern
问题描述
在下面的代码中,将填充
为 draw
会产生意外的结果。附加的图片演示了由绘制
红色和绿色矩形造成的令人不满意但颇受赞赏的后现代效果。
( - 2,-2)
和(+ 2,+2)
。 这是一个独立的例子。尝试将任何绘制
s更改为 fill
,反之亦然,以实现您自己的自定义绘画Oracle的办公室。
我正在使用Java SE 7,JDK 1.7.0_21!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class G {
public static void main(String [] args){
JFrame frame = new JFrame(G.class.getCanonicalName());
JComponent component = new JComponent(){
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D)g.create();
AffineTransform xform = new AffineTransform(getWidth()/ 4,0,0, - getHeight()/ 4,getWidth()/ 2,getHeight()/ 2);
g2.setTransform(xform);
paint2D(g2);
g2.dispose();
}
保护void paint2D(Graphics2D g2){
g2.setColor(Color.RED);
g2.draw(new Rectangle2D.Double(0,0,1,1));
g2.setColor(Color.GREEN);
g2.draw(new Rectangle2D.Double(-1,-1,1,1));
g2.setColor(Color.BLUE);
g2.fill(new Rectangle2D.Double(-1,0,1,1));
g2.setColor(Color.YELLOW);
g2.fill(new Rectangle2D.Double(0,-1,1,1));
}
};
frame.setLayout(new BorderLayout());
frame.add(component,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,400);
frame.setVisible(true);
}
}
这是1单位默认笔画设置。我将坐标空间从每个维度的 -20
增加到 +20
,并绘制了 10
单位宽度和高矩形确认了问题,如下图所示。
In the following code changing fill
to draw
produces unexpected results. The attached image demonstrates the undesired but much appreciated postmodern effect caused by draw
ing the red and green rectangles.
The affine transform should not be part of the problem, but as Holmes said, once you rule out all other possibilities ... So, I will explain the transform. I solved a bunch of equations to figure out how to make the window show a cartesian coordinate system with (-2, -2)
in the lower left and (+2, +2)
in the upper right.
This is a self-contained example. Try changing any of the draw
s to a fill
or vice versa to achieve your own custom art that you can frame in the Oracle office.
I am using Java SE 7, JDK 1.7.0_21!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class G {
public static void main (String [] args) {
JFrame frame = new JFrame(G.class.getCanonicalName());
JComponent component = new JComponent() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
AffineTransform xform = new AffineTransform(getWidth() / 4, 0, 0, - getHeight() / 4, getWidth() / 2, getHeight() / 2);
g2.setTransform(xform);
paint2D(g2);
g2.dispose();
}
protected void paint2D (Graphics2D g2) {
g2.setColor(Color.RED);
g2.draw(new Rectangle2D.Double( 0, 0, 1, 1));
g2.setColor(Color.GREEN);
g2.draw(new Rectangle2D.Double(-1, -1, 1, 1));
g2.setColor(Color.BLUE);
g2.fill(new Rectangle2D.Double(-1, 0, 1, 1));
g2.setColor(Color.YELLOW);
g2.fill(new Rectangle2D.Double( 0, -1, 1, 1));
}
};
frame.setLayout(new BorderLayout());
frame.add(component, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
it is the 1 unit default stroke setting. i increased the coordinate space from -20
to +20
in each dimension and drew 10
unit wide and high rectangles which confirmed the problem as the image below shows.
这篇关于Graphics2D的PostModern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!