Za pomocą języka programowania Java wygeneruj obrazek przedstawiający chmurę, której…
- User
Za pomocą języka programowania Java wygeneruj obrazek przedstawiający chmurę, której lewa górna część jest koloru fioletowego, prawa górna część jest koloru zielonego, lewa dolna część jest koloru czerwonego, a prawa dolna część jest koloru niebieskiego. Kolory powinny płynnie przechodzić jeden w drugi, bez ostrych granic. Format obrazka ma być bitmapą (BMP).
Assistant
Oto kod w Javie, który generuje obrazek chmury z płynnym przejściem kolorów:
import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class CloudGenerator { public static void main(String[] args) { int width = 800; int height = 600; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = image.createGraphics(); // Włączenie antyaliasingu dla gładszych krawędzi g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Białe tło g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, width, height); // Generowanie chmury z gradientami drawCloud(g2d, width, height); g2d.dispose(); // Zapisanie jako BMP try { File outputFile = new File("chmura.bmp"); ImageIO.write(image, "bmp", outputFile); System.out.println("Obrazek został zapisany jako: " + outputFile.getAbsolutePath()); } catch (IOException e) { System.err.println("Błąd podczas zapisywania obrazka: " + e.getMessage()); } } private static void drawCloud(Graphics2D g2d, int width, int height) { // Centrum chmury int centerX = width / 2; int centerY = height / 2; // Rozmiar chmury int cloudWidth = 400; int cloudHeight = 200; // Tworzenie kształtu chmury z elips int numCircles = 8; double angleStep = 2 * Math.PI / numCircles; // Rysowanie chmury piksel po pikselu dla płynnych gradientów for (int x = centerX - cloudWidth/2 - 50; x < centerX + cloudWidth/2 + 50; x++) { for (int y = centerY - cloudHeight/2 - 50; y < centerY + cloudHeight/2 + 50; y++) { // Sprawdzenie czy piksel jest wewnątrz chmury if (isInsideCloud(x, y, centerX, centerY, cloudWidth, cloudHeight)) { // Obliczenie pozycji względnej w chmurze (0-1) double relX = (double)(x - (centerX - cloudWidth/2)) / cloudWidth; double relY = (double)(y - (centerY - cloudHeight/2)) / cloudHeight; // Ograniczenie wartości do zakresu 0-1 relX = Math.max(0, Math.min(1, relX)); relY = Math.max(0, Math.min(1, relY)); // Interpolacja kolorów Color topLeft = new Color(128, 0, 128); // Fioletowy Color topRight = new Color(0, 128, 0); // Zielony Color bottomLeft = new Color(255, 0, 0); // Czerwony Color bottomRight = new Color(0, 0, 255); // Niebieski // Interpolacja górnego rzędu Color topColor = interpolateColor(topLeft, topRight, relX); // Interpolacja dolnego rzędu Color bottomColor = interpolateColor(bottomLeft, bottomRight, relX); // Interpolacja między górą a dołem Color finalColor = interpolateColor(topColor, bottomColor, relY); // Dodanie efektu miękkości na krawędziach double edgeFactor = getEdgeFactor(x, y, centerX, centerY, cloudWidth, cloudHeight); int alpha = (int)(255 * edgeFactor); // Mieszanie z białym tłem Color blendedColor = blendWithBackground(finalColor, Color.WHITE, alpha); g2d.setColor(blendedColor); g2d.fillRect(x, y, 1, 1); } } } } private static boolean isInsideCloud(int x, int y, int centerX, int centerY, int width, int height) { // Sprawdzenie czy punkt jest wewnątrz kształtu chmury (połączenie elips) double dx1 = (x - centerX) / (width * 0.5); double dy1 = (y - centerY) / (height * 0.4); double dist1 = dx1 * dx1 + dy1 * dy1; double dx2 = (x - (centerX - width * 0.3)) / (width * 0.3); double dy2 = (y - centerY) / (height * 0.35); double dist2 = dx2 * dx2 + dy2 * dy2; double dx3 = (x - (centerX + width * 0.3)) / (width * 0.3); double dy3 = (y - centerY) / (height * 0.35); double dist3 = dx3 * dx3 + dy3 * dy3; double dx4 = (x - centerX) / (width * 0.4); double dy4 = (y - (centerY - height * 0.2)) / (height * 0.3); double dist4 = dx4 * dx4 + dy4 * dy4; return dist1 <= 1.0 || dist2 <= 1.0 || dist3 <= 1.0 || dist4 <= 1.0; } private static double getEdgeFactor(int x, int y, int centerX, int centerY, int width, int height) { // Obliczenie odległości od krawędzi dla efektu miękkości double dx1 = (x - centerX) / (width * 0.5); double dy1 = (y - centerY) / (height * 0.4); double dist1 = Math.sqrt(dx1 * dx1 + dy1 * dy1); double dx2 = (x - (centerX - width * 0.3)) / (width * 0.3); double dy2 = (y - centerY) / (height * 0.35); double dist2 = Math.sqrt(dx2 * dx2 + dy2 * dy2); double dx3 = (x - (centerX + width * 0.3)) / (width * 0.3); double dy3 = (y - centerY) / (height * 0.35); double dist3 = Math.sqrt(dx3 * dx3 + dy3 * dy3); double dx4 = (x - centerX) / (width * 0.4); double dy4 = (y - (centerY - height * 0.2)) / (height * 0.3); double dist4 = Math.sqrt(dx4 * dx4 + dy4 * dy4); double minDist = Math.min(Math.min(dist1, dist2), Math.min(dist3, dist4)); if (minDist >= 1.0) return 0.0; if (minDist <= 0.8) return 1.0; // Płynne przejście na krawędziach return 1.0 - (minDist - 0.8) / 0.2; } private static Color interpolateColor(Color c1, Color c2, double factor) { int r = (int)(c1.getRed() * (1 - factor) + c2.getRed() * factor); int g = (int)(c1.getGreen() * (1 - factor) + c2.getGreen() * factor); int b = (int)(c1.getBlue() * (1 - factor) + c2.getBlue() * factor); r = Math.max(0, Math.min(255, r)); g = Math.max(0, Math.min(255, g)); b = Math.max(0, Math.min(255, b)); return new Color(r, g, b); } private static Color blendWithBackground(Color foreground, Color background, int alpha) { double a = alpha / 255.0; int r = (int)(foreground.getRed() * a + background.getRed() * (1 - a)); int g = (int)(foreground.getGreen() * a + background.getGreen() * (1 - a)); int b = (int)(foreground.getBlue() * a + background.getBlue() * (1 - a)); return new Color(r, g, b); } }Ten program tworzy obrazek chmury z następującymi cechami:
- Kształt chmury - składa się z kilku nachodzących na siebie elips, tworząc naturalny kształt chmury
- Gradienty kolorów - kolory płynnie przechodzą jeden w drugi:
- Lewa górna część: fioletowa
- Prawa górna część: zielona
- Lewa dolna część: czerwona
- Prawa dolna część: niebieska
- Miękkie krawędzie - chmura ma rozmyte krawędzie dla bardziej naturalnego wyglądu
- Format BMP - obrazek jest zapisywany jako plik "chmura.bmp"
Aby uruchomić program:
- Zapisz kod w pliku
CloudGenerator.java - Skompiluj:
javac CloudGenerator.java - Uruchom:
java CloudGenerator
Program utworzy plik "chmura.bmp" w bieżącym katalogu.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.