繁体   English   中英

不绘制Java的Paint方法

[英]Paint method not painting Java

我的绘制方法似乎无法绘制我的 20x20 单元格。 我有一个用于单元格的布尔数组来控制它们的状态,如果为真,则调用单元格绘制方法,绘制单元格但是我有两个问题;

  1. 一次只绘制一个,这很奇怪,因为我应该有一个 40x40 的布尔数组,这意味着我有 40x40 个单元格

  2. 他们实际上并没有准确地绘制我点击的位置。 我不知道这是怎么回事,因为当我获得我的点击坐标时,我立即将这些坐标作为我的 x 和 y 值放置在我的绘画方法中。

主要的


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;

public class mainApplication extends JFrame implements Runnable, MouseListener {


    private static final Dimension windowsize = new Dimension(80, 600);
    private BufferStrategy strategy;
    private Graphics offscreenGraphics;
    private static boolean isGraphicsInitialised = false;
    private static int rows = 40;
    private static int columns = 40;
    private static int height = windowsize.height;
    private static int width = windowsize.width;
    private static Cells cells = new Cells();
    private int xArrayElement,yArrayElement, xPosition, yPosition;
    private static boolean gameState[][] = new boolean[rows][columns];


    public mainApplication() {


        System.out.println(System.getProperty("user.dir"));

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

        int x = screensize.width / 2 - windowsize.width / 2;
        int y = screensize.height / 2 - windowsize.height / 2;

        setBounds(x, y, screensize.width, screensize.height);

        setVisible(true);

        createBufferStrategy(2);

        strategy = getBufferStrategy();

        offscreenGraphics = strategy.getDrawGraphics();

        isGraphicsInitialised = true;

       // MouseEvent mouseEvent = new MouseEvent();
        addMouseListener(this);
       // addMouseMotionListener(MouseEvent);

        Thread t = new Thread(this);

        t.start();

    }


    public void mousePressed(MouseEvent e) { }

    public void mouseReleased(MouseEvent e) { }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    public void mouseClicked(MouseEvent e) {

        if(e.getClickCount() == 1){

               xPosition = e.getX();
               yPosition = e.getY();

           cells.setPosition(xPosition,yPosition);

            xArrayElement = (xPosition/20);
            yArrayElement = (yPosition/20);

            if(gameState[xArrayElement][yArrayElement]){
                gameState[xArrayElement][yArrayElement] = false;
            }
            else if (!gameState[xArrayElement][yArrayElement]) {
                gameState[xArrayElement][yArrayElement] = true;
            }
            else(gameState[xArrayElement][yArrayElement]) = true;
        }
    }

    @Override
    public void run() {
            while (true) {

                try { //threads entry point
                    Thread.sleep(20); //forces us to  catch exception
                }

                catch (InterruptedException e) {
                }
              this.repaint();
            }
        }


    public void paint(Graphics g) {

        if (isGraphicsInitialised) {
            g = strategy.getDrawGraphics();

            g.setColor(Color.BLACK);

            g.fillRect(0, 0, 800, 800);


                    if (gameState[xArrayElement][yArrayElement]) {
                        g.setColor(Color.WHITE);
                        cells.paint(g);
                        System.out.println(xPosition);
                    }

                    else if (!gameState[xArrayElement][yArrayElement]) {

                        g.setColor(Color.BLACK);
                        g.fillRect(xPosition, yPosition, 20, 20);
                    }

                strategy.show();
            }
        }

    public static void main(String[]args){

        mainApplication test = new mainApplication();

    }
}

细胞类别


import java.awt.*;

public class Cells {

    int x;
    int y;


    public Cells(){

    }

    public void setPosition(int xi, int xj){

        x = xi;
        y = xi;
    }

    public boolean cellState(boolean visible){

        return visible;
    }

    public void paint(Graphics g){

        g.drawRect(x, y, 20,20);
    }
}

你做错了很多事情。 我的第一个建议是忘记屏幕外的图形,并确保你正在做你想做的事。 您可以随时创建图像。 以下是一些基本准则:

  • 不要扩展JFrame 使用实例。
  • 扩展JPanel或创建一个扩展类JPanel ,并加入到框架实例
  • 然后覆盖paintComponent(g)并使用该图形上下文进行绘制。

这是一个较早的答案,可能有助于Can't add Graphics into JPanel in Java

更多信息可以在Java绘画教程中找到。

更新 我花了几分钟才找到这个。

 public void setPosition(int xi, int xj){
        x = xi;
        y = xi; // <--- should be xj
 }

关于上述(1) 每次输入paintComponent时都必须重新绘制每个单元格。 这意味着您需要遍历列表并将它们绘制在正确的位置。 现在你只在每个条目上画一个。

还有一些建议。 与其搞乱线程并在循环中每 20 毫秒调用一次重绘,为什么不直接在mouseClicked()方法中调用repaint

如果您最终需要每 20 毫秒绘制一次。 我建议按如下方式使用swing Timer :(检查 JavaDoc 以确保我的语法正确!!)

Timer timer  = new Timer(0, (ev)-> frame.repaint());
timer.setDelay(20);
timer.start();

您可以创建自己的mouseListener类并扩展MouseAdapter 这些适配器类的目的是保持混乱,这样您就不必使用空方法来满足接口要求。 将类放在主类中,以便它可以访问适当的数据结构。 然后只需将它的一个实例添加到目标组件的鼠标侦听器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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