This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDK 5.0 RC Repaint Bug

 
Joseph Goodman
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been playing around with JDK 5.0 RC and I have noticed a reapaint bug. The following code on my machine, using JDK 5.0 RC will sometimes not repaint the JFrame after resizing it. Am I just doing something crazy or is this a serious bug? I never had this problem with JDK 1.4.1.

Thanks in advance

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

public class AlphaTest extends JFrame
{
private Color mTransparentColor = new Color(0, 1, 2, 0);
/** Create the alpha test. */
public AlphaTest()
{
setSize(800, 600);
setTitle("Alpha Test");


addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

public final void paint(Graphics g)
{
Graphics2D G2d = (Graphics2D)g;



BufferedImage BackBuffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D BackBufferGraphics = BackBuffer.createGraphics();

BackBufferGraphics.setColor(mTransparentColor);
BackBufferGraphics.fillRect(0, 0, getWidth(), getHeight());

BackBufferGraphics.setColor(Color.red);
BackBufferGraphics.fillRect(20, 20, 100, 100);


G2d.setColor(Color.blue);
G2d.fillRect(0, 0, getWidth(), getHeight());

G2d.drawImage(BackBuffer, 0, 0, this);
}

/** The main loop of the program .*/
public static void main(String args[])
{
new AlphaTest().setVisible(true);
}
 
Bernhard Jung
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Custom painting should be done in a different way.
Read this:
http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html

Create a class that inherits from JPanel, override the paintComponent method and add an instance of it to a JFrame.

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

public class PaintTest extends JPanel
{

public PaintTest()
{
setBackground(Color.BLUE);
}

public final void paintComponent(Graphics g)
{
super.paintComponent(g);

g.setColor(Color.RED);
g.fillRect(20, 20, 100, 100);
}

public static void main(String args[])
{
JFrame frame = new JFrame("PaintTest");
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new PaintTest());
frame.setVisible(true);
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic