Draw a grid to test the monitor. By clicking the mouse on the form, cyclically change the colors of the grid lines in order: red, blue, green. Provide the ability to change the pitch of the grid. Provide the ability to rotate the grid at an arbitrary angle.

package dialogtest1; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class DialogTest1 extends JFrame{ JPanel panSouth; JPanel panCenter; JButton but; JButton but1; JTextField tf1; JTextField tf2; JTextField tf3; JLabel[] grid; private int colorIndex = 0; DialogTest1(){ super(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(700, 700); setLayout(new BorderLayout()); panSouth = new JPanel(); tf1 = new JTextField(5); tf2 = new JTextField(5); tf3 = new JTextField(5); but = new JButton("Change"); but1 = new JButton("Change1"); panSouth.add(new JLabel("X: ")); panSouth.add(tf1); panSouth.add(new JLabel("Y: ")); panSouth.add(tf2); panSouth.add(but); panSouth.add(new JLabel("*: ")); panSouth.add(tf3); panSouth.add(but1); add(panSouth, BorderLayout.SOUTH); but.addActionListener(new ActionListener() { int x; int y; @Override public void actionPerformed(ActionEvent e) { if (!tf1.getText().isEmpty() && !tf2.getText().isEmpty()){ x = Integer.parseInt(tf1.getText()); y = Integer.parseInt(tf2.getText()); if(panCenter != null) { remove(panCenter); } //double degreeToRotate = 40.0; grid = new JLabel[x*y]; for (int i = 0; i<grid.length; i++){ grid[i] = new JLabel() { @Override public void paintComponent(Graphics g) { Graphics2D gx = (Graphics2D) g; gx.rotate(Math.toRadians(40), getX() + getWidth() / 2, getY() + getHeight() / 2); super.paintComponent(gx); } }; grid[i].setBorder(BorderFactory.createLineBorder(Color.RED)); panCenter.add(grid[i]); } add(panCenter, BorderLayout.CENTER); revalidate(); } } }); addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); // Клик int x = Integer.parseInt(tf1.getText()); int y = Integer.parseInt(tf2.getText()); Color color; colorIndex = colorIndex + 1; if (colorIndex > 2) { colorIndex = 1; } switch (colorIndex) { case 0:color = Color.RED;break; case 1:color = Color.BLUE;break; case 2:color = Color.GREEN;break; default:color = Color.RED;break; } // System.out.println("mouseClicked ->" + e.getX() + "->" + e.getY()); for (int i = 0; i<x*y; i++){ grid[i].setBorder(BorderFactory.createLineBorder(color)); } repaint(); } }); } public static void main(String[] args) { new DialogTest1().setVisible(true); } } 

Please tell me how you can rotate the grid at a given angle.

    1 answer 1

    You can rotate using the Graphics2D.rotate method ( https://docs.oracle.com/javase/8/docs/api/java/awt/Graphics2D.html ):

     double degreeToRotate = 40.0; panCenter = new JPanel(new GridLayout(y, x)) { @Override public void paintComponent(Graphics g) { Graphics2D gx = (Graphics2D) g; gx.rotate(Math.toRadians(degreeToRotate), getX() + getWidth() / 2, getY() + getHeight() / 2); super.paintComponent(g); } }; 
    • And how can I push in the gx grid [i]? After all, I turn the grid, and it is drawn: grid = new JLabel [x * y]; for (int i = 0; i <grid.length; i ++) {grid [i] = new JLabel (); grid [i] .setBorder (BorderFactory.createLineBorder (Color.RED)); panCenter.add (grid [i]); - Alex
    • In the same way, you can override the method in JLabel and rotate each grid [i] - kb0
    • Can you please tell me how to override the method in JLabel? - Alexey
    • You can also - new JLabel () {@Override public void paintComponent (Graphics g) {Graphics2D gx = (Graphics2D) g; gx.rotate (Math.toRadians (degreeToRotate), getX () + getWidth () / 2, getY () + getHeight () / 2); super.paintComponent (g); }}; But it is better to inherit a new class and redefine paintComponent in it - kb0
    • Added method override in JLabel. If it is not difficult, please see what the error is in there (edited the first message). I just want to apologize if there is something stupid in the code, I do not study java for long. - Alexey