thorax232

Drawing a GUI Multiplication Table

Jul 27th, 2013
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 KB | None | 0 0
  1. /*
  2.  * Write a program that displays a multiplication table in a panel using the drawing methods, as shown in Figure 13.27a.
  3.  */
  4.  
  5. import java.awt.*;
  6. import javax.swing.*;
  7.  
  8. @SuppressWarnings("serial")
  9. public class DisplayAMultiplicationTable extends JFrame
  10. {
  11.     // Constructor
  12.     public DisplayAMultiplicationTable()
  13.     {
  14.         add(new MultiplicationTablePanel());
  15.     }
  16.    
  17.     // Main method
  18.     public static void main(String[] args)
  19.     {
  20.         DisplayAMultiplicationTable frame = new DisplayAMultiplicationTable();
  21.         frame.setSize(300, 400);
  22.         frame.setTitle("Exercise 13_04");
  23.         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  24.         frame.setLocationRelativeTo(null);
  25.         frame.setVisible(true);
  26.     }
  27. }
  28.  
  29. @SuppressWarnings("serial")
  30. // New class
  31. class MultiplicationTablePanel extends JPanel
  32. {
  33.     @Override
  34.     protected void paintComponent(Graphics g)
  35.     {
  36.         super.paintComponent(g);
  37.        
  38.         // Declaration
  39.         int x = 10, y = 40, i = 0; // x & y = start point for "paint brush"
  40.         String s = "";
  41.        
  42.         // Display the title
  43.         g.setColor(Color.RED);
  44.         g.setFont(new Font("Times", Font.BOLD, 20));
  45.         g.drawString("Multiplication Table", x + 50, y);
  46.        
  47.         // Font for numbers
  48.         g.setFont(new Font("Times", Font.BOLD, 15));
  49.        
  50.         y += 30; // Move down 30 pixels
  51.         for(i = 1; i < 10; i++) // Vertical row
  52.             g.drawString("   " + i, x + 10, y + 10 + i * 20); // Spaces move line left/right, prints downwards
  53.            
  54.         x += 40; // Move right 40 pixels
  55.         for(i = 1; i < 10; i++)
  56.         {
  57.             s = s + "   " + i; // Create full string
  58.         }
  59.         g.drawString(s, x, y); // Then print horizontally (top)
  60.        
  61.         y += 10;
  62.         g.drawRect(x, y, 200, 200); // Draw border
  63.        
  64.         s = ""; // Reset string
  65.         y += 20; // Move down 20
  66.         // x does not need to be reset string will add spaces after last printed vertical numbers
  67.        
  68.         for(i = 1; i < 10; i++)
  69.         {
  70.             for(int j = 1; j < 10; j++)
  71.             {
  72.                 if(i * j < 10)
  73.                     s = s + "   " + i * j; // Builds entire row with spaces
  74.                 else
  75.                     s = s + " " + i * j; // Accounts for single and double digits
  76.             }
  77.            
  78.             g.drawString(s, x, y); // Print built string
  79.             s = ""; // Reset string
  80.             y += 20; // Move down one row
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment