public boolean mutate() { copyImage(_lastGeneration, _tempGeneration); int centerX = _random.nextInt(_smallWidth); int centerY = _random.nextInt(_smallHeight); for (int id = 0; id < 4; id++) { _xs[id] = (int) (centerX + _random.nextInt(_smallWidth) * X_SIGNS[id] * _random.nextDouble()); _ys[id] = (int) (centerY + _random.nextInt(_smallWidth) * Y_SIGNS[id] * _random.nextDouble()); } Color color = new Color( _random.nextInt(256), _random.nextInt(256), _random.nextInt(256), _random.nextInt(256)); _tempGfx.setColor(color); _tempGfx.fillPolygon(_xs, _ys, 4); long fitness = _comparer.compare(_tempGeneration); if (fitness <= _lastFitness) { _lastFitness = fitness; copyImage(_tempGeneration, _lastGeneration); for (int i = 0; i < 4; i++) { _xs[i] /= SCALE; _ys[i] /= SCALE; } _finalGfx.setColor(color); _finalGfx.fillPolygon(_xs, _ys, 4); return true; } else return false; }
public GeneratedTest(BufferedImage base) { _baseImage = base; _smallWidth = (int) (base.getWidth() * SCALE); _smallHeight = (int) (base.getHeight() * SCALE); _smallBaseImage = new BufferedImage(_smallWidth, _smallHeight, BufferedImage.TYPE_INT_ARGB); _smallBaseImage.createGraphics().drawImage(base, 0, 0, _smallWidth, _smallHeight, null); _comparer = new ImageComparer(_smallBaseImage); _lastGeneration = new BufferedImage(_smallWidth, _smallHeight, BufferedImage.TYPE_INT_RGB); _tempGeneration = new BufferedImage(_smallWidth, _smallHeight, BufferedImage.TYPE_INT_RGB); _tempGfx = _tempGeneration.createGraphics(); _finalImage = new BufferedImage( _baseImage.getWidth(), _baseImage.getHeight(), BufferedImage.TYPE_INT_RGB); _finalGfx = _finalImage.createGraphics(); _finalGfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); _lastFitness = _comparer.compare(_lastGeneration); System.out.println("Scaled to " + _smallWidth + "x" + _smallHeight); }