Note that there are some explanatory texts on larger screens.

plurals
  1. PODrawing a shape by clicking a JButton
    primarykey
    data
    text
    <p>I am currently trying to draw a figure by clicking a button. My problems occurs when I click the button and it does not show up in the panel, but I know it's drawing because it runs through the loop.</p> <p>The drawing will occur when I request the panel to draw it inside the constructor, but not inside the button, inside the constructor</p> <p>If i put the code in the method "stuff()" inside the constructor it will draw everything just fine.</p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; public class MainFrame{ public static void main(String[]args){ MainFrame f = new MainFrame(); } public JFrame frame = new JFrame(); public JPanel panel = new JPanel(new BorderLayout()); public MainFrame(){ JButton button1 = new JButton("Shweet Button"); button1.setBounds(185, 10, 130, 20); frame.setBounds(1680/4,1050/4,500, 500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.add(panel); panel.setBackground(Color.black); frame.setVisible(true); frame.getContentPane().setLayout(null); frame.add(button1); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stuff(); } }); } public void stuff(){ for(int i = 0;i&lt;1000;i++){ panel.add(new paintComponent()); panel.setBackground(Color.black); frame.repaint(); try { Thread.sleep(80); } catch (InterruptedException e1){e1.printStackTrace();} } } static class paintComponent extends JComponent{ public int options; public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.white); if(options == 0){ options = 1; g2.drawOval(50, (JFrame.HEIGHT/2)+100, 50, 50); g2.drawOval(60, (JFrame.HEIGHT/2)+110, 8, 8); g2.fillOval(60, (JFrame.HEIGHT/2)+110, 8, 8); g2.drawOval(80, (JFrame.HEIGHT/2)+110, 8, 8); g2.fillOval(80, (JFrame.HEIGHT/2)+110, 8, 8); g2.drawArc(65, (JFrame.HEIGHT/2)+130, 100, 0, 140, 30); g2.drawLine(75, (JFrame.HEIGHT/2)+150, 75, (JFrame.HEIGHT/2)+220); g2.drawLine(75, (JFrame.HEIGHT/2)+180, 100, (JFrame.HEIGHT/2)+160); g2.drawLine(75, (JFrame.HEIGHT/2)+180, 65, (JFrame.HEIGHT/2)+210); g2.drawLine(75, (JFrame.HEIGHT/2)+220, 50, (JFrame.HEIGHT/2)+260); g2.drawLine(75, (JFrame.HEIGHT/2)+220, 100, (JFrame.HEIGHT/2)+260); }else if(options == 1){ options = 0; g2.drawOval(50, (JFrame.HEIGHT/2)+100, 50, 50); g2.drawOval(60, (JFrame.HEIGHT/2)+110, 8, 8); g2.fillOval(60, (JFrame.HEIGHT/2)+110, 8, 8); g2.drawOval(80, (JFrame.HEIGHT/2)+110, 8, 8); g2.fillOval(80, (JFrame.HEIGHT/2)+110, 8, 8); g2.drawArc(65, (JFrame.HEIGHT/2)+130, 100, 0, 140, 30); g2.drawLine(75, (JFrame.HEIGHT/2)+150, 75, (JFrame.HEIGHT/2)+220); g2.drawLine(75, (JFrame.HEIGHT/2)+180, 100, (JFrame.HEIGHT/2)+180); g2.drawLine(75, (JFrame.HEIGHT/2)+180, 65, (JFrame.HEIGHT/2)+210); g2.drawLine(75, (JFrame.HEIGHT/2)+220, 50, (JFrame.HEIGHT/2)+260); g2.drawLine(75, (JFrame.HEIGHT/2)+220, 100, (JFrame.HEIGHT/2)+260); } } } } </code></pre>
    singulars
    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