画像の上にグリッドを配置したい。JLabel
イメージを使用するメソッドを保持するメソッドpaintComponent
を使用しました。このメソッドを使用したのは、プロジェクトのさまざまなフェーズで、グリッドの描画に役立つJLayer
クラス でイメージが変化するためです (小さな例では、メソッドのみを使用しました)。GridDrawer extends LayerUI
drawRect()
私のコード:
GridPhoto(メイン) クラス:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gridphoto;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
/**
*
* @author VJ
*/
public class GridPhoto {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
new GUI();
} catch (IOException ex) {
Logger.getLogger(GridPhoto.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
GUI クラス:
package gridphoto;
import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JLayer;
import javax.swing.plaf.LayerUI;
public class GUI {
JFrame frame;
JPanel panel;
JLayer<JLabel> GridLayer;
JLabel imagelabel;
LayerUI<JLabel> GridUI;
BufferedImage img;
public GUI() throws IOException {
frame = new JFrame("GridImage Test");
panel = new JPanel();
img = ImageIO.read(new File("/Users/VJ/Desktop/gs.png"));
imagelabel = new JLabel() {
public void paintComponent(Graphics g) {
g.drawImage(img.getScaledInstance(500, 500, BOTTOM), 0, 0, null);
}
};
GridUI = new GridDrawer();
GridLayer = new JLayer(imagelabel, GridUI);
panel.setLayout(new BorderLayout());
frame.setLayout(new BorderLayout());
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 700);
panel.add(GridLayer);
frame.add(panel);
frame.setVisible(true);
}
public class GridDrawer extends LayerUI<JLabel> {
public void paintComponent(Graphics g) {
g.drawRect(0, 0, 250, 250);
}
}
}
私の問題は、追加JLayer
しJPanel
た後でも、グリッドではなく画像のみが表示されることです。たとえば、クラスのpaintComponent
メソッドは.GridDrawer
Rectangle
JLayer
私のコードで何が間違っているか教えてください。または、グリッドを配置する以外に方法はありますImage
か?
出力。