/ /これを描く簡単な方法は? - java

これを簡単に描く方法は? - java

私は十字架の型を描こうとしていますJavaグラフィックスメソッド。私はこれを書いたが、それはやりすぎだと思うし、かなり単純化することができるような気がする。私はそれがどのように見えるのかの写真を含めます。

どのように私はこれを簡単にすることができますか?

    graphics.setColor(mainColor);
graphics.drawRect(Mouse.getPos().x - 13, Mouse.getPos().y - 13, 27, 27); // Rectangle stroke.
graphics.drawRect(Mouse.getPos().x, Mouse.getPos().y - 512, 1, 500);     // Top y axis stroke.
graphics.drawRect(Mouse.getPos().x, Mouse.getPos().y + 13, 1, 500);      // Bottom y axis stroke.
graphics.drawRect(Mouse.getPos().x + 13, Mouse.getPos().y, 800, 1);      // Right x axis stroke.
graphics.drawRect(Mouse.getPos().x - 812, Mouse.getPos().y, 800, 1);     // left x axis stroke.
graphics.fillOval(Mouse.getPos().x - 3, Mouse.getPos().y - 3, 7, 7);     // Center dot stroke.
graphics.setColor(offColor);
graphics.drawRect(Mouse.getPos().x - 12, Mouse.getPos().y - 12, 25, 25); // Rectangle.
graphics.drawRect(Mouse.getPos().x, Mouse.getPos().y - 512, 0, 500);     // Top y axis line.
graphics.drawRect(Mouse.getPos().x, Mouse.getPos().y + 13, 0, 500);      // Bottom y axis line.
graphics.drawRect(Mouse.getPos().x + 13, Mouse.getPos().y, 800, 0);      // Right x axis line.
graphics.drawRect(Mouse.getPos().x - 812, Mouse.getPos().y, 800, 0);     // left x axis line.
graphics.fillOval(Mouse.getPos().x - 2, Mouse.getPos().y - 2, 5, 5);     // Center dot.

これは、それがどのように見えるのか、そして、それはどのように見えるのでしょう。 ここに画像の説明を入力

回答:

回答№1は1

1つの単純化は、 Graphics#drawRect 方法。

private static void drawRect (Graphics g, int x, int y, int width, int height)
{
g.drawRect(Mouse.getPos().x + x, Mouse.getPos().y + y, width, height);
}

次に、この呼び出しコードは

graphics.drawRect(Mouse.getPos().x - 13, Mouse.getPos().y - 13, 27, 27); // Rectangle stroke.
graphics.drawRect(Mouse.getPos().x, Mouse.getPos().y - 512, 1, 500);     // Top y axis stroke.
graphics.drawRect(Mouse.getPos().x, Mouse.getPos().y + 13, 1, 500);      // Bottom y axis stroke.
graphics.drawRect(Mouse.getPos().x + 13, Mouse.getPos().y, 800, 1);      // Right x axis stroke.
graphics.drawRect(Mouse.getPos().x - 812, Mouse.getPos().y, 800, 1);     // left x axis stroke.

drawRect(graphics, -13, -13, 27, 27);  // Rectangle stroke.
drawRect(graphics, 0, -512, 1, 500);   // Top y axis stroke.
drawRect(graphics, 0, 13, 1, 500);     // Bottom y axis stroke.
drawRect(graphics, 13, 0, 800, 1);     // Right x axis stroke.
drawRect(graphics, -812, 0, 800, 1);   // left x axis stroke.