JAVA - 创建可缩放到 window 大小的基本 2D 形状
JAVA - creating basic 2D shapes that scale to window size
我正在尝试通过阅读和做我在网上找到的教科书中的示例来学习 Java。我能够相当轻松地完成书中的示例,但想更进一步。当我为一个名为 'concentric circles' 的程序编写代码时,我注意到我的圈子没有缩放到 window 的大小,我认为它看起来很糟糕(如果我也制作 window小圆圈不缩放,你只能看到一小块圆圈。我调整了我的代码,它似乎缩放了一点,但当我让我的 window 太小或太大时看起来很糟糕。我想要无论 window 的大小如何,我的 12 个圆圈都适合 window 的大小。我觉得我已经很接近了,但似乎无法绕过它。任何和所有的帮助都是很大的感谢,提前致谢!
import java.awt.Graphics;
import javax.swing.JPanel;
public class Circles extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int x = width / 12;
int y = height / 12;
int buffersize = 0;
for (int i = 0; i < 12; i++)
{
g.drawOval(width / 2 - buffersize, height / 2 - buffersize, x + buffersize * 2, y + buffersize * 2);
buffersize += 10;
}
}
}
Driver class
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class ConcentricCirclesTest
{
public static void main(String[] args)
{
//create a panel that will contain our drawing
Circles panel = new Circles();
//create a new frame to house the panel
JFrame application = new JFrame();
//set the frame to exit when closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(350, 350);
application.setVisible(true);
}
}
如果你想让你的圆是圆形的(不是椭圆形的)那么首先你需要知道哪个更窄,宽度还是高度。使用 if...else
的语法快捷方式你会想要这样的东西:
int smallest = width < height ? width : height;
接下来您需要考虑您的圈子之间的距离。如果您只有 100 个像素可以绘制它们,那么尝试绘制 12 个变大(或变小)10 像素的圆圈是没有用的。同样,如果您有 500 个像素 space。所以像:
float spacing = smallest / 12f;
请注意,将一个 int
与另一个相除可能会得到意想不到的结果。
然后在你的 for
循环中:
g.drawOval(0 + ((spacing / 2) * i), 0 + ((spacing / 2) * i),
smallest - ((spacing / 2) * i), smallest - ((spacing / 2) * i));
我希望这是明确的。基本上,每次迭代,边界矩形(或正方形)都会变小spacing
。
好的,这将需要调整,所以它不会在左下角绘制,我认为最后一个圆圈应该是 0 x 0,但我希望这对您有所帮助。祝你好运!
我正在尝试通过阅读和做我在网上找到的教科书中的示例来学习 Java。我能够相当轻松地完成书中的示例,但想更进一步。当我为一个名为 'concentric circles' 的程序编写代码时,我注意到我的圈子没有缩放到 window 的大小,我认为它看起来很糟糕(如果我也制作 window小圆圈不缩放,你只能看到一小块圆圈。我调整了我的代码,它似乎缩放了一点,但当我让我的 window 太小或太大时看起来很糟糕。我想要无论 window 的大小如何,我的 12 个圆圈都适合 window 的大小。我觉得我已经很接近了,但似乎无法绕过它。任何和所有的帮助都是很大的感谢,提前致谢!
import java.awt.Graphics;
import javax.swing.JPanel;
public class Circles extends JPanel
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int x = width / 12;
int y = height / 12;
int buffersize = 0;
for (int i = 0; i < 12; i++)
{
g.drawOval(width / 2 - buffersize, height / 2 - buffersize, x + buffersize * 2, y + buffersize * 2);
buffersize += 10;
}
}
}
Driver class
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
*
* @author
*/
public class ConcentricCirclesTest
{
public static void main(String[] args)
{
//create a panel that will contain our drawing
Circles panel = new Circles();
//create a new frame to house the panel
JFrame application = new JFrame();
//set the frame to exit when closed
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(350, 350);
application.setVisible(true);
}
}
如果你想让你的圆是圆形的(不是椭圆形的)那么首先你需要知道哪个更窄,宽度还是高度。使用 if...else
的语法快捷方式你会想要这样的东西:
int smallest = width < height ? width : height;
接下来您需要考虑您的圈子之间的距离。如果您只有 100 个像素可以绘制它们,那么尝试绘制 12 个变大(或变小)10 像素的圆圈是没有用的。同样,如果您有 500 个像素 space。所以像:
float spacing = smallest / 12f;
请注意,将一个 int
与另一个相除可能会得到意想不到的结果。
然后在你的 for
循环中:
g.drawOval(0 + ((spacing / 2) * i), 0 + ((spacing / 2) * i),
smallest - ((spacing / 2) * i), smallest - ((spacing / 2) * i));
我希望这是明确的。基本上,每次迭代,边界矩形(或正方形)都会变小spacing
。
好的,这将需要调整,所以它不会在左下角绘制,我认为最后一个圆圈应该是 0 x 0,但我希望这对您有所帮助。祝你好运!