The Artima Developer Community
Sponsored Link

Java Answers Forum
repaint invoked in an infinite loop

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

repaint invoked in an infinite loop Posted: Mar 22, 2004 4:32 PM
Reply to this message Reply
Advertisement
I am trying to diplay characters in differents size and fonts.

/*
 * Created on 09-Mar-04
 *
 */
package org.joone.example.fonts.demo;
 
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
 
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 
/**
 * @author tsmets
 *
 */
public class DisplayAllKnownFonts	
{
	private static final Logger log = Logger.getLogger (DisplayAllKnownFonts.class);
	
	public static final char[] UPPER_ALPHABET = "AZERTYUIOPMLKJHGFDSQWXCVBN".toCharArray();
	public static final char[] LOWER_ALPHABET = "azertyuiopmlkjhgfdsqwxcvbn".toCharArray();
 
	public static final int SLEEP_TIME = 1000; 
	public static final int WIDTH = 700; 
	public static final int HEIGHT = 500;
	public static final int xLoc = 200; 
	public static final int yLoc = 300;
	
	public static final int DEFAULT_FONT_SIZE = 200;
	public static final int DEFAULT_FONT_TYPE = 0;
	public static final String DEFAULT_FONT_NAME = "Courier";
 
	private JFrame frame = null;
	private Container content = null;
	private JTextPane textPane = null;
	private StyledDocument document = null;
	private StyleContext ctxt = null;
	
	private String[] fonts = null;
	
	private ShowStrings sss = null; 
	
	/**
	 * 
	 * 
	 *
	 */
	DisplayAllKnownFonts ()
	{
		frame = new JFrame (DisplayAllKnownFonts.class.getName());
		log.debug ("DisplayAllKnownFonts::started");
		
		content = frame.getContentPane ();		
		
		document = new DefaultStyledDocument ();					
		textPane = new JTextPane (document);
		textPane.setEditable (true);
		JScrollPane scrollPane = new JScrollPane (textPane);		
		content.add (scrollPane, BorderLayout.CENTER);
		
		GraphicsEnvironment graphEnvir = GraphicsEnvironment.getLocalGraphicsEnvironment();
		fonts = graphEnvir.getAvailableFontFamilyNames();
		if (Level.DEBUG.equals(log.getLevel()))
			for (int i = 0; i < fonts.length; i++)
				log.debug ("Font [" + i + "] = " + fonts[i]);
		
		frame.setSize (WIDTH, HEIGHT);
		frame.setVisible (true);
		frame.setResizable (true);
		frame.setLocation ( xLoc, yLoc);
		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		
		log.debug ("DisplayAllKnownFonts::ended");
	}
	
	private void init ()
	{
		sss = new ShowStrings ();
		content.add ( BorderLayout.CENTER,
					  sss );		
	}
	
	
	public static void main (String[] args)
	{
		new DisplayAllKnownFonts ().init ();
	}
	
	/**
	 *
	 */
	class ShowStrings
		extends JPanel 
	{	
		public Logger log = Logger.getLogger (ShowStrings.class);
		
		public ShowStrings ()
		{
			log.info ("Created");
		}
			
		/**
		 * @see javax.swing.JComponent#repaint(java.awt.Rectangle)
		 */
		public void paintComponent (Graphics graphics)
		{			
			log.info ("public void paintComponent (Graphics " + graphics + ") :: invoked");
			super.paint(graphics);
			Graphics2D g2d = (Graphics2D) graphics;
								
			g2d.setFont (new Font (fonts[0], Font.ITALIC, DEFAULT_FONT_SIZE) );
			FontMetrics metrics = g2d.getFontMetrics();
			String character = String.valueOf (UPPER_ALPHABET[0]);
			int w = metrics.stringWidth (character),
				h = metrics.getHeight();
	
			g2d.drawString ( character, frame.getWidth() /2 - w / 2, frame.getHeight() / 2 - h / 2 );
			log.info ( "Width / Height : " + frame.getWidth() + " / " + frame.getHeight() ); 
			log.info ( "g2d.drawString ( " + character + " , " +  ( frame.getWidth () / 2  -  w / 2 ) + ", " + ( frame.getHeight() / 2  -  h / 2 ) + " );" );		
		}
	}
}


It seems the code is not diplaying a lot of thing sin the JPanel neither is it behaving correctly (some sort of infinite loop). Can someone help ?

\T,
The code is based on the example from Sun, see : http://java.sun.com/docs/books/tutorial/2d/textandfonts/fontselection.html in the Fontselection.java code.

Topic: Who Called Me? Previous Topic   Next Topic Topic: Setters/Getters: Related To JavaBeans Only?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use