「java二维图形」java编写一个表示二维平面

博主:adminadmin 2022-12-27 23:33:11 53

本篇文章给大家谈谈java二维图形,以及java编写一个表示二维平面对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用JAVA定义一个二维数组的地图

地图拥用个二维数组,A,B的逻辑分别判断,Aif(i(数组高度)= 0,j != 0 )(j--),if(j = 0,i = 0)(i ++),if(i != 0,j == 0)(j ++)if(i == 10, j == 10)(i --)B的逻辑与A反下,就可以了

JAVA程序编写。 定义一个抽象类shape ,用来表示一般图形。

Java程序:

public class Main {

public static void main(String[] args) {

Shape s = null;

s = new Circle(3);

System.out.println("圆的面积:" + s.area());

System.out.println("圆的周长:" + s.perimeter());

}

}

/**

 * 形状类:抽象类

 * @author developer

 * @version 2017.05.23

 */

abstract class Shape {

/**

 * 计算形状的面积

 * @return 形状的面积

 */

abstract double area();

/**

 * 计算形状的周长

 * @return 形状的周长

 */

abstract double perimeter();

}

/**

 * 圆类

 * @author developer

 * @version 2017.05.23

 */

class Circle extends Shape {

/**

 * 半径

 */

protected double radius;

/**

 * 构造方法

 * @param radius 半径

 */

public Circle(double radius) {

this.radius = radius;

}

@Override

double area() {

return Math.PI * radius * radius;

}

@Override

double perimeter() {

return 2 * Math.PI * radius;

}

}

运行测试:

圆的面积:28.274333882308138

圆的周长:18.84955592153876

java 2d绘图时如何让所有图形左移一个单位

Graphics有translate方法,先调用它把你的画笔平移,之后绘制出来的东西自然也是平移过的,或者你自己计算坐标也可以

java做的一个二维曲线坐标图

方法1、对数坐标系下画图;semilogx,semilogy,loglog

方法2、求差值并画图;

方法3、局部放大;

Java如何读取BMP的每个像素点,输出到一个二维数组

楼上的基本正确,

问题一:

int[] rgb = new int[3];最好用二维数组

int[] rgb = new int[3][width*height]

问题二:

rgb[0] = (pixel 0xff0000 ) 16 ;

rgb[1] = (pixel 0xff00 ) 8 ;

rgb[2] = (pixel 0xff );

会把数组内的值覆盖,获得就是最后像素点的RGB值;

我写了一个希望可以帮助到你

package imageReadAndWrite;

import java.awt.image.BufferedImage;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageDecoder;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* JPG File reader/writer. Uses native com.sun libraries (which may deprecate at

* any time)

*

*

* @author PhoenixZJG

* @version 1.0

*/

public class JPGFile implements xxxFile {

private int[] ints = null;

private byte bytes[] = null; // bytes which make up binary PPM image

private double doubles[] = null;

private int[][] imageRGB = null;

private String filename = null; // filename for PPM image

private int height = 0;

private int width = 0;

/**

* Read the PPM File.

*

* @throws FileNotFoundException

* if the directory/image specified is wrong

* @throws IOException

* if there are problems reading the file.

*/

public JPGFile(String filename) throws FileNotFoundException, IOException {

this.filename = filename;

readImage();

}

/**

* Get the height of the PPM image.

*

* @return the height of the image.

*/

public int getHeight() {

return height;

}

/**

* Get the width of the PPM image.

*

* @return the width of the image.

*/

public int getWidth() {

return width;

}

/**

* Get the data as byte array. Data is of any type that has been read from

* the file (usually 8bit RGB)

*

* @return The data of the image.

*/

public byte[] getBytes() {

return bytes;

}

/**

* Get the data as double array. Data is of any type that has been read from

* the file (usually 8bit RGB put into an 64bit double)

*

* @return The data of the image.

*/

public double[] getDouble() {

return doubles;

}

/**

* Get the data as double array. Data is of any type that has been read from

* the file (usually 8bit RGB put into an 64bit double)

*

* @return The data of the image.

*/

public int[] getInt() {

return ints;

}

/**

* Get the data as integer array. Data is of any type that has been read from

* the file (usually 8bit RGB put into an 64bit double)

*

* @return The data of the image.

*/

public int[][] getImageRGB() {

return imageRGB;

}

/**

* Write to codefn/code file the codedata/code using the

* codewidth, height/code variables. Data is assumed to be 8bit RGB.

*

* @throws FileNotFoundException

* if the directory/image specified is wrong

* @throws IOException

* if there are problems reading the file.

*/

public static void writeImage(String fn, int[] data, int width, int height)

throws FileNotFoundException, IOException {

FileOutputStream fOut = new FileOutputStream(fn);

JPEGImageEncoder jpeg_encode = JPEGCodec.createJPEGEncoder(fOut);

BufferedImage image = new BufferedImage(width, height,

BufferedImage.TYPE_INT_RGB);

image.setRGB(0, 0, width, height, data, 0, width);

jpeg_encode.encode(image);

fOut.close();

}

/**

* Read the image from the specified file.

*

* @throws FileNotFoundException

* pretty obvious

* @throws IOException

* filesystem related problems

*/

private void readImage() throws FileNotFoundException, IOException {

FileInputStream fIn = new FileInputStream(filename);

JPEGImageDecoder jpeg_decode = JPEGCodec.createJPEGDecoder(fIn);

BufferedImage image = jpeg_decode.decodeAsBufferedImage();

width = image.getWidth();

height = image.getHeight();

int[] rgbdata = new int[width * height];

image.getRGB(0, 0, width, height, rgbdata, 0, width);

ints = rgbdata;

bytes = new byte[rgbdata.length];

doubles = new double[rgbdata.length];

imageRGB = new int[3][rgbdata.length];

for (int i = 0; i bytes.length; i++) {

bytes[i] = (byte) (rgbdata[i] 0xFF);

doubles[i] = (double) (rgbdata[i]);

imageRGB[0][i] = (rgbdata[i] 16711680) 16;

imageRGB[1][i] = (rgbdata[i] 65280) 8;

imageRGB[2][i] = (rgbdata[i] 255);

}

}

}

上述代码可以复制,粘贴使用,有方法的注视,getImageRGB() 就可以获得所有像素的RGB值,你就可以在其他方法里处理这个二维数组,得到你想要的平均值了,处理后的值,记得把值逆运算,再转换到Int[]里,就可以用,writeImage()输出图像了。

java如何判断二维数组中多个元素组成的图形是否是连通的

可以用搜索算法去做,

从某一个位置开始搜索下一个相连的位置,如果存在图形,继续搜索,直到最后,

否则返回false

关于java二维图形和java编写一个表示二维平面的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。