我有一个256 x 256大小的圆圈图像。可以使用函数B(i,j)创建圆圈。通过使用Java生产者和消费者模型,我如何创建一个Java程序来绘制圆圈?
此图片中的代码使用八度代码。
答案 0 :(得分:1)
这是我创建的Java Swing GUI。
我在CreateImage类中创建(或生成)了图像。我在你的问题图像中使用了代码,除了我没有采用平方根。比较数字的平方更快。
我在JPanel上绘制(或消费)了图像。我不打算详细解释Swing代码。我写了我需要写的东西来显示图像。
以下是代码:
package com.ggl.testing;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class DrawImage implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new DrawImage());
}
@Override
public void run() {
Image image = new CreateImage(256, 256, 80).createImage();
JFrame frame = new JFrame("Image of a circle");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawingPanel(256, 256, image));
frame.pack();
frame.setVisible(true);
}
public class DrawingPanel extends JPanel {
private static final long serialVersionUID = 1584647402715684757L;
private Image image;
public DrawingPanel(int width, int height, Image image) {
this.image = image;
this.setPreferredSize(new Dimension(width, height));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
public class CreateImage {
private int width;
private int height;
private int radius;
public CreateImage(int width, int height, int radius) {
this.width = width;
this.height = height;
this.radius = radius;
}
public Image createImage() {
int circleRadiusSquared = radius * radius;
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
for (int i = 0; i < width; i++) {
int idist = i - width / 2;
for (int j = 0; j < height; j++) {
int jdist = j - height / 2;
int distSquared = idist * idist + jdist * jdist;
if (distSquared < circleRadiusSquared) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
}
g.drawLine(i, j, i, j);
}
}
g.dispose();
return image;
}
}
}