I am working on a uni project that is designed to create a face in a bone using 2 graphic shapes. I have everything, but I have a problem: I want my shape to resize when I resize the window, and not just stay still so that it stays in the middle.
I thought that I could set the position so that it was in the center, but did not work. I'm not sure, but should I write the coordinates so that the changes change using the window? Any help on both issues would be great.
GUI customization code
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JComponent; import javax.swing.JFrame; public class DiceSimulator { public static void main(String[] args) { JFrame frame = new JFrame("DiceSimulator"); frame.setVisible(true); frame.setSize(400, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); draw object = new draw(); frame.add(object); frame.setLocationRelativeTo(null); object.drawing(); } }
Color code
import javax.swing.*; import java.awt.*; //import java.util.Random; public class draw extends JComponent { public void drawing() { repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); //Random1 random = new Random1(); Graphics2D g2 = (Graphics2D) g; g.setColor(Color.BLACK); Rectangle box = new Rectangle(115, 60, 150, 150); g2.fill(box); g.setColor(Color.WHITE); g.fillOval(145, 75, 30, 30); g.setColor(Color.WHITE); g.fillOval(205, 75, 30, 30); g.setColor(Color.WHITE); g.fillOval(145, 115, 30, 30); g.setColor(Color.WHITE); g.fillOval(205, 115, 30, 30); g.setColor(Color.WHITE); g.fillOval(145, 155, 30, 30); g.setColor(Color.WHITE); g.fillOval(205, 155, 30, 30); } }
source share