Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I successfully wrote code for the scenario below. I would like someone from your

ID: 3662348 • Letter: I

Question

I successfully wrote code for the scenario below. I would like someone from your team to rewrite the code using a different approach. This will allow me to see how someone else may go about coding this program and ultimately coming out with the same results. Please code using Java and verify it will run in a Java IDE. My code can be found below. Thanks.

14.16 (Display a 3 * 3 grid) Write a program that displays a 3 * 3 grid, as shown below. Use RED color for vertical lines and BLUE for horizontals.

Expert missed this step in the code that was written. Please write the code to include this: The lines are automatically resized when the window is resized.

This is my code for the program. It runs successfully, no issues. Please re-code using a different approach so I can compare and contrast a different way to write the same program.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Exercise_16 extends Application {

    @Override
    public void start(Stage primaryStage) {

        Pane pane = new Pane();
        primaryStage.setTitle("3x3 Grid");
        primaryStage.setScene(new Scene(pane, 400, 400));


        double divider = 3;
        for (int i = 0; i < 2; i++) {
            Line line = new Line();
            line.setStroke(Color.RED);
            line.startXProperty().bind(pane.widthProperty().divide(divider));
            line.startYProperty().bind(pane.layoutYProperty());
            line.endXProperty().bind(line.startXProperty());
            line.endYProperty().bind(pane.heightProperty());
            pane.getChildren().add(line);
            divider /= 2;
        }
        divider = 3;
        for (int i = 0; i < 2; i++) {
            Line line = new Line();
            line.setStroke(Color.BLUE);
            line.startXProperty().bind(pane.layoutXProperty());
            line.startYProperty().bind(pane.heightProperty().divide(divider));
            line.endXProperty().bind(pane.widthProperty());
            line.endYProperty().bind(pane.heightProperty().divide(divider));
            pane.getChildren().add(line);
            divider /= 2;
        }


        primaryStage.show();
    }

    public static void main(String[] args) {

        Application.launch(args);

    }
}

Expert code does not automatically resize the lines when the window is resized. When the window is expanded the line stay the same, and do not extend to fit the window. Please fix this. Thanks.

Expert Answer

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

class SwingControlDemo extends JFrame{
   public SwingControlDemo(){
       setTitle("DEMO");
       setSize(400, 400);
       setLayout(null);

       setVisible(true);
       super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paint(Graphics g){
       super.paint(g);
       Graphics2D g2 = (Graphics2D) g;
       g.setColor(Color.RED);
       Line2D lin_1 = new Line2D.Float(133,0,133,400);
       g2.draw(lin_1);

       Line2D lin_2 = new Line2D.Float(266,0,266,400);
       g2.draw(lin_2);

       g.setColor(Color.BLUE);
       Line2D lin_3 = new Line2D.Float(0,133,400,133);
       g2.draw(lin_3);

       Line2D lin_4 = new Line2D.Float(0,266,400,266);
       g2.draw(lin_4);
    }
}

class main{
   public static void main(String[] args){
       SwingControlDemo demo = new SwingControlDemo();    
   }
}

Explanation / Answer

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

class SwingControlDemo extends JFrame implements ComponentListener {
   public SwingControlDemo(){
       setTitle("DEMO");
       setSize(400, 400);
       setLayout(null);
      
       addComponentListener(this);
       setVisible(true);
       super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
   public void componentHidden(ComponentEvent event){};
   public void componentResized(ComponentEvent event){
       repaint();
   };
   public void componentShown(ComponentEvent event){};
   public void componentMoved(ComponentEvent event){};
   public void paint(Graphics g){
       super.paint(g);
       Graphics2D g2 = (Graphics2D) g;
      
       Dimension win = this.getSize();
       int w = win.width;
       int h = win.height;
      
       g.setColor(Color.RED);
       Line2D lin_1 = new Line2D.Float(w/3,0,w/3,h);
       g2.draw(lin_1);
       Line2D lin_2 = new Line2D.Float((2*w)/3,0,(2*w)/3,h);
       g2.draw(lin_2);

       g.setColor(Color.BLUE);
       Line2D lin_3 = new Line2D.Float(0,h/3,w,h/3);
       g2.draw(lin_3);
       Line2D lin_4 = new Line2D.Float(0,(2*h)/3,w,(2*h)/3);
       g2.draw(lin_4);
   }
}

class main{
   public static void main(String[] args){
       SwingControlDemo demo = new SwingControlDemo();
   }
}


run as I explain last time;