但我的问题是我不知道如何在Java中创建这样的比率。具体来说,我希望能够实现这一点:
“...一个直立的......十字架,其四个相等长度的臂比它们的宽度长六分之一”,因此从垂直臂到最短边缘的距离始终是一个臂的宽度。
这是我的代码:
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RedCross extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g); // Call JPanel's paintComponent method
// to paint the background
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int xoo = (getWidth() * 3) / 16;
int yoo = (getHeight() * 3) / 16;
if (getHeight() < getWidth())
{
g.setColor(Color.RED);
g.fillRect(xCenter - (yoo / 2), yoo, yoo, getHeight() - 2 * yoo);
g.fillRect(xCenter - (5 * yoo) / 3, 13 * yoo / 6, getHeight() - 2 * yoo, yoo);
}
else
{
g.setColor(Color.RED);
g.fillRect(13 * xoo / 6, yCenter - (5 * xoo) / 3, xoo, getWidth() - 2 * xoo);
g.fillRect(xoo, yCenter - (xoo / 2), getWidth() - 2 * xoo, xoo);
}
}
public static void main(String[] args)
{
JFrame window = new JFrame("Red Cross");
window.setBounds(300, 300, 200, 200);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
RedCross panel = new RedCross();
panel.setBackground(Color.WHITE);
Container c = window.getContentPane();
c.add(panel);
window.setVisible(true);
}
}
感谢任何帮助:)