Линии, нарисованные с помощью DrawLine, случайным образом показаны в JFrame - PullRequest
0 голосов
/ 23 июня 2018

Ниже приведен минимальный пример ошибки, которую я не могу исправить в Java-программе.

Ошибка состоит в том, что строки случайным образом отображаются в JFrame: иногда я вижу плату во время выполненияиногда нет.

Любая подсказка очень ценится.

import java.awt.Graphics;
import javax.swing.JFrame;

public class EssaiDrawLine extends JFrame{

public static void main(String[] args) {

    EssaiDrawLine mafenetre = new EssaiDrawLine();
    mafenetre.setVisible(true);

}

// constructeur
public EssaiDrawLine() {
    setTitle("mon titre");
    setSize(600, 500);
}

public void paint(Graphics g) {
    super.paint(g);
    for (int i = 0; i <= 8; i++) {
        g.drawLine(50 * i + 10, 40, 50*i + 10, 440);
        g.drawLine(10, 50 * i + 40, 410, 50 * i + 40);
    }
    g.drawString("Cliquer pour obtenir la prochaine solution", 20, 470);
}
}

Ответы [ 2 ]

0 голосов
/ 23 июня 2018

Другой вариант: не рисовать линии, а вместо этого создавать сетку компонентов, например, двумерный массив JPanels, хранящийся в другом JPanel, который использует GridLayout, а затем дать JPanels MouseListener, чтобы они могли отвечать на события мыши. Например, простая версия этого может выглядеть примерно так (пожалуйста, прочитайте комментарии в коде):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;    
import javax.swing.*;

@SuppressWarnings("serial") // get rid of warning
public class EssaiDrawLine3 extends JPanel {
    private static final int SIDE = 8; 
    private static final int SQR_LENGTH = 50;
    private static final int GAP = 5;
    private static final Color CLICK_COLOR = Color.RED;
    private static final Color SQR_COLOR = Color.LIGHT_GRAY;
    private static final String CLICK_TEXT = "Cliquer pour obtenir la prochaine solution";

    // our grid of JPanel
    private JPanel[][] grid = new JPanel[SIDE][SIDE];

    public EssaiDrawLine3() {
        // JPanel to hold the grid
        // give it an 8x8 GridLayout with 1 pixel gap for lines to show 
        JPanel gridHolder = new JPanel(new GridLayout(SIDE, SIDE, 1, 1));

        // background color that shows through as lines in gaps in grid
        gridHolder.setBackground(Color.BLACK);
        gridHolder.setBorder(BorderFactory.createLineBorder(Color.black));

        // mouse listener to add to each JPanel cell
        MyMouse myMouse = new MyMouse();

        // nested for loop to create our grid of JPanels
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[row].length; col++) {
                // create a single cell
                JPanel cellPanel = new JPanel();

                // size it 50x50 pixels
                cellPanel.setPreferredSize(new Dimension(SQR_LENGTH, SQR_LENGTH));

                // add the mouse listener to it
                cellPanel.addMouseListener(myMouse);

                // give it a default background color
                cellPanel.setBackground(SQR_COLOR);

                // place it into the 2-D array of JPanel
                grid[row][col] = cellPanel;

                // place it into the grid layout-using JPanel
                gridHolder.add(cellPanel);
            }
        }

        // display text at the bottom
        JLabel label = new JLabel(CLICK_TEXT, SwingConstants.CENTER);

        // allow gaps around the main JPanel
        setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));

        // give the main JPanel a BorderLayout
        setLayout(new BorderLayout(GAP, GAP));
        add(gridHolder);  // add the grid to the CENTER position

        // add the JLabel to the bottom position
        add(label, BorderLayout.PAGE_END);
    }

    private class MyMouse extends MouseAdapter {

        @Override
        public void mousePressed(MouseEvent e) {
            // get the JPanel cell that was clicked
            JComponent comp = (JComponent) e.getSource();

            // get its color and change it
            Color color = comp.getBackground();
            if (color.equals(CLICK_COLOR)) {
                comp.setBackground(SQR_COLOR);
            } else {
                comp.setBackground(CLICK_COLOR);
            }
        }
    }

    private static void createAndShowGui() {
        EssaiDrawLine3 mainPanel = new EssaiDrawLine3();

        JFrame frame = new JFrame("Essai Draw Line3");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
0 голосов
/ 23 июня 2018

Никогда не рисуйте непосредственно в JFrame, а вместо этого в методе JPanel paintComponent, а затем отображайте этот JPanel в вашем JFrame. Все это хорошо объясняется в стандартных уроках: Урок: Выполнение пользовательской живописи

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

@SuppressWarnings("serial")
public class EssaiDrawLine2 extends JPanel {

    private static final int PS_WIDTH = 600;
    private static final int PS_HEIGHT = 500;

    public EssaiDrawLine2() {
        setPreferredSize(new Dimension(PS_WIDTH, PS_HEIGHT));
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i <= 8; i++) {
            g.drawLine(50 * i + 10, 40, 50 * i + 10, 440);
            g.drawLine(10, 50 * i + 40, 410, 50 * i + 40);
        }
        g.drawString("Cliquer pour obtenir la prochaine solution", 20, 470);
    }

    private static void createAndShowGui() {
        EssaiDrawLine2 mainPanel = new EssaiDrawLine2();

        JFrame frame = new JFrame("Essai Draw Line");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
...