jiang晴岚 jiang晴岚
关注数: 8 粉丝数: 3 发帖数: 536 关注贴吧数: 4
一个添色算法~ 求解答 //第一个类 import java.awt.*; import javax.swing.*; import java.awt.event.*; public class MyFrame extends JFrame{ public MyFrame(){ this.add(new DrawPanel(),BorderLayout.CENTER); } public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.setTitle("DrawColor"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500,500); frame.setVisible(true); } } class DrawPanel extends JPanel implements MouseListener,MouseMotionListener{//鼠标事件在面板上作画 int x,y; private Point linestart=new Point(0,0); public DrawPanel(){ this.addMouseListener(this); this.addMouseMotionListener(this); } public void mousePressed(MouseEvent e){//得到当前坐标  linestart.move(e.getX(),e.getY());  }     public void mouseDragged(MouseEvent e){//用鼠标画图  Graphics g=getGraphics();  if(e.isMetaDown()){//点击右键擦除  g.setColor(getBackground());  g.fillOval(e.getX()-10,e.getY()-10,20,20);  }else{  g.setColor(Color.BLACK);  g.drawLine(linestart.x,linestart.y,e.getX(),e.getY());  }    linestart.move(e.getX(),e.getY());  g.dispose();  }     public void mouseClicked(MouseEvent e){//点击鼠标填充颜色     DrawPanel draw=new DrawPanel(); try{Robot   robot   =   new   Robot();  Point point=new Point(draw.getX(),draw.getY()); Color interiorColor= robot.getPixelColor(draw.getX(),draw.getY());  boundaryFill4  (draw.getX(),draw.getY(),Color.RED, Color.BLACK, interiorColor);}catch(Exception ex){};  }     public void mouseReleased(MouseEvent e){    }     public void mouseEntered(MouseEvent e){    }     public void mouseExited(MouseEvent e){    }     public void mouseMoved(MouseEvent e){    }  public int getX() {//得到X,Y点坐标 return x; } public int getY() { return y; } public void setPy(int x, int y) { if (x >= 0 && y >= 0) { this.x = x; this.y = y; } } void  boundaryFill4  (int x, int y,Color fillColor, Color boarderColor,Color interiorColor)//种子填充
1 下一页