Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class LoopBasics extends JFrame implements ActionListener {
- private final int DISPLAY_WIDTH = 800;
- private final int DISPLAY_HEIGHT = 600;
- private JPanel guiPanel;
- private DisplayPanel display;
- int i;
- int num;
- int locX, locY;
- final int LIMIT=30;
- final int LETTER_SPACE =20;
- final int LETTER_HEIGHT=25;
- final int ROW_GAP=5;
- final int COLUMN_GAP=5;
- final int NUM_ROWS=10;
- public static void main(String[] args) {
- LoopBasics frame = new LoopBasics();
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.initializeVariables();
- frame.setUpGUI();
- frame.pack();
- frame.setVisible(true);
- }
- public void initializeVariables(){
- locX=0;
- locY=20;
- }
- public void setUpGUI() {
- display = new DisplayPanel();
- guiPanel = new JPanel(new FlowLayout());
- add(guiPanel, BorderLayout.SOUTH);
- add(display, BorderLayout.CENTER);
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- display.repaint();
- }
- class DisplayPanel extends JPanel {
- DisplayPanel() {
- setPreferredSize(new Dimension(DISPLAY_WIDTH, DISPLAY_HEIGHT));
- this.setBackground(Color.WHITE);
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2d=(Graphics2D)g;
- g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
- RenderingHints.VALUE_ANTIALIAS_ON);
- //whileTest(g2d);
- forTest(g2d);
- }
- public void whileTest(Graphics2D g2d){
- //modify this so that it will print out 1-100
- //each line will have only 30 numbers
- //hint, this will use if statements and a change in locY
- num=1;
- while(num<=LIMIT){
- num++;
- g2d.drawString(num+", ", locX+num* LETTER_SPACE, locY);
- }
- }
- public void forTest(Graphics2D g2d){
- boolean isTrue=true;
- //modify this so that each number alternates between green and red
- for(int j=1; j<=NUM_ROWS; j++) {
- for (int i = 0; i < LIMIT; i++) {
- g2d.drawRect(locX + i * (LETTER_SPACE+COLUMN_GAP),
- locY+(j*(LETTER_HEIGHT+ROW_GAP)), LETTER_SPACE, LETTER_HEIGHT);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement