Re: Self-changing font...
- From: Vova Reznik <address@xxxxxxxx>
- Date: Tue, 28 Feb 2006 15:27:00 GMT
You may also comment repaint() call from componentResized (calls repaint or update anyway after resizing).
I think better way is to use paintComponent combined with your #2 fix
Rob McDonald wrote:
I have included a test program below that isn't well behaved. If someone.
can identify the real problem, it will be much appreciated...
If you run the program, and resize the JFrame, sometimes the Magic Text is
printed in Times New Roman (desired behavior) and sometimes it is printed in
a default font. If the window is large, it is more likely to be wrong. If
the window is small, it is more likely to be right. If it is wrong, and you
raise another window above the program, and then bring it back to the top,
it will fix the font.
I realize that this is a very complicated program for doing something rather
simple, but it is representative of my real program that is showing this
behavior. So, just simplifying the program without understanding exactly
what is going wrong isn't really a fix. I need something that will apply to
a far more complex version of this...
For starters, here are some easy ways to fix the program (none of them are
acceptable to me, but maybe they'll give someone smarter a clue to the real
problem.)
1) Change insPanel to extend java.awt.Component or javax.swing.JComponent.
2) Move w & h calculation to paint() and remark out
this.addComponentListener
3) draw "MagicText" from within the gTmp graphics context. (above the clip,
or inside it if you prefer)
Thanks in advance for any suggestions,
Rob
* I need to play the games with creating a separate graphics context with a
clip because this is used with a PDF output routine. The PDF file format
can't 'grow' a clip, it can only shrink it. So, to set a temporary clip,
you have to create a copy of the crrent graphics context, use it, and then
get rid of it.
// TestWeird.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestWeird{
insPanel pan;
public TestWeird(){
final JFrame frame = new JFrame();
pan = new insPanel();
setFont(new Font("Times New Roman", Font.BOLD, 14));
frame.getContentPane().add(pan, BorderLayout.CENTER);
frame.pack();
frame.setSize(500, 400);
frame.setLocation(100,100);
frame.setVisible(true);
}
class insPanel extends JPanel{
int w;
int h;
insPanel(){
super();
this.addComponentListener(new compListener());
}
class compListener implements ComponentListener {
public void componentHidden(ComponentEvent evt) {}
public void componentMoved(ComponentEvent evt) {}
public void componentShown(ComponentEvent evt) {}
public void componentResized(ComponentEvent evt) {
w = getWidth()/2;
h = getHeight()/2;
repaint();
}
}
public void paint(Graphics g){
Graphics gTmp = g.create();
super.paint(gTmp);
gTmp.setClip(w,h,w,h);
gTmp.drawString("Clipped Text", w-30, h+7);
gTmp.dispose();
g.drawString("Magic Text", w-10, h-30);
}
}
public void setFont(Font fnt){
pan.setFont(fnt);
}
private static void createAndShowGUI() {
new TestWeird();
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
- Follow-Ups:
- Re: Self-changing font...
- From: Rob McDonald
- Re: Self-changing font...
- References:
- Self-changing font...
- From: Rob McDonald
- Self-changing font...
- Prev by Date: Re: Self-changing font...
- Next by Date: Re: Self-changing font...
- Previous by thread: Re: Self-changing font...
- Next by thread: Re: Self-changing font...
- Index(es):
Relevant Pages
|
|