class MyDrawPanel extends JPanel { int x = 20; public void paintComponent(Graphics g) { for (int i = 0; i < 5; i++) { g.setColor(Color.black); g.fillRect(x, 20, 100, 100); x += 120; } } } public class MyDraw { MyDrawPanel panel = new MyDrawPanel(); JFrame frame = new JFrame(); public void createGuiPanel() { frame.getContentPane().add(BorderLayout.CENTER, panel); frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } public static void main(String[] args) { MyDraw draw = new MyDraw(); draw.createGuiPanel(); } } - 2How should she "act"? - Olexiy Morenets
- as an argument for the method fillRect - ALEXEY
- 2What makes you think that does not work? Add output to the console with its values and you will see that everything is "valid." What behavior do you expect? - Olexiy Morenets
- I added a class, when I start it in the console, I expect to see the squares, but there are none. But if you move the declaration of the variable x to the PaintComponent method, then everything works. - ALEXEY
- How can it work there? PaintComponent is not called anywhere. - Enikeyschik
|