Java 从计算机上的某个保存位置输入图像
我有一个绘图功能Java 从计算机上的某个保存位置输入图像,java,user-interface,methods,Java,User Interface,Methods,我有一个绘图功能 public void drawBoard(Graphics g) { int height = this.getHeight(); int width = this.getWidth(); int dx = width / 7; int dy = height / 6; for (int x = 0, row = 0; x <= width && row < gameboard.length; row++, x += dx)
public void drawBoard(Graphics g) {
int height = this.getHeight();
int width = this.getWidth();
int dx = width / 7;
int dy = height / 6;
for (int x = 0, row = 0; x <= width && row < gameboard.length; row++, x += dx) {
for (int col = 0, y = 0; y <= height&& col < gameboard[0].length; y += dy, col++) {
if (gameboard[row][col] == 0) {
g.setColor(Color.GRAY);
g.fillOval(y, x, dy, dx);
} else if (gameboard[row][col] == 1) {
g.setColor(Color.RED);
g.fillOval(y, x, dy, dx);
} else if(gameboard[row][col] == 1){
g.setColor(Color.BLACK);
g.fillOval(y, x, dy, dx);
} else if(gameboard[row][col]==3){
}else if(gameboard[row][col]==4){
}else if(gameboard[row][col]==5){
}else if(gameboard[row][col]==6){
}else if(gameboard[row][col]==7){
}else if(gameboard[row][col]==8){
}else if(gameboard[row][col]==9){
}
}
}
}
public void绘图板(图g){
int height=this.getHeight();
int width=this.getWidth();
int dx=宽度/7;
int dy=高度/6;
对于(int x=0,row=0;x首先,您需要将图像作为BuffereImage获取。我建议使用:
接下来,您要使用绘制图像。您的注释表明您认为需要缩放图像,因此请使用适当的drawImage方法来完成此操作。使用上面代码中的图形对象g,这可能如下所示:
int oldWidth = img.getWidth();
int oldHeight = img.getHeight();
int newWidth = 10; //You decide this...
int newHeight = 10; //You decide this too...
g.drawImage(img, 0, 0, oldWidth, newWidth, 0, 0, newWidth, newHeight, null);
Oracle本身有一个小程序,它演示了这种方法的代码,以及您可能希望在其代码中对图像执行的许多其他常见操作是从web下载的图像a)存储在本地文件中-例如,下载是在您的程序之外/手动b)在web上完成的,并且应该由您的程序从URL下载?下载是在程序之外完成的。我还认为我需要重新调整照片的大小,以适合它们进入的位置
int oldWidth = img.getWidth();
int oldHeight = img.getHeight();
int newWidth = 10; //You decide this...
int newHeight = 10; //You decide this too...
g.drawImage(img, 0, 0, oldWidth, newWidth, 0, 0, newWidth, newHeight, null);