PaintComponent 无法正确绘制 [英] paintComponent does not paint correctly

查看:34
本文介绍了PaintComponent 无法正确绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作跳棋游戏,但以下架构未在 JFrame 上显示我做错了什么

I'm trying to make checkers game but the following architecture does not show on JFrame what I'm doing wrong

//基类

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;

public abstract class   Case extends JComponent implements MouseListener {
    
     /**
     * 
     */
    private static final long serialVersionUID = 1L;
    protected static final int LONGUEUR=40; 
    protected  int x,y,width,height;   
    
     
    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub
        
    }
}

//黑色矩形

import java.awt.Color;
import java.awt.Graphics;


public class CaseNoire extends Case
{
/**
     * 
     */
    private static final long serialVersionUID = 1L;
    public CaseNoire(int x, int y,int width,int height)
    {

        this.x=x;
        this.y=y;  
        this.width = width;   
        this.height= height;
    }

@Override
protected void paintComponent(Graphics g)
{   
    g.setColor(Color.black);      
    g.fillRect(x, y,width,height);  
}
}

//白色矩形

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;


public class CaseBlanche extends Case {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public CaseBlanche(int x, int y,int width,int height)
    {

        this.x=x;
        this.y=y;  
        this.width = width;   
        this.height= height;
    
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.white);      
        g.fillRect(x, y,width,height);   
    }

}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;

import javax.swing.JComponent;
import javax.swing.JPanel;

public class Plateau extends JComponent
{
    private Case[][] cases= new Case[10][10];  
    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);                
           for(int i=0; i<10;  i++)
           {
               for(int j=0;j<10;j++)
               {
                   if((i+j)%2==0)
                   {
                       CaseBlanche blanche= new CaseBlanche(j*Case.LONGUEUR,i*Case.LONGUEUR,Case.LONGUEUR, Case.LONGUEUR);
                       //cases[i][j]=blanche;
                       add(blanche); 
                       //g.setColor(Color.white);      
                       // g.fillRect(j*40, i*40,40,40); 
                   }
                   else
                   {
                       CaseNoire caseNoire=new CaseNoire(j*Case.LONGUEUR,i*Case.LONGUEUR,Case.LONGUEUR, Case.LONGUEUR);
                   add(caseNoire);
                     
                     
                   }                   
                  
               }
           }                        
    }
}

这里是主类
导入 javax.swing.JFrame;

and here the Main Class
import javax.swing.JFrame;

public class MainDamesFrame extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    public static void main(String[] args )
    {
        MainDamesFrame damesFrame = new MainDamesFrame(); 

        
    } 
    public  MainDamesFrame()    
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Jeu de Dames");                           
        this.getContentPane().add(new Plateau());       
        setSize(800, 600);
        setVisible(true);                   
        
    }

}

我期待这样的事情

那么我的代码有什么问题?为什么 CaseBlancheCaseNoire 中的 paintComponent 表现不如预期?
请帮忙

So what's wrong with my code? why paintComponent in CaseBlanche and CaseNoire does not behave as expected?
Please help

推荐答案

public void paintComponents(Graphics g)

你打错了.去掉paintComponents()中的"s".

You have a typo. Get rid of the "s" in paintComponents().

setSize(800, 600);

这不会像您预期的那样工作,因为 (880, 600) 包含标题栏和边框,因此某些方块将无法正确绘制.

This won't work as you expected because the (880, 600) included the title bar and borders, so some of the squares will not be painted properly.

因此,您的自定义绘画类应覆盖 getPreferredSize() 方法以返回每个组件的大小.然后您可以使用 pack() 方法来确保组件的大小正确.

So, your custom painting classes should override the getPreferredSize() method to return the size of each component. Then you can use the pack() method to make sure the components are properly sized.

不需要 CaseNoir 和 CaseBlanche 类.当您创建 Case 对象时,您可以使用:

There is no need for the CaseNoir and CaseBlanche classes. When you create the Case object you can just use:

setBackground(Color.BLACK);

然后当你绘制你刚使用的组件时:

Then when you paint the component you just use:

g.setColor( getBackground() );

另外,你的高原课程是错误的.PaintComponent() 方法用于绘画.您的代码正在创建和添加组件组件.如果您想添加组件,那么您应该使用布局管理器.只要实现了 getPreferredSize() 方法,GridLayout 就可以轻松地在网格中添加组件.您创建具有指定宽度/高度的组件,因此要实现此方法,您可以执行以下操作:

Also, your Plateau class is wrong. The paintComponent() method is for painting. Your code is creating and adding components components. If you want to add components then you should be using a layout manager. The GridLayout will easily add components in a grid as long as the getPreferredSize() method has been implemented. You create the component with a specified width/height, so to implement this method you do something like:

@Override
public Dimension getPreferredSize()
{
    return new Dimension(width, height)
}

查看此棋盘示例,了解在网格中添加组件的方法.

Check out this Chessboard Example for a way to add components in a grid.

这篇关于PaintComponent 无法正确绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆