Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. public class LoopBasics extends JFrame implements ActionListener {
  7.     private final int DISPLAY_WIDTH = 800;
  8.     private final int DISPLAY_HEIGHT = 600;
  9.  
  10.     private JPanel guiPanel;
  11.     private DisplayPanel display;
  12.     int i;
  13.     int num;
  14.     int locX, locY;
  15.     final int LIMIT=30;
  16.     final int LETTER_SPACE =20;
  17.     final int LETTER_HEIGHT=25;
  18.     final int ROW_GAP=5;
  19.     final int COLUMN_GAP=5;
  20.     final int NUM_ROWS=10;
  21.  
  22.     public static void main(String[] args) {
  23.         LoopBasics frame = new LoopBasics();
  24.         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  25.         frame.setLayout(new BorderLayout());
  26.  
  27.         frame.initializeVariables();
  28.  
  29.         frame.setUpGUI();
  30.  
  31.         frame.pack();
  32.         frame.setVisible(true);
  33.     }
  34.  
  35.     public void initializeVariables(){
  36.  
  37.         locX=0;
  38.         locY=20;
  39.     }
  40.  
  41.     public void setUpGUI() {
  42.         display = new DisplayPanel();
  43.         guiPanel = new JPanel(new FlowLayout());
  44.  
  45.         add(guiPanel, BorderLayout.SOUTH);
  46.         add(display, BorderLayout.CENTER);
  47.  
  48.     }
  49.  
  50.     @Override
  51.     public void actionPerformed(ActionEvent e) {
  52.  
  53.         display.repaint();
  54.     }
  55.  
  56.     class DisplayPanel extends JPanel {
  57.         DisplayPanel() {
  58.             setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
  59.             this.setBackground(Color.WHITE);
  60.         }
  61.  
  62.         public void paintComponent(Graphics g) {
  63.             super.paintComponent(g);
  64.             Graphics2D g2d=(Graphics2D)g;
  65.             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  66.                     RenderingHints.VALUE_ANTIALIAS_ON);
  67.             //whileTest(g2d);
  68.             forTest(g2d);
  69.         }
  70.  
  71.         public void whileTest(Graphics2D g2d){
  72.             //modify this so that it will print out 1-100
  73.             //each line will have only 30 numbers
  74.             //hint, this will use if statements and a change in locY
  75.             num=1;
  76.             while(num<=LIMIT){
  77.                 num++;
  78.                 g2d.drawString(num+", ", locX+num* LETTER_SPACE, locY);
  79.  
  80.             }
  81.         }
  82.  
  83.         public void forTest(Graphics2D g2d){
  84.             boolean isTrue=true;
  85.             //modify this so that each number alternates between green and red
  86.             for(int j=1; j<=NUM_ROWS; j++) {
  87.                 for (int i = 0; i < LIMIT; i++) {
  88.                     g2d.drawRect(locX + i * (LETTER_SPACE+COLUMN_GAP),
  89.                             locY+(j*(LETTER_HEIGHT+ROW_GAP)), LETTER_SPACE, LETTER_HEIGHT);
  90.                 }
  91.             }
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement