在图像上放置网格 [英] Laying grids above images
问题描述
到目前为止我在java中实现的是要求用户从目录中上载图像。我的下一步是当图像被加载时,网格被放置在该图像上方,仅用于视觉目的,以便图像被分成10×10个网格等。我如何实现这个东西?这是我迄今为止所实施的。
JFileChooser choose = new JFileChooser();
choose.showOpenDialog(null);
文件f = choose.getSelectedFile();
String filename = f.getAbsolutePath();
path.setText(filename);
BufferedImage img;
尝试{
img = ImageIO.read(f);
Image dimg = img.getScaledInstance(500,500,Image.SCALE_SMOOTH);
ImageIcon imageIcon =新ImageIcon(dimg);
image_label.setIcon(imageIcon);
}
catch(Exception e){
System.out.println(e);
-
在面板中绘制图像
protected void paintComponent(Grapchics g){
super.paintComponent G);
g.drawImage(image,0,0,this);
}
-
然后根据你想要的单元数量,比如说10x10 ,只需在图像上绘制100个单元格(
drawRect()
)。类似于protected void paintComponent(Grapchics g){
super.paintComponent(g);
g.drawImage(image,0,0,this);
int cellHeight =(int)(getHeight()/ 10);
int cellWidth =(int)(getWidth()/ 10);
for(int y = 0; y< getWidth(); y + = cellHeight){
for(int x = 0; x< getHeight(); x + = cellWidth){
g.drawRect(x,y,cellWidth,cellHeight);
$
我没有测试过,但基本概念就在那里。您可能还想为10使用变量(一个常量)。
更新1
你可以看到精度有点off,因为我使用了
int
,但是您可以使用双打并使用Grapchics2D
Rectangle2D进行绘制。双
。我懒得改变它import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
导入javax.swing.SwingUtilities;
public class ImageGrid extends JPanel {
private static final int CELLS = 10;
BufferedImage img;
$ b public ImageGrid(){
try {
img = ImageIO.read(getClass()。getResource(/ resources / stackoverflow5.png));
} catch(IOException ex){
Logger.getLogger(ImageGrid.class.getName()).log(Level.SEVERE,null,ex);
}
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(img!= null){
g.drawImage(img,0,0,this);
int cellHeight =(int)(getHeight()/ CELLS);
int cellWidth =(int)(getWidth()/ CELLS);
for(int y = 0; y< getHeight(); y + = cellHeight){
for(int x = 0; x< getWidth(); x + = cellWidth){
g.drawRect(x,y,cellWidth,cellHeight);
$覆盖
public Dimension getPreferredSize(){
return img == null? new Dimension(300,300)
:new Dimension(img.getWidth(),img.getHeight());
public static void main(String [] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
)JFrame frame = new JFrame();
JPanel wrapperPanel = new JPanel(new GridBagLayout());
wrapperPanel.add(new ImageGrid());
frame.add(wrapperPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
code
$ bUPDATE 2
WithJLabel
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
导入javax.swing.SwingUtilities;
public class ImageGrid extends JLabel {
private static final int CELLS = 10;
BufferedImage img;
$ b public ImageGrid(){
try {
img = ImageIO.read(getClass()。getResource(/ resources / stackoverflow5.png));
setIcon(new ImageIcon(img));
} catch(IOException ex){
Logger.getLogger(ImageGrid.class.getName()).log(Level.SEVERE,null,ex);
}
}
@Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
if(img!= null){
int cellHeight =(int)(getHeight()/ CELLS);
int cellWidth =(int)(getWidth()/ CELLS);
for(int y = 0; y< getHeight(); y + = cellHeight){
for(int x = 0; x< getWidth(); x + = cellWidth){
g.drawRect(x,y,cellWidth,cellHeight);
$ b public static void main(String [] args){
SwingUtilities.invokeLater (new Runnable(){
public void run(){
JFrame frame = new JFrame();
JPanel wrapperPanel = new JPanel(new GridBagLayout());
wrapperPanel。 add(new ImageGrid());
frame.add(wrapperPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo( null);
frame.setVisible(true);
}
});
}
}
What I have implemented till now in java is ask the user to upload an image from the directory. My next step is that when the image is loaded a grid is placed above that image just for visual purpose so that the image gets divided in a, say 10 x 10 grids. How do I implement this stuff? Here's what I have implemented till now.
JFileChooser choose=new JFileChooser(); choose.showOpenDialog(null); File f=choose.getSelectedFile(); String filename=f.getAbsolutePath(); path.setText(filename); BufferedImage img; try { img=ImageIO.read(f); Image dimg = img.getScaledInstance(500,500,Image.SCALE_SMOOTH); ImageIcon imageIcon = new ImageIcon(dimg); image_label.setIcon(imageIcon); } catch(Exception e) { System.out.println(e); }
解决方案paint the image in a panel
protected void paintComponent(Grapchics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); }
Then based on the the number of cells you want, say 10x10, just draw 100 cells (
drawRect()
) over the image. Something likeprotected void paintComponent(Grapchics g) { super.paintComponent(g); g.drawImage(image, 0, 0, this); int cellHeight = (int)(getHeight() / 10); int cellWidth = (int)(getWidth() / 10); for (int y = 0; y < getWidth(); y += cellHeight) { for (int x = 0; x < getHeight(); x += cellWidth){ g.drawRect(x, y, cellWidth, cellHeight); } } }
I haven't test it, but the basic concept is there. You may also want to use variables (a constant probably) for the 10.
UPDATE 1
You can see the precision's a little off because I used
int
, but you can use doubles and draw by usingGrapchics2D
Rectangle2D.Double
. I'm too lazy to change itimport java.awt.Dimension; import java.awt.Graphics; import java.awt.GridBagLayout; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ImageGrid extends JPanel { private static final int CELLS = 10; BufferedImage img; public ImageGrid() { try { img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png")); } catch (IOException ex) { Logger.getLogger(ImageGrid.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) { g.drawImage(img, 0, 0, this); int cellHeight = (int) (getHeight() / CELLS); int cellWidth = (int) (getWidth() / CELLS); for (int y = 0; y < getHeight(); y += cellHeight) { for (int x = 0; x < getWidth(); x += cellWidth) { g.drawRect(x, y, cellWidth, cellHeight); } } } } @Override public Dimension getPreferredSize() { return img == null ? new Dimension(300, 300) : new Dimension(img.getWidth(), img.getHeight()); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); JPanel wrapperPanel = new JPanel(new GridBagLayout()); wrapperPanel.add(new ImageGrid()); frame.add(wrapperPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
UPDATE 2
WithJLabel
import java.awt.Graphics; import java.awt.GridBagLayout; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class ImageGrid extends JLabel { private static final int CELLS = 10; BufferedImage img; public ImageGrid() { try { img = ImageIO.read(getClass().getResource("/resources/stackoverflow5.png")); setIcon(new ImageIcon(img)); } catch (IOException ex) { Logger.getLogger(ImageGrid.class.getName()).log(Level.SEVERE, null, ex); } } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (img != null) { int cellHeight = (int) (getHeight() / CELLS); int cellWidth = (int) (getWidth() / CELLS); for (int y = 0; y < getHeight(); y += cellHeight) { for (int x = 0; x < getWidth(); x += cellWidth) { g.drawRect(x, y, cellWidth, cellHeight); } } } } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame(); JPanel wrapperPanel = new JPanel(new GridBagLayout()); wrapperPanel.add(new ImageGrid()); frame.add(wrapperPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } }
这篇关于在图像上放置网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!