Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否将repaint()方法添加到JFrame或Viewer?_Java_Jframe_Repaint - Fatal编程技术网

Java 是否将repaint()方法添加到JFrame或Viewer?

Java 是否将repaint()方法添加到JFrame或Viewer?,java,jframe,repaint,Java,Jframe,Repaint,这是一个棋盘游戏,上面有棋子 在这里,我希望在完成拖动后重新绘制图形。 我在控制台中有一个非图形版本(基于文本),显示工件移动。 我已经向JFrame和组件(theImgTester)添加了repaint()方法 ChessViewer theImgTester是一个包含棋子和棋盘的组件。 我正在尝试使repaint()方法正常工作。 我需要在ChessViewer中更改任何内容吗?不过我很确定问题出在这个文件里。 请给我一些提示 编辑:ChessViewer.java import java.

这是一个棋盘游戏,上面有棋子

在这里,我希望在完成拖动后重新绘制图形。 我在控制台中有一个非图形版本(基于文本),显示工件移动。 我已经向JFrame和组件(theImgTester)添加了repaint()方法

ChessViewer theImgTester是一个包含棋子和棋盘的组件。 我正在尝试使repaint()方法正常工作。 我需要在ChessViewer中更改任何内容吗?不过我很确定问题出在这个文件里。 请给我一些提示

编辑:ChessViewer.java

import java.awt.event.*;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class ChessRunner implements MouseListener
{  

    private static final int FRAME_WIDTH = 500;
    private static final int FRAME_HEIGHT = 600;
    private static int colFrom;
    private static int rowFrom;
    private static int colTo;
    private static int rowTo;

    public static void main(String[] args)
    {  

        JFrame frame = new JFrame();
        ChessViewer theImgTester = new ChessViewer(); 
        frame.add(theImgTester);
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        frame.addMouseListener(new MouseAdapter(){

            public void mouseClicked(MouseEvent event){

            }

            /**
             * Gets initial mouse click position, translates it to row and column
             */
            public void mousePressed(MouseEvent event){
                colFrom = (int)((event.getX()/45));
                rowFrom = (int)(9-(event.getY()/45));
            }
            /**
             * Set the new row and column after mouse is released.
             */
        public void mouseReleased(MouseEvent event){
            colTo = (int)(event.getX()/45);
            rowTo = (int)(9-event.getY()/45);


            theImgTester.getBoard().move(rowFrom,colFrom,rowTo,colTo);
            theImgTester.getBoard().print();
            theImgTester.repaint();
            frame.repaint();
        }
        });
    }   
}
导入java.awt.Graphics2D;
导入java.awt.Graphics;
导入javax.swing.JComponent;
导入javax.swing.JLabel;
公共类ChessViewer扩展JComponent{
专用JLabel turn=新JLabel();
私人棋盘;
/**
*chess查看器的构造函数。
*构建一个新的棋盘。
*/
公共棋子查看器(){
棋盘=新棋盘();
board.start();
}
/**
*返回棋盘
*@返回
*/
公共棋盘{
返回板;
}
/**
*方法打印棋盘和棋盘上的棋子。
*/
公共空间涂料(图形h){
图形2d g=(图形2d)h;
板材、油漆(g);
对于(inti=0;i对于(int j=0;jc您能告诉我如何在
theImgTester
?(是
theImgTester
的子类
awt.component
?)上添加
repaint()
方法吗如果我记得的话,已经有了一个
repaint
method你知道@niceman如何实现
theImgTester
吗?也许我们可以在那里找到问题?@MartinFrank我怎么知道?!但是问题肯定在那里,我觉得他自己定义了repaint方法,或者忘记调用
super.repaint()
或者他隐藏了super的函数而不是重写它。“如果我记得的话”我是指我对java的了解。@不错,我就是这么想的,但我不敢写出来!谢谢你的勇敢!我想你是对的!让我们看看
theImgTester实例的类中写了什么
import java.awt.Graphics2D; 
import java.awt.Graphics; 
import javax.swing.JComponent;
import javax.swing.JLabel;

public class ChessViewer extends JComponent{
    private JLabel turn = new JLabel();


    private ChessBoard board;

    /**
     * Constructor for the chess viewer.
     * Constructs a new chessboard.
     */
        public ChessViewer() {
        board= new ChessBoard();
        board.start();
    }


    /**
     * Returns the chessboard
     * @return
     */
    public ChessBoard getBoard(){
        return board;
    }




    /**
     * Method to print the chessboard and pieces on the chessboard.
     */
    public void paint(Graphics h) {
        Graphics2D g = (Graphics2D)h;

        board.paint(g);
        for(int i = 0; i < board.getSizeB(); i++){
            for(int j = 0; j<board.getSizeB(); j++){
                board.getBoard()[j][i].paint(h);
            }
        }


    }
}