难题求解
我编了一个程序,从图片文件中导入图像,黑块为障碍物。我想让小圆碰上障碍物后能沿着障碍物的边走,怎么办?(障碍物为任意图形)以下为一部分代码,请求更正(红字为重点修改对象):
class PaintThread extends JPanel implements Runnable
{
int x = 30, y = 50,f=0,n,i;
final ImageIcon imageIcon = new ImageIcon("bb.jpg");
Image image = imageIcon.getImage();
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(image,0,0,this);
g.fillOval(x, y, 30, 30);
g.drawRect(0,0,1100,600);
}
public void run()
{
try {
BufferedImage bi = ImageIO.read(new File("bb.jpg"));
do{ for(i=0;i<=120;i++)
{
n=bi.getRGB((int)(x+15*Math.cos(i*Math.PI/60-Math.PI/2)+15),(int)(y+15*Math.sin(i*Math.PI/60-Math.PI/2)+15));
if(n!=-16777216)
{
x=(int)(Math.cos((i+10)*Math.PI/60+Math.PI)*2+x+15);
y=(int)(Math.sin((i+10)*Math.PI/60+Math.PI)*2+y+15);
}
}
repaint();
try{Thread.sleep(10);}
catch(InterruptedException e){e.printStackTrace();}
}while(true);
} catch (IOException e) {e.printStackTrace();}
}
}
求大神!!
----------------解决方案--------------------------------------------------------