/* * [TestPanel.java] * * Summary: Test smooth scrolling. * * Copyright: (c) 1998-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2002-06-28 original. */ package com.mindprod.smooth; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.Toolkit; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.awt.image.MemoryImageSource; /** * Test smooth scrolling. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2002-06-28 original. * @since 2002-06-28 */ public final class TestPanel extends JPanel implements Runnable { int w, h; Image image = null; int lastLine; // Start position of the last line int[] imageData; // Our image data. int[] buffer; // Will hold a single image line MemoryImageSource mis; public TestPanel() { super( false ); w = 800; h = 900; lastLine = w * ( h - 1 ); imageData = new int[ w * h ]; buffer = new int[ w ]; Font f = new Font( "Arial", Font.BOLD, 48 ); BufferedImage bi = new BufferedImage( w, h, BufferedImage.TYPE_INT_RGB ); Graphics g = bi.getGraphics(); RenderingHints ANTI_ALIAS = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); Graphics2D g2d = null; if ( g instanceof Graphics2D ) { g2d = ( Graphics2D ) g; g2d.addRenderingHints( ANTI_ALIAS ); } g.setColor( Color.RED ); g.fillRect( 0, 0, w, 168 ); g.setColor( Color.BLUE ); g.fillRect( 0, 168, w, h ); g.setColor( Color.WHITE ); g.setFont( f ); FontMetrics fm = g.getFontMetrics(); int strHeight = fm.getHeight(); g.drawString( "AQUEDUCT", ( ( w - fm.stringWidth( "AQUEDUCT" ) ) / 2 ), strHeight ); int y = strHeight + 168; for ( int i = 0; i < h / strHeight; i++ ) { g.drawString( i + "Now is the time for all good men to come to the aid of...", 10, y ); y += strHeight; } bi.getRGB( 0, 0, w, h, imageData, 0, w ); mis = new MemoryImageSource( w, h, imageData, 0, w ); mis.setAnimated( true ); image = Toolkit.getDefaultToolkit().createImage( mis ); } public static void main( String[] args ) { JFrame f = new JFrame(); final TestPanel tp = new TestPanel(); f.getContentPane().add( tp ); f.pack(); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent we ) { System.exit( 0 ); } public void windowOpened( WindowEvent we ) { new Thread( tp ).start(); } } ); f.setVisible( true ); } public Dimension getPreferredSize() { return new Dimension( 800, 600 ); } public void paintComponent( Graphics g ) { g.drawImage( image, 0, 0, this ); } public void run() { while ( true ) { System.arraycopy( imageData, w * 168, buffer, 0, w ); System.arraycopy( imageData, w + w * 168, imageData, w * 168, ( h - 168 ) * w - w ); System.arraycopy( buffer, 0, imageData, lastLine - w, w ); // mis.newPixels(); mis.newPixels( 0, 168, w, h ); try { Thread.sleep( 55 ); } catch ( InterruptedException ie ) { } } } }