-2

Can someone please help me with the comment lines except the ones where it asks to delete things. Thank you!

public class ex1011c extends JApplet implements ActionListener
{
        // get rid of all winkbutton code

    JButton winkbutton = new JButton("Wink At You");
    boolean wink = false, first = true;
    Container c;

    public void init()
    {
        c = getContentPane();
        c.setLayout(new FlowLayout());
        c.setBackground(Color.blue);
        winkbutton.setForeground(Color.cyan);
        c.add(winkbutton);
        winkbutton.addActionListener(this);
    }

        // get rid of actionPerformed

    public void actionPerformed(ActionEvent e)
    {
        wink = !wink;
        repaint();
    }

    public void paint(Graphics g)
    {
        /* if first time, draw the face and non winking eye,
            set first to false */

        super.paint(g);
        g.setColor(Color.yellow);
        g.fillOval(50, 50, 100, 100);
        g.setColor(Color.black);
        g.fillOval(85, 80, 10, 20);

        /* cover just the eye that winks (if winking or not, but do not
         cover anything else), switch the wink boolean */

        // draw the full eye or winking eye

        if (wink)
            g.fillOval(105, 88, 10, 5);
        else
            g.fillOval(105, 80, 10, 20);

        // go to sleep for a second

        // call repaint
    }

        // override update to lesson flicker

}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Don't put logic in a paint event. Instead, use a timer and store the state. – SLaks Sep 23 '13 at 03:21
  • @raffian throughout the code there's comment lines on things to do. I'm stuck on them. I'd appreciate any help – user2805435 Sep 23 '13 at 03:23
  • 2
    *"I'm stuck on them."* What tutorials have you looked through? What have you tried? Where *exactly* are you stuck? Note that a good SO question should have one question with one or more answers to that one question. What you've posted amounts to a 'shopping list'. – Andrew Thompson Sep 23 '13 at 03:59

1 Answers1

2

Simple, don't sleep/pause/block or otherwise impede the Event Dispatching Thread.

The EDT, is amongst other things, responsible for process paint requests, anything to stops it from running (like Thread.sleep) will stop it from updating the screen.

Remember, just because you paint something to the Graphics context, doesn't mean it gets rendered to the output.

Instead, use a javax.swing.Timer

Take a look at Concurrency in Swing, Performing Custom Painting and Painting in AWT and Swing for more details.

I would also, strongly, encourage you against overriding any paint method of a top level container like JApplet. Instead, use something like JPanel and override it's paintComponent method instead.

Apart from potability, you gain the benefit of double buffering which top level containers don't have...

For example...

With simple example

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Blinky {

    public static void main(String[] args) {
        new Blinky();
    }

    public Blinky() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new BlinkPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class BlinkPane extends JPanel {

        private boolean wink;

        public BlinkPane() {
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    wink = !wink;
                    repaint();
                }
            });
            timer.start();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = (getWidth() - 10) / 2;
            int eyeHeight = 20;
            if (wink) {
                eyeHeight = 5;
            }
            int y = (getWidth() - eyeHeight) / 2;
            g.fillOval(x, y, 10, eyeHeight);
            g2d.dispose();
        }
    }
}
Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366