Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Basically, <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#getInsets%28%29" rel="nofollow"><code>JComponent#getInsets</code></a> will return the components border insets.</p> <p>This is the amount of space from each edge that you should not paint in, think of them as the margins for example...</p> <p>So with that information in hand, you <em>could</em> do something like...</p> <pre><code>// Random size, minimum size of 10, maximum size of 50 randomW = 10 + (Math.random() * 40); randomH = 10 + (Math.random() * 40); // Get the components insets Insets insets = getInsets(); // Calculate the available size by subtract the sum of the margins... int width = getWidth() - (insets.left + insets.right); int height = getHeight() - (insets.top + insets.bottom); // Calculate a postion that is at minimum, the top/left margin // but less than the available space minus the size of the shape... randomX = insets.left + (Math.random() * (width - randomW)); randomY = insets.top + (Math.random() * (height - randomH)); </code></pre> <p>Apologies, the above code comes from the following, runnable example...</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Ellipse2D; import javax.swing.BorderFactory; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.border.EmptyBorder; import javax.swing.border.LineBorder; public class RandomShape extends JPanel { public static void main(String[] args) { new RandomShape(); } public RandomShape() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private double randomX; private double randomY; private double randomW; private double randomH; public TestPane() { setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(10,10,10,10), new LineBorder(Color.BLACK))); Timer timer = new Timer(500, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { random(); } }); timer.start(); } public void random() { randomW = 10 + (Math.random() * 40); randomH = 10 + (Math.random() * 40); Insets insets = getInsets(); int width = getWidth() - (insets.left + insets.right); int height = getHeight() - (insets.top + insets.bottom); randomX = insets.left + (Math.random() * (width - randomW)); randomY = insets.top + (Math.random() * (height - randomH)); repaint(); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); Ellipse2D.Double circle = new Ellipse2D.Double(randomX, randomY, randomW, randomH); g2d.fill(circle); g2d.dispose(); } } } </code></pre> <p>So you'll need to splice the concept into your own code ;)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload