在本论坛里找到的加载BMP图像代码,我要做的是把封闭的区域填充成一个随机色.
使用的是十字方向的像素判断,用数组模拟栈(我不会在JAVA中用栈),但填充不上(在MFC下能实现),也就是说问题出在JAVA上,我会的有限,请路过的神仙帮一下,谢谢.
代码如下:
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.awt.Robot;
import javax.imageio.ImageIO;
/*
* BoundFill.java
*
* Created on 2007年10月9日, 下午1:37
*/
/**
*
* @author freeforever
*/
public class BoundFill extends javax.swing.JFrame {
/** Creates new form BoundFill */
public BoundFill() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jLabel1MouseClicked(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
Image image =null;
try{
image=ImageIO.read(new File("f:/12.bmp"));
}catch(IOException ex){}
jLabel1.setIcon(new javax.swing.ImageIcon(image));//加载图片
}//GEN-LAST:event_formComponentShown
private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel1MouseClicked
Point posCurrent;
posCurrent=getMousePosition();
try
{
robot = new Robot();
point = MouseInfo.getPointerInfo().getLocation();//坐标
color = robot.getPixelColor(point.x, point.y);//取色
}
catch(Exception ex)
{
ex.printStackTrace();
}
g=getGraphics();
Color FillColor=new Color(
(int)(Math.random()*255),
(int)(Math.random()*255),
(int)(Math.random()*255)
);
g.setColor(FillColor);//设置填充色
Point posStk[] = new Point [65535];//模拟栈的点
Point rePos,pos4;//判断用的点和判断的十字方向点
int top=0;
posStk[++top] = posCurrent;//原始点入栈
while(top!=0)
{
rePos=posStk[top--];/*取栈顶元素,并模拟出栈*/pos4=rePos;//初始化
g.drawLine(rePos.x,rePos.y,rePos.x,rePos.y);//着色该点
pos4.x=rePos.x+1; pos4.y=rePos.y;//右边的点
if(robot.getPixelColor(pos4.x, pos4.y)==color)//与鼠标取色相同
posStk[++top] = pos4;//入栈
g.drawString(""+color,30,48);//调试信息,原始色
g.drawString(""+robot.getPixelColor(pos4.x, pos4.y),30,58);//调试信息
pos4.x=rePos.x; pos4.y=rePos.y-1;//上边的点
if(robot.getPixelColor(pos4.x, pos4.y)==color)
posStk[++top] = pos4;
g.drawString(""+robot.getPixelColor(pos4.x, pos4.y),30,68);//调试信息
pos4.x=rePos.x-1; pos4.y=rePos.y;//左边的点
if(robot.getPixelColor(pos4.x, pos4.y)==color)
posStk[++top] = pos4;
g.drawString(""+robot.getPixelColor(pos4.x, pos4.y),30,78);//调试信息
pos4.x=rePos.x; pos4.y=rePos.y+1;//左边的点
if(robot.getPixelColor(pos4.x, pos4.y)==color)
posStk[++top] = pos4;
g.drawString(""+robot.getPixelColor(pos4.x, pos4.y),30,88);//调试信息
}
}//GEN-LAST:event_jLabel1MouseClicked
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BoundFill().setVisible(true);
}
});
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
// 变量声明结束//GEN-END:variables
private Graphics g;
private Robot robot;
private Color color = new Color(0, 0, 0);
private Point point;
}
在NetBeans5.5.1中文版下写的代码
[此贴子已经被作者于2007-10-11 21:35:38编辑过]