unwanted flickering graphics

My code produces annoying flickering graphics. Can somebody explain how to fix this?

Here is the code. Sorry for the lack of comments. I am sure that if you know java you will know what is going on.

import java.lang.Integer;

import java.awt.*;

import java.awt.

event.*;

import java.awt.font.*;

import java.awt.geom.*;

import java.awt.image.*;

import java.awt.event.ItemListener;

import java.awt.event.ItemEvent;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.*;

import java.awt.Graphics;

import javax.swing.*;

import java.awt.*;

import javax.swing.text.*;

import javax.swing.event.*;

import java.awt.event.*;

import javax.swing.JApplet.*;

import javax.swing.JFrame;

import java.applet.Applet;

//Class definition

public class Shapeshift extends JApplet {

JTextField hfield = new JTextField(10);

private int x;

public void init() {

int start = 0;

GridBagLayout gbl = new GridBagLayout();

getContentPane().setLayout(gbl);

GridBagConstraints gbc = new GridBagConstraints();

gbc.weightx = 1;

gbc.fill = GridBagConstraints.BOTH;

JLabel h = new JLabel();

h.setText("h");

h.setHorizontalAlignment(JLabel.LEFT);

gbl.setConstraints(h, gbc);

getContentPane().add(h);

hfield.setText(start + "");

getContentPane().add(hfield);

JButton up = new JButton();

up.setText("UP");

getContentPane().add(up);

up.addActionListener(new T1C());

}

class T1C implements ActionListener

{

public void actionPerformed(ActionEvent e) {

x = (int)(Integer.parseInt(hfield.getText())) + 1;

hfield.setText(x + "");

}

}

public void paint(Graphics g) {

super.paint(g);

Graphics2D g2 = (Graphics2D) g;

Dimension d = getSize();

int w = d.width;

int h = d.height;

AffineTransform saveXform = g2.getTransform();

AffineTransform toCenterAt = new AffineTransform();

toCenterAt.translate(w/2, h/2);

g2.transform(toCenterAt);

g2.setColor(Color.gray);

g2.fillRect(x, 0, 50, 50);

g2.fillRect(-25, -20, x + 25, 90);

g2.fillRect(50, -20, 25, 90);

repaint();

}

public static void main( String[] argv ) {

//if ( argv.length > 0 && argv[0].equals( "-no2d" ) ) {

//Transform.no2D = true;

//}

JFrame frame = new JFrame( "Transform" );

frame.addWindowListener( new WindowAdapter(){

public void windowClosing( WindowEvent e ){

System.exit( 0 );

}

});

JApplet applet = new Shapeshift();

frame.getContentPane().add( BorderLayout.CENTER, applet );

applet.init();

frame.setSize( 550, 400 );

frame.setVisible(true);

}

}

[2995 byte] By [zite.1a] at [2007-11-27 10:26:04]
# 1

> My code produces annoying flickering graphics. Can

> somebody explain how to fix this?

Reclassify the flickering as a security feature.

cotton.ma at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 2

Read up on double buffering.

http://www.realapplets.com/tutorial/DoubleBuffering.html

http://java.sun.com/docs/books/tutorial/extra/fullscreen/doublebuf.html

CaptainMorgan08a at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 3

You added an applet to a frame? Eww....

BigDaddyLoveHandlesa at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 4

So, what does that mean? Any suggestions?

Jamaala at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 5

a JApplet, like a JFrame, JDialog, and JWindow is a top level container. It is meant to stand on its own and is not meant to be put into anything. For info on top level containers, check out this:

http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html

petes1234a at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 6

Thanks. But I don't know what parts of the codes to keep and which to take. Can you give me a simpler example?

Jamaala at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 7

sure you can give any example you like. In my mind the bottom line is this. FIRST you have to chose what you are creating. Is it a JFrame? Then make it so. A JApplet? Then create that.

One possible solution would be to create a class that extends JPanel and then add an object of this class to a JFrame's optionPane if you want a JFrame or to that of a JApplet if you want a JApplet.

petes1234a at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...
# 8

I agree with Pete, you should make it extend JPanel, not JApplet. There are a few things you should change. Instead of writing:

getContentPane().setLayout(...);

//or

getContentPane().add(...);

Just write:setLayout(...);

//or

add(...);

Also, you should be overriding the paintComponent() method, not the paint() method.

CaptainMorgan08a at 2007-7-28 17:37:21 > top of Java-index,Java Essentials,New To Java...