1

Basically I have a JApplet that tries to draw with the graphics (ie, g.drawOval(10,10,100,100) and also include JCompotents (ie. JButton). What happens is that the repaint can get really wacky.

Sometimes the graphics will draw over widgets or vis-versa. It's not reliable and leads to unpredictable behavior.

(Button also always be on top of those graphics)

I've played around with it trying to override or manually draw components, changing orders, etc, but think I'm missing something very fundamental here. Anyone have a template or the correct way to use both g.drawXXX and JCompotents?

user1516346
  • 47
  • 1
  • 6
  • 1
    You need to show your code where you draw and any other pertinent code. Else we have no idea what you may be doing wrong. Also, be sure never to draw directly in the JApplet but rather in a JPanel or in its contentPane (which is a JPanel). Make sure to draw in this JPanel's paintComponent(...) method. – Hovercraft Full Of Eels Jul 11 '12 at 01:10
  • 1
    Check the [usual suspects](http://stackoverflow.com/a/11389042/230513) and compare your code to this [`AnimationTest`](http://stackoverflow.com/a/3256941/230513). – trashgod Jul 11 '12 at 01:17
  • 1
    @user1516346: sure there are *many* examples on line and on this site, some I've written myself. Keep searching! And your problem simply is you're not following any of my suggestions in my first post. You're still drawing directly in the applet. – Hovercraft Full Of Eels Jul 11 '12 at 02:06
  • *"Image explains it better:"* Hmm.. An image generally paints a thousand words, but that one might have been summed up with "but the drawing appears on top of the components". – Andrew Thompson Jul 11 '12 at 02:11
  • @HovercraftFullOfEels , link? I've been searching for hours, but since you have many examples on hand, it would be much appreciated! :) – user1516346 Jul 11 '12 at 02:12
  • One link is this one. See answer below. But more importantly, read the Oracle Swing painting tutorials. It's spelled out there as well. – Hovercraft Full Of Eels Jul 11 '12 at 02:13
  • I changed it to this, and still has the problem: getContentPane().getGraphics().drawImage(images[state], 300, 10, this); – user1516346 Jul 11 '12 at 02:14
  • NEVER use getGraphics() on a component! That Graphics object won't persist and you're flirting with a NullPointerException or worse. – Hovercraft Full Of Eels Jul 11 '12 at 02:15

1 Answers1

3

Again, just follow what I recommended,

be sure never to draw directly in the JApplet but rather in a JPanel or in its contentPane (which is a JPanel). Make sure to draw in this JPanel's paintComponent(...) method.

and it works:

import java.awt.*;
import java.awt.event.*;
import java.lang.reflect.InvocationTargetException;

import javax.swing.*;

public class Test2 extends JApplet {


   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               Test2BPanel panel = new Test2BPanel();
               setContentPane(panel);
            }
         });
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      } catch (InterruptedException e) {
         e.printStackTrace();
      }

   }


}

class Test2BPanel extends JPanel {
   private String[] backgroundImageFileNames = { "test", "test", "test" };

   private JButton refreshButton;
   private JComboBox backgroundList;

   public Test2BPanel() {

      setBackground(Color.white);

      setLayout(new FlowLayout());

      refreshButton = new JButton("replant new forest");
      refreshButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {

         }

      });
      add(refreshButton);

      backgroundList = new JComboBox(backgroundImageFileNames);
      backgroundList.setSelectedIndex(2);
      add(backgroundList);
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      paintIt(g);
   }

   public void paintIt(Graphics g) {
      for (int i = 0; i < 200; i++) {
         for (int j = 0; j < 200; j++) {
            g.setColor(Color.red);
            g.drawOval(10 * i, j, 10, 10);
         }
      }
   }
}

Also, please check the Swing painting tutorials including the Basic Painting Tutorial and the Advanced Painting Tutorial.

For a great book on this and more, please look into buying Filthy Rich Clients by Chet Haase and Romain Guy. You won't regret the purchase! It's one of the best Java books that I own.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    You're welcome. Please read the tutorials linked to on the bottom of my answer for the gritty details. Then run, don't walk to the bookstore and buy [Filthy Rich Clients](http://filthyrichclients.org/) by Haase and Guy, to know all that you need to know and more! – Hovercraft Full Of Eels Jul 11 '12 at 02:22