Note that there are some explanatory texts on larger screens.

plurals
  1. POJPanel listeners and threads issues
    primarykey
    data
    text
    <p>Here is the code for displaying circles with varying radius on a panel inside a frame with a given delay rate, but the code is showing the final output not the intermediate stages i.e., the circles are not appearing one by one but all the circles are coming at once as a final output. There may be some errors related to button action listeners and panel threads. The code is taking initial circle radius and the total number of iterations (the total number of circles to be displayed), radius of each next circle gets incremented by 10.</p> <pre><code>import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ControlCircle extends JFrame { private JButton jbtEnlarge = new JButton("Start"); private JButton jbtShrink = new JButton("Stop"); private CirclePanel canvas = new CirclePanel(); private int radius = 0; private int iter; public ControlCircle() { JPanel panel = new JPanel(); JPanel jp = new JPanel(); jp.setPreferredSize(new Dimension(300, 0)); panel.add(jbtEnlarge); panel.add(jbtShrink); this.add(jp, BorderLayout.WEST); this.add(canvas, BorderLayout.CENTER); this.add(panel, BorderLayout.SOUTH); final JTextField f1 = new JTextField(8),f2 = new JTextField(8); jp.setLayout(new FlowLayout(FlowLayout.RIGHT, 50, 30)); jp.add(new JLabel("Radius")); jp.add(f1); jp.add(new JLabel("Iteration")); jp.add(f2); f1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { radius = Integer.parseInt(new String(f1.getText())); } }); f2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { iter = Integer.parseInt(new String(f2.getText())); } }); jbtEnlarge.addActionListener(new EnlargeListener()); jbtShrink.addActionListener(new ShrinkListener()); } public static void main(String[] args) { JFrame frame = new ControlCircle(); frame.setTitle("ControlCircle"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(800, 600); frame.setLocationRelativeTo(null); frame.setVisible(true); } class EnlargeListener implements ActionListener { public void actionPerformed(ActionEvent e) { canvas.enlarge(); } } class ShrinkListener implements ActionListener { public void actionPerformed(ActionEvent e) { //canvas.shrink(); } } class CirclePanel extends JPanel { private int r = radius; public void enlarge() { //radius += 2; repaint(); } public void shrink() { radius -= 2; repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i &lt; iter; i++) { g.drawOval(getWidth() / 2 - r, getHeight() / 2 - r, 2 * r, 2 * r); try { Thread.sleep(100); } catch (Exception exp) { } r = r + 10; } r = 0; } } } </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.
 

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