Java画图第一次和第二次怎么画的位置不一样?

主类初始化时图片:
图片说明
调用new MyPanel().paint(getGraphics());时的图片:

package wzq;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Wzq extends JFrame //五子棋  
{
    private static final long serialVersionUID = 1L;
    static final int NONE=0;  //空常量
    static final int WHITE=1; //表示颜色的常量,白色
    static final int BLACK=2; //表示颜色的常量,黑色
    static final int PVP=3;   //模式常量,两个玩家对下
    static final int PVAI=4;  //模式常量,玩家和电脑下
    static final Color LBROWN=new Color(220,210,134); //黄棕色
    static final Color LBLACK=new Color(20,20,20); //灰黑色
    static final Color DWHITE=new Color(240,240,240); //灰白色
    Mark[][] marks=new Mark[29][29];   //棋子数组(29*29,剩下10格为了方便遍历不出错而空出)
    Container container=getContentPane();
    int player=BLACK;  //此时下棋的人(黑白)
    double mx=0; //鼠标坐标
    double my=0;
    int x1=0;  //鼠标在棋盘上的坐标
    int y1=0;
    int mode=PVP; //游戏模式(玩具对下或玩具和电脑下)
    public class MyPanel extends JPanel
    {
        private static final long serialVersionUID = 1L;
        public void paint(Graphics g1)
        {
            super.paint(g1);
            Graphics2D g=(Graphics2D)g1;
            g.setColor(LBROWN);          //绘制棋版↓
            g.fillRect(0,100,400,400); 
            g.setColor(Color.BLACK);
            for(int i=0;i<19;i++)
            {
                g.drawLine(i*20+10,100,i*20+10,500);  //画棋盘线
                g.drawLine(0,i*20+110,400,i*20+110);
            }
            for(int x=0;x<3;x++) //画棋盘上的点
            {
                for(int y=0;y<3;y++)
                    g.fillOval(x*20*7+45,y*20*7+145,10,10);
            }
            g.setColor(Color.RED);
            g.drawRect(0,100,380,380);  //绘制棋盘方框   绘制棋盘↑
            for(int x=0;x<19;x++)
            {
                for(int y=0;y<19;y++)
                    marks[x+5][y+5].draw(g,x,y);
            }
        }
    }
    public class ListenMouse implements MouseListener //鼠标监听器
    {
        public void mouseClicked(MouseEvent e) 
        {
            Point p=MouseInfo.getPointerInfo().getLocation();
            mx=p.getX()-getX();
            my=p.getY()-getY();
            if(my<100)
                return;
            x1=(int)(mx/20);  //把鼠标坐标转换为屏幕上的坐标
            y1=(int)((my-100)/20);
            if(mode==PVP)  //玩具对下的时候下棋
            {
                if(!addMark(x1,y1,player))
                    return;
                if(player==BLACK)
                    player=WHITE;
                else if(player==WHITE)
                    player=BLACK;
            }
            new MyPanel().paint(getGraphics());
        }
        public void mouseEntered(MouseEvent e) {} 
        public void mouseExited(MouseEvent e) {}
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
    }
    class Mark //棋子  
    {
        int color;  //此棋的颜色
        public Mark(int _player)  //玩家(白,黑或空)
        {
            color=_player;
            if(player==BLACK)  //一方下棋后,将下一次的下棋者设为另一方
                player=WHITE;
            else if(player==WHITE)
                player=BLACK;
        }
        void draw(Graphics2D g,int x,int y)  //在棋盘上画上自己
        {
            switch(color)
            {   
            case BLACK:
                g.setColor(LBLACK);
                break;
            case WHITE:
                g.setColor(DWHITE);
                break;
            case NONE:
                return;
            }
            g.fillOval(x*20+1,y*20+101,18,18);
        }
    }
    boolean addMark(int x,int y,int color) //添加棋子(返回值为是否添加成功,如果位置有棋子就失败)
    {
        if(marks[x+5][y+5].color!=NONE)
            return false;
        marks[x+5][y+5].color=color;
        return true;
    }
    public Wzq()
    {
        for(int x=0;x<29;x++)  //给列表初始化,添加空棋子
        {
            for(int y=0;y<29;y++)
                marks[x][y]=new Mark(NONE);
        }
        setTitle("五子棋");
        setBounds(200,100,387,509);
        setResizable(false);
        setLayout(null);
        setContentPane(new MyPanel());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        addMouseListener(new ListenMouse());
    }
    public static void main(String[] args)
    {
        new Wzq();
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐