Talk:Java Swings/Large Examples

import java.awt.*; import javax.swing.*;

public class Panels extends JApplet { private MyPanel[] panels; public class MyPanel extends JPanel implements Runnable { private int counter; private Color color; private boolean keepGoing; public MyPanel(int width,                    int height,                     int xlocation,                     int ylocation,                     Color color) { super(true); setSize(width,height); setLocation(xlocation,ylocation); this.color = color; counter = 50; keepGoing = true; new Thread(this).start; }         public void paint(Graphics g) { g.clearRect(0,0,getWidth,getHeight); super.paint(g); g.setColor(color); g.fillOval(getWidth/2-counter,getHeight/2-counter,2*counter,2*counter); g.setColor(Color.lightGray); g.fillOval(getWidth/2-counter+10,getHeight/2-counter+10,2*counter-20,2*counter-20); }         private void pause(double seconds) { try { Thread.sleep((int)(seconds*1000)); }                catch (InterruptedException ie) { }        }          public void stop { keepGoing = false; }         public void run { while (keepGoing) { System.out.println("Hello"); if (counter < 100) counter += 10; else if (counter == 100) keepGoing = false; else counter = 50; repaint; pause(0.1); }        }      }       public void stop { if (panels != null) for (int counter=0;counter<panels.length;counter++) if (panels[counter] != null) panels[counter].stop; }      public void init { Container container = getContentPane; container.setLayout(null); container.setBackground(Color.white); panels = new MyPanel[4]; Color[] colors = {Color.black,Color.red,Color.blue,Color.green}; for (int counter=0;counter<panels.length;counter++) container.add(panels[counter] = new MyPanel(getWidth/2,getHeight/2, getWidth/2*(counter/2), getHeight/2*(counter % 2), colors[counter])); }  }

i need help in understanding this code what does the line of code does ?

container.add(panels[counter] = new MyPanel(getWidth/2,getHeight/2, getWidth/2*(counter/2), getHeight/2*(counter % 2), colors[counter]));

Also 1 more Question:

whatg does thi do ?

g.fillOval(getWidth/2-counter,getHeight/2-counter,2*counter,2*counter); g.setColor(Color.lightGray); g.fillOval(getWidth/2-counter+10,getHeight/2-counter+10,2*counter-20,2*counter-20);

In general, the first bit of code you are asking about makes and adds a certain panel to the container and the second line draws a certain oval, and then another oval (which is gray). --209.6.80.30 04:11, 27 Mar 2005 (UTC)

I think it would be better to break up the program more and have more explanation to it. It would also be beneficial to differentiate between AWT, SWT, and Swing.

Also, GridBagLayouts are much better than the kind you are using.

Clean up examples
The examples in the Long Examples section need to be cleaned up, both due to bugs and poor programming practices. The concepts of the examples is sound however. Dark Pegasus 23:58, 14 October 2006 (UTC)
 * 1) Doesn't access swing components only from the event dispatch thread. This can cause a deadlock.
 * 2) Uses string literals for determining component placement instead of constants from the layout manager ("East", "West"... instead of BorderLayout.EAST, BorderLayout.WEST etc.)
 * 3) Uses one agrigate listener that has to check which buttons were pushed. There should be one listener to handle the number buttons, one for the operations, one for special buttons.
 * 4) Also, doing "if (src == button1)" is error prone. If button1 has changed since the listener was added that will return false.
 * 5) Formatting and naming convention doesn't follow Java standards. See Java Coding Conventions
 * 6) Two examples showing the same thing in functional and non-functional versions is overly verbose and confusing.
 * 7) String equality is done using var == "literal" instead of "literal".equals(var), this "works" only because all literals are inlined.
 * 8) "if (OperatorKey == true)" is redundant, should be "if(OperatorKey)"
 * 9) Status is set to literal values instead of being set to constant values. Set to "VALID", "FIRST", "ERROR" et al.

Applied changes. Dark Pegasus 04:01, 16 October 2006 (UTC)

Reorganized
Along with splitting up the swing section into more subsections, I also moved the above talk from the first page to this page, as this is now the page that it pertains to. --Ceclauson (talk) 01:08, 10 March 2008 (UTC)

My version of the Calculator
My version of the Swing Calculator is on https://github.com/AlexanderMisel/SimpleCalculator I fixed some small bugs on the code shown in the example. Alexander Misel (discuss • contribs) 13:33, 20 November 2014 (UTC)