/* * Created on Apr 5, 2005 */ package WordPro2Controller; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JPanel; import WordPro2Document.IDocument; /** A class which can display a document graphically. * You need to modify this class. * If you define additional fields or methods, they must be private. */ public class DocumentViewer extends JPanel { private IDocument theDocument; /** Request that a document be displayed on this panel. * Normally you would call this method from MyDocument. The * argument would normally be 'this'. * You should not have to change this method. If you think you do, * you probably should think again. * @param aDocument a non-null document. */ public void display(IDocument aDocument) { this.theDocument = aDocument; repaint(); } /** DO NOT CALL THIS METHOD. Take it on blind faith that it will * get called when needed. Instead of calling this method, MyDocument * normally should just call display. In fact, the disply method, * as coded above, will indirectly cause paintComponent to be called. * DO NOT change the signature (name, return value, arguments, etc.). * But do modify the code! All your code for displaying the document should * be here. */ public void paintComponent(Graphics oldG) { //Always start your paint methods with this cast. Graphics2D g = (Graphics2D) oldG; //Then always call the superclass version super.paintComponent(g); int x = 5; int y = 10; final int linespacing = 20; g.drawString("Here's a line as a test.", x, y); y += linespacing; g.drawString("The next line would start further down (by changing y)", x, y); y += linespacing; g.drawString("These lines are just a demo.", x, y); y += linespacing; g.drawString("The first person to ask, \"Is it OK to delete " + " or change these lines?\"", x, y); y += linespacing; g.drawString("that person flunks the class automatically.", x, y); y += linespacing; for (int line = 0; line < 100; line++) { g.drawString("just another line.", x, y); y += linespacing; } //adjust the height of the visible window. //otherwise, it won't appear to scroll. //Do this after you have written all the data. int width = this.getSize().width; this.setPreferredSize(new Dimension(width, y)); this.revalidate(); } }