/* * [SmoothScroll.java] * * Summary: Traditional Scroll. * * 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; /** * Traditional Scroll. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2002-06-28 original. * @since 2002-06-28 */ 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.event.WindowAdapter; import java.awt.event.WindowEvent; public final class SmoothScroll extends JPanel { int w, h; // width and height Image bigImage; // offscreen big image we pan over /** * how many pixels we have so far panned the image * up. */ private int pixelsScrolled = 0; public SmoothScroll() { super( false ); w = 800; h = 900; } public static void main( String[] args ) { JFrame f = new JFrame(); final SmoothScroll tp = new SmoothScroll(); f.getContentPane().add( tp ); f.pack(); f.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent we ) { System.exit( 0 ); } } ); f.setVisible( true ); } public void addNotify() { super.addNotify(); // create buffered image here Font f = new Font( "Arial", Font.BOLD, 48 ); // Ideally want Image, using native video card format. // prepare giant image offscreen. bigImage = createImage( w, h ); Graphics g = bigImage.getGraphics(); Graphics2D g2d = null; if ( g instanceof Graphics2D ) { g2d = ( Graphics2D ) g; g2d.addRenderingHints( new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ) ); } 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 the party.", 10, y ); y += strHeight; } } public Dimension getPreferredSize() { return new Dimension( 800, 600 ); } public void paintComponent( Graphics g ) { // copy a small piece of the big image onto the screen // We are not scaling. g.drawImage( bigImage, /* dx1 */ 0, /* dy1 */ 0, /* dx2 */ w, /* dy2 */ h, /* sx1 */ 0, /* sy1 */ pixelsScrolled, /* sx2 */ w, /* sy2 */ h + pixelsScrolled, /* observer */ this ); pixelsScrolled++; repaint(); // tight loop to scroll as fast as possible. // in real life you would control the frequency of repaints in a more sophisticated // manner. } }