| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1068 人关注过本帖
标题:java做五子棋的问题,请教高手
只看楼主 加入收藏
luoweijun
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:67
专家分:138
注 册:2009-6-12
结帖率:64.71%
收藏
 问题点数:0 回复次数:3 
java做五子棋的问题,请教高手
在画好棋盘后,通过drawImage函数来画棋子,在监听调用的时候不能做到,但是单独通过给定坐标却能在棋盘上边画到一个棋子,不知道是哪里出了问题,请教下高手,在线等待
搜索更多相关主题的帖子: 五子棋 java 
2010-08-04 19:30
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:0 
最好贴出你的代码,让大家一起研究下

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2010-08-04 19:54
syg5434
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:山沟沟
等 级:版主
威 望:10
帖 子:285
专家分:781
注 册:2010-2-28
收藏
得分:0 
我写过一个,也用drawImage,没有出什么问题啊,不过功能不完整,图片也没法给你,
残缺代码你参考下,希望能有所帮助:
程序代码:
package day716;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import import import import import import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.import org.import org.import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class FiveFrame extends JFrame {

    JButton start = new JButton("开始");

    JButton back = new JButton("悔棋");

    static JButton save = new JButton("保存");

    JButton show = new JButton("回放");

    JPanel panel1 = new JPanel();

    JPanel boardPanel = new BoardPanel();// 棋盘面板

    // 棋盘、棋子图片
    BufferedImage imgBoard, imgBlack, imgWhite;

    int[][] board = new int[15][15];

    int currentColor = BLACK;

    static final int BLACK = -1;

    static final int WHITE = 1;

    // 保存走棋顺序的集合
    List<String> steps = new ArrayList<String>();

    public FiveFrame() throws Exception {
        imgBoard = ImageIO.read(FiveFrame.class
                .getResourceAsStream("/image/chessboard.jpg"));
        imgBlack = ImageIO.read(FiveFrame.class
                .getResourceAsStream("/image/b.gif"));
        imgWhite = ImageIO.read(FiveFrame.class
                .getResourceAsStream("/image/w.gif"));

        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                start();
            }
        });

        back.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                back();
            }
        });

        save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    save();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        show.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                show();
            }
        });

        panel1.add(start);
        panel1.add(back);
        panel1.add(save);
        panel1.add(show);
        // 在棋盘面板上绘制棋盘图片
        // boardPanel.getGraphics().drawImage(imgBoard,0,0,null);

        this.add(panel1, BorderLayout.NORTH);
        this.add(boardPanel);

        this.setSize(540, 590);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle("五子棋");
    }

    public void playChess(int row, int col) {
        if (row < 0 || col < 0 || row > 14 || col > 14)
            return;
        if (board[row][col] != 0)
            return;
        board[row][col] = currentColor;
        currentColor = -currentColor;

        // 放入走棋步骤集合
        steps.add(row + "," + col);
    }

    // 开始新游戏的方法
    public void start() {
        board = new int[15][15];// 新棋盘
        currentColor = BLACK;// 黑棋先行
        boardPanel.repaint();// 重新绘制面板
    }

    // 悔棋方法
    public void back() {
        if (steps.isEmpty())
            return;
        String s = steps.remove(steps.size() - 1);
        String[] arr = s.split(",");
        int row = Integer.parseInt(arr[0]);
        int col = Integer.parseInt(arr[1]);
        board[row][col] = 0;
        currentColor = -currentColor;
        boardPanel.repaint();
    }

    // 内部类,棋盘panel
    class BoardPanel extends JPanel {
        public BoardPanel() {
            this.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent e) {
                    playChess(e.getY() / 35, e.getX() / 35);
                    repaint();
                }
            });
        }

        // 演示Demo
        public void paintChess(int x, int y) {
            int row = y / 35 * 35 + 4;
            int col = x / 35 * 35 + 4;
            Graphics g = getGraphics();
            g.drawImage(imgBlack, col, row, null);
        }

        // 重写绘制panel的方法
        // 在屏幕上绘制自身的方法
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);// 执行父类中的代码将组件绘制
            // 等组件绘制完成,接着绘制自己的内容
            g.drawImage(imgBoard, 0, 0, null);
            // 画走过的棋子
            for (int row = 0; row < board.length; row++) {
                for (int col = 0; col < board[0].length; col++) {
                    if (board[row][col] != 0) {
                        BufferedImage img = imgBlack;
                        if (board[row][col] == WHITE) {
                            img = imgWhite;
                        }
                        g.drawImage(img, col * 35 + 4, row * 35 + 4, null);// 列计算x坐标,行计算y坐标
                    }
                }
            }
        }
    }

    // 保存游戏
    public void save() throws Exception {
        System.out.println(steps);
        Document doc = null;
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
                new File("/home/soft13/Desktop/myjava/five.xml"), true));
        doc = DocumentHelper.createDocument();
        Element five = doc.addElement("five");
        int i = 0;
        for (String str : steps) {

            Element step = five.addElement("step");
            Element row = step.addElement("row");
            Element col = step.addElement("col");
            Element color = step.addElement("color");

            str = steps.get(i);
            String[] str1 = str.split(",");
            step.addAttribute("id", i + "");
            row.addText(str1[0]);
            col.addText(str1[1]);
            i++;
            color.addText(-currentColor + "");
            currentColor = -currentColor;
            boardPanel.repaint();
            // writer.close();
            // writer.flush();

        }
        OutputFormat f = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, f);
        writer.write(doc);
        writer.close();

    }

    /*
    public void show() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            //FiveFrame ff1 = new FiveFrame();
            DefaultHandler dh = new DefaultHandler();
            parser.parse("/home/soft13/Desktop/myjava/five.xml", dh);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    public static void main(String[] args) throws Exception {
        // new FiveFrame().setVisible(true);
        FiveFrame ff = new FiveFrame();
        ff.setVisible(true);
    }

}
class Chess{
    public static final String XPATH_STEP="/five/step";
    public static final String XPATH_ROL="/five/step/rol";
    public static final String XPATH_COW="/five/step/cow";
    public static final String XPATH_COLOR="/five/step/color";
   

    private String step;
    private String rol;
    private String cow;
    private String color;
   

    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getCow() {
        return cow;
    }
    public void setCow(String cow) {
        this.cow = cow;
    }
    public String getRol() {
        return rol;
    }
    public void setRol(String rol) {
        this.rol = rol;
    }
    public String getStep() {
        return step;
    }
    public void setStep(String step) {
        this.step = step;
    }
   

   

}



不要迷恋哥,嫂子会揍你。阿-弥-陀-佛--!
2010-08-04 21:02
东方一帅
Rank: 2
等 级:论坛游民
帖 子:18
专家分:34
注 册:2010-7-23
收藏
得分:0 
  好多代码。。。
2010-08-14 22:16
快速回复:java做五子棋的问题,请教高手
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022867 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved