简体   繁体   中英

How do I get my multiplication table to have vertical lines?

I am trying to draw a multiplication table in Java. I can only seem to get the horizontal lines to print out. I'm new to coding and need help knowing where and what line of code I put in my for loop to make it run and look like a complete table.

public static void drawRow(int row, int size) {
    g.drawLine(width, 3 + (row - 1) * height, 270, 3 + (row - 1) * height);

    for (int col = 1; col <= size; ++col) {
        g.drawString(pad(col), col * width, height);
        System.out.printf("%4d", row * col);
        g.drawString(pad(row * col), width * col, height * row);

    }

    g.drawLine(width, 3 + (row + 0) * height, 270, 3 + (row + 0) * height);

I tried to do a string above and below my for loop using g.drawLine.

To get an answer, a reproducible example is always a good place to start. I don't know what your code looks like exactly, but I think it might be something like this:

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

public class App extends JFrame {
    public static void main(String[] args) {
        new App();
    }

    public App() {
        super("Multiplication Table");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(640, 480));
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                for(int row = 1; row <= 10; row++) drawRow(g, row, 10, 25, 25);
            }
        };
        add(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void drawRow(Graphics g, int row, int size, int width, int height) {
        g.drawLine(width, 3 + (row - 1) * height, 270, 3 + (row - 1) * height);

        for (int col = 1; col <= size; ++col) {
            g.drawString(pad(col), col * width, height);
            System.out.printf("%4d", row * col);
            g.drawString(pad(row * col), width * col, height * row);

        }

        g.drawLine(width, 3 + (row + 0) * height, 270, 3 + (row + 0) * height);
    }

    public static String pad(Integer value) {
        return value.toString();
    }

}

What could an answer look like then? Maybe like this:

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

public class App extends JFrame {
    public static void main(String[] args) {
        new App();
    }

    public App() {
        super("Multiplication Table");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMinimumSize(new Dimension(640, 480));
        JPanel panel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                drawTable(g, 10, 25, 20);
            }
        };
        add(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    /**
     * @param g - Graphics from eg paintComponent
     * @param size - size of the table (eg 10 for 10x10)
     * @param width - width of a cell in pixels
     * @param height - height of a cell in pixels
     */
    public void drawTable(Graphics g, int size, int width, int height) {
        // set the top/left corner of the table
        g.translate(20, 20);
        // draw horizontal and vertical lines
        for(int i = 0; i <= size; i++) {
            g.drawLine(i * width, 0, i * width, height * size);
            g.drawLine(0, i * height, width * size, i * height);
        }
        // calculate maximum width (eg width of "100" for size 10)
        int maxWdth = g.getFontMetrics().stringWidth("" + size * size);
        // draw the multiplications
        for(int y = 1; y <= size; y++)
            for(int x = 1; x <= size; x++) {
                // calculate current width (eg width of "56" for 7x8)
                String result = String.valueOf(x * y);
                int resWdth = g.getFontMetrics().stringWidth(result);
                // to align right, add (maxWdth - resWdth) to drawString x
                g.drawString(result, (x - 1) * width + maxWdth - resWdth + 2, y * height - 2);
            }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM