我正在尝试在中间画线。我尝试了不同的坐标
I am trying to draw the line in the middle. I tried different coordinates
这是我的 paintComponent,它包含坐标
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawLine(-100,0,500,0);
g2.drawLine(141,-500,141,500);
g2.translate(getWidth()/2.0, getHeight()/2.0);
g2.scale(1,-1);
g2.rotate(45*Math.PI/180);
Rectangle2D r = new Rectangle2D.Double(0,0,100,100);
g2.fill(r);
有点作弊,但是...
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g2.drawLine(0, getHeight() / 2, getWidth(), getHeight()/ 2);
g2.translate(getWidth() / 2.0, (getHeight() / 2.0));
g2.scale(1, -1);
g2.rotate(45 * Math.PI / 180);
Rectangle2D r = new Rectangle2D.Double(-50, -50, 100, 100);
g2.fill(r);
g2.dispose();
}
基本上,原点现在是屏幕的中心,所以为了在原点周围绘制矩形 "centered",您需要相应地调整 x/y
现在,您还可以通过 50x50
来调整原点,但随后您需要将 Graphics
上下文围绕其旋转的锚点更改为框的中心
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g2.drawLine(0, getHeight() / 2, getWidth(), getHeight()/ 2);
g2.translate((getWidth() / 2.0) - 50, (getHeight() / 2.0) - 50);
g2.scale(1, -1);
g2.rotate(45 * Math.PI / 180, 50, 50);
Rectangle2D r = new Rectangle2D.Double(0, 0, 100, 100);
g2.fill(r);
g2.dispose();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.drawLine(-100,0,500,0);
g2.drawLine(141,-500,141,500);
g2.translate(getWidth()/2.0, getHeight()/2.0);
g2.scale(1,-1);
g2.rotate(45*Math.PI/180);
Rectangle2D r = new Rectangle2D.Double(0,0,100,100);
g2.fill(r);
有点作弊,但是...
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g2.drawLine(0, getHeight() / 2, getWidth(), getHeight()/ 2);
g2.translate(getWidth() / 2.0, (getHeight() / 2.0));
g2.scale(1, -1);
g2.rotate(45 * Math.PI / 180);
Rectangle2D r = new Rectangle2D.Double(-50, -50, 100, 100);
g2.fill(r);
g2.dispose();
}
基本上,原点现在是屏幕的中心,所以为了在原点周围绘制矩形 "centered",您需要相应地调整 x/y
现在,您还可以通过 50x50
来调整原点,但随后您需要将 Graphics
上下文围绕其旋转的锚点更改为框的中心
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
g2.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());
g2.drawLine(0, getHeight() / 2, getWidth(), getHeight()/ 2);
g2.translate((getWidth() / 2.0) - 50, (getHeight() / 2.0) - 50);
g2.scale(1, -1);
g2.rotate(45 * Math.PI / 180, 50, 50);
Rectangle2D r = new Rectangle2D.Double(0, 0, 100, 100);
g2.fill(r);
g2.dispose();
}