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

public class twobuttons
{
    int x=70;
    int y=70;

    public static void main(String args[])
    {
        twobuttons gui =new twobuttons();
        gui.go();
    }

    public void go()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mydraw drawpanel = new mydraw();

        frame.getContentPane().add(drawpanel);
        frame.setSize(300,300);
        frame.setVisible(true);

        for(int i=0;i<130;i++)
        {
            x++;
            y++;

            drawpanel.repaint();
            try
            {
                Thread.sleep(50);
            }
            catch(Exception ex)
            {

            }
        }

    }
    class mydraw extends JPanel
    {

        public void paintconponent(Graphics g)
        {
            g.setColor(Color.green);
            g.fillOval(x, y, 40, 40);
        }
    }
}

this is a code from the Head First Java,chapter 12,p384(I am reading the Chinese edition ,maybe it's not in the same page in the English edition.)If successful,there should be a dot running from the left corner to the right corner,however,I can't see anything in the window.

coqer
  • 307
  • 1
  • 9

3 Answers3

5

You have a typo in the painting class (paintconponent => paintComponent). So you were not overriding the behavior of the JComponent super class. Try this:

class mydraw extends JPanel
{

    @Override
    public void paintComponent(Graphics g)
    {
        g.setColor(Color.green);
        g.fillOval(x, y, 40, 40);
    }
}
assylias
  • 321,522
  • 82
  • 660
  • 783
4

If this snippet is literally copy-pasted from the book, I would throw the book away

  • Swing operations should happen on the EDT, so the main method needs an EventQueue.invokeLater (or similar)
  • Calling Thread.sleep on the same Thread where the painting is performed will not work.

StackOverflow is filled with questions why Thread.sleep does not work on the EDT, and possible work-arounds. In this case, I would use a Swing Timer to move the dot and trigger the repaint

Robin
  • 36,233
  • 5
  • 47
  • 99
  • The real book is well-regarded and the [real example](http://books.google.com/books?id=5VTBuvfZDyoC&pg=PA379&lpg=PA379&dq=Head+First+Java+two+buttons+code&source=bl&ots=aplyIGdRvS&hl=en&sa=X&ei=fj1iT8uYNOja0QGgt43LCA&sqi=2&ved=0CEkQ6AEwBA#v=onepage&q=Head%20First%20Java%20two%20buttons%20code&f=false) looks better. – trashgod Mar 15 '12 at 19:11
  • @trashgod That looks better indeed. Never read the book myself so my comments was only based on the snippet posted in the question – Robin Mar 15 '12 at 20:07
2

1) never use Thread.sleep(int) during EDT, this code line caused flickering or freeze of Swing GUI, example about freeze here

2) Swing GUI should be started in Initial Threads

3) use Swing Timer for animations or pausing of Swing GUI

4) learn Java Naming Convention

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319