Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPanel上的drawImage()或在GridLayout上添加图像_Java_Image_Swing_Jpanel_Grid Layout - Fatal编程技术网

Java JPanel上的drawImage()或在GridLayout上添加图像

Java JPanel上的drawImage()或在GridLayout上添加图像,java,image,swing,jpanel,grid-layout,Java,Image,Swing,Jpanel,Grid Layout,我有一个100乘100的标签网格。我有一个创建和填充字符串数组的方法。下一个方法创建一个标签数组,然后使用setText()方法将字符串(从上一个方法创建)添加到标签中。一些标签也包含图像。方法获取这些jlabel并将它们添加到网格布局的JPanel中(我们称之为x1)。然后我将JPanel添加到一个JScrollPane(x2),JScrollPane被添加到另一个带有空边框的JPanel(x3),最后的JPanel(x3)被添加到JFrame。这就是我创建网格的方式,我很高兴,我不想改变它

我有一个100乘100的标签网格。我有一个创建和填充字符串数组的方法。下一个方法创建一个标签数组,然后使用setText()方法将字符串(从上一个方法创建)添加到标签中。一些标签也包含图像。方法获取这些jlabel并将它们添加到网格布局的JPanel中(我们称之为x1)。然后我将JPanel添加到一个JScrollPane(x2),JScrollPane被添加到另一个带有空边框的JPanel(x3),最后的JPanel(x3)被添加到JFrame。这就是我创建网格的方式,我很高兴,我不想改变它

我想添加一个图像到x1-JPanel与网格布局。 为此,我必须添加paintComponent方法并使用drawImage()方法。我的问题是Eclipse如何知道将图像添加到哪个面板?我不想为x1创建一个单独的类,我以前做过,但结果不太好,我不想再走那种令人沮丧的道路,我很抱歉

我曾考虑过使用玻璃窗格,但我将无法再看到JLabels的图像,这一点非常重要

我认为将图像添加到JPanel的背景将是最好的,因为我还希望有一个显示/隐藏网格线的按钮—JLabel的边界

我希望我说的有道理

下面是代码。我知道一个类中有很多代码。我在两个不同的班上都有,但我就是没用。我觉得这容易多了。我希望你不介意

package roverMars;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

public class MenuPanel extends JPanel {

    private static final long serialVersionUID = -3928152660110599311L;

    public JPanel frame, textfield, buttons, cpPanel;
    public JTextField Commands;
    public JButton Plot, Submit, Undo;
    public JLabel Position, cpLabel;
    public Border loweredetched;
    public JCheckBox gridLines;

    public SubmitButton sub;

    static final int rows = 100, columns = 100;

    // ******IMAGES******
    static BufferedImage North, South, West, East;

    public void ImageLoader() {

        try {
            North = ImageIO.read(this.getClass().getResource("North.png"));
            South = ImageIO.read(this.getClass().getResource("South.png"));
            West = ImageIO.read(this.getClass().getResource("West.png"));
            East = ImageIO.read(this.getClass().getResource("East.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("Error occured: " + e);
            e.printStackTrace();
        }
    }

    // ******IMAGES******

    public void createMenu(JPanel p) {


        // Text Field Panel
        Commands = new JTextField(20);
        textfield = new JPanel();
        textfield.setPreferredSize(new Dimension(150, 50));
        textfield.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        textfield.setBackground(new Color(204, 153, 255));
        textfield.add(Commands);

        // Have a button next to the Text Field to clear contents.
        // Might need to give the JPanel a new Flow Layout.


        // Buttons Panel
        buttons = new JPanel();
        buttons.setPreferredSize(new Dimension(150, 250));
        buttons.setLayout(new BoxLayout(buttons, BoxLayout.Y_AXIS));
        buttons.setBackground(new Color(170, 051, 170));

        // Create and Add buttons to the Buttons Panel
        buttons.add(Box.createRigidArea(new Dimension(30, 10)));
        Plot = new JButton("Plot");
        Plot.setAlignmentX(Component.CENTER_ALIGNMENT);
        Plot.setAlignmentY(Component.CENTER_ALIGNMENT);

        buttons.add(Plot);
        buttons.add(Box.createRigidArea(new Dimension(30, 10)));
        Submit = new JButton("Submit");
        Submit.setAlignmentX(Component.CENTER_ALIGNMENT);
        Submit.setAlignmentY(Component.CENTER_ALIGNMENT);

        Submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                sub = new SubmitButton();
                sub.Submit(Commands);
                cpLabel.setText("*****SET CURRENT POSITION*****");
                labels[2][2].setIcon(new ImageIcon(North));

                // I will be able to move the rover from here using for loops
                // and if statements.

            }
        });

        buttons.add(Submit);
        buttons.add(Box.createRigidArea(new Dimension(30, 10)));
        Undo = new JButton("Undo");
        Undo.setAlignmentX(Component.CENTER_ALIGNMENT);
        Undo.setAlignmentY(Component.CENTER_ALIGNMENT);
        buttons.add(Undo);
        buttons.add(Box.createRigidArea(new Dimension(30, 10)));

        gridLines = new JCheckBox();
        gridLines.setText("Show gridlines");
        gridLines.setAlignmentX(Component.CENTER_ALIGNMENT);
        gridLines.setAlignmentY(Component.CENTER_ALIGNMENT);

        gridLines.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // Set the colour of the JLabels array from here.
                System.out.println("clicked");

            }
        });

        buttons.add(gridLines);
        buttons.add(Box.createRigidArea(new Dimension(30, 20)));


        loweredetched = BorderFactory
                .createEtchedBorder(EtchedBorder.RAISED);

        cpLabel = new JLabel("Current position: ", JLabel.CENTER);

        cpPanel = new JPanel();
        cpPanel.setBackground(new Color(153, 153, 204));
        cpPanel.add(cpLabel);
        cpPanel.setBorder(loweredetched);


        // Panel for the main window
        JPanel frame = new JPanel();
        frame.setPreferredSize(new Dimension(150, 350));
        frame.setLayout(new BorderLayout());
        frame.add(textfield, BorderLayout.NORTH);
        frame.add(buttons, BorderLayout.CENTER);


        // This Main Panel
        p.setPreferredSize(new Dimension(350, 700));
        p.setBackground(new Color(153, 153, 204));
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.setBorder(BorderFactory.createEmptyBorder(10, 50, 10, 25));
        p.add(Box.createRigidArea(new Dimension(100, 100)));
        p.add(frame);
        p.add(Box.createRigidArea(new Dimension(15, 15)));
        p.add(cpPanel);
        p.add(Box.createRigidArea(new Dimension(100, 300)));
    }


    // From line 142 to 202 is everything to do with creating the Grid
    public void StringArray(String[][] labelText) {
        int x = 1; // increment rows

        for (int i = 0; i < labelText.length; i++) { // x
            for (int j = 0; j < labelText.length; j++) { // y
                labelText[i][j] = Integer.toString(x); // populate string
                x++;
            }
        }
    }

    public void JLabelArray(JLabel[][] label, String[][] labelText) {

        for (int i = 0; i < label.length; i++) { // x
            for (int j = 0; j < label.length; j++) { // y
                label[i][j] = new JLabel();
                label[i][j].setText(labelText[i][j]);
                label[i][j].setOpaque(false);
                label[i][j].setBorder(BorderFactory.createLineBorder(new Color(
                        0, 155, 200), 1));
                // label[i][j].setBackground(Color.WHITE);

            }
        }
    }

    public void populateGrid(JPanel Grid, JLabel[][] label) { // Add Labels to
                                                                // Panel,

        String x1[][] = new String[rows][columns];
        StringArray(x1);
        JLabelArray(label, x1);

        Grid.setBackground(Color.RED);

        int gHeight = label.length, gWidth = label.length;
        Grid.setLayout(new GridLayout(gWidth, gHeight));

        for (int i = 0; i < label.length; i++) { // x
            for (int j = 0; j < label.length; j++) { // y
                Grid.add(label[i][j]);

            }
        }
    }

    public void createGrid(JPanel finalPanel, JPanel Grid) {

        // Add Grid to Scroll Pane
        JScrollPane x4 = new JScrollPane(Grid);
        x4.setPreferredSize(new Dimension(600, 600)); // DO NOT DELETE THIS.
        x4.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        x4.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        // Add Scroll Pane to another Panel with the Border
        finalPanel.setBackground(new Color(153, 153, 204));
        finalPanel.setBorder(BorderFactory.createEmptyBorder(50, 25, 50, 50));
        finalPanel.add(x4);

    }

    // Variables for creaeteGUI method.
    static MenuPanel t = new MenuPanel();
    static JPanel menu = new JPanel();
    static JPanel finalPanel = new JPanel();
    static JPanel gridPanel = new JPanel();
    static JLabel labels[][] = new JLabel[rows][columns];

    public static void createGUI() {


        t.createMenu(menu);
        t.populateGrid(gridPanel, labels);
        t.createGrid(finalPanel, gridPanel);

        JFrame f = new JFrame();
        f.setTitle("Project Testing");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setLocation(100, 100);
        f.setAlwaysOnTop(true);
        f.setSize(500, 500);

        f.add(finalPanel, BorderLayout.CENTER);
        f.add(menu, BorderLayout.WEST);

        f.pack();

    }

    public static void main(String args[]) {

        createGUI();

        t.ImageLoader();
        labels[2][2].setIcon(new ImageIcon(West));


    }

}
package漫游者;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.Component;
导入java.awt.Dimension;
导入java.awt.GridLayout;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.awt.image.buffereImage;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.BorderFactory;
导入javax.swing.Box;
导入javax.swing.BoxLayout;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JCheckBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入javax.swing.JTextField;
导入javax.swing.border.border;
导入javax.swing.border.EtchedBorder;
公共类MenuPanel扩展了JPanel{
私有静态最终长serialVersionUID=-3928152660110599311L;
公共JPanel框架、文本字段、按钮、cpPanel;
公共JTextField命令;
公共JButton绘图、提交、撤消;
公共JLabel位置,cpLabel;
公共边界被重新划分;
公共JCheckBox网格线;
公共提交按钮子;
静态最终整数行=100,列=100;
//*******图像******
静态缓冲图像北、南、西、东;
public-void-ImageLoader(){
试一试{
North=ImageIO.read(this.getClass().getResource(“North.png”);
South=ImageIO.read(this.getClass().getResource(“South.png”);
West=ImageIO.read(this.getClass().getResource(“West.png”);
East=ImageIO.read(this.getClass().getResource(“East.png”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
System.out.println(“发生错误:+e”);
e、 printStackTrace();
}
}
//*******图像******
公共创建菜单(JPanel p){
//文本字段面板
命令=新的JTextField(20);
textfield=newjpanel();
setPreferredSize(新维度(150,50));
setboorder(BorderFactory.createEmptyBorder(10,10,10,10));
设置背景(新颜色(204153255));
textfield.add(命令);
//在文本字段旁边有一个按钮以清除内容。
//可能需要为JPanel提供一个新的流布局。
//按钮面板
按钮=新的JPanel();
按钮。设置首选尺寸(新尺寸(150250));
按钮.setLayout(新的BoxLayout(按钮,BoxLayout.Y_轴));
按钮。背景(新颜色(1700511170));
//创建按钮并将其添加到“按钮”面板
添加(Box.createRigidArea(新维度(30,10));
Plot=新的JButton(“Plot”);
Plot.setAlignmentX(组件中心对齐);
Plot.setAlignmentY(部件中心对齐);
按钮。添加(绘图);
添加(Box.createRigidArea(新维度(30,10));
提交=新按钮(“提交”);
提交setAlignmentX(组件中心对齐);
提交setAlignmentY(部件中心对齐);
Submit.addActionListener(新建ActionListener()){
@凌驾
已执行的公共无效操作(操作事件e){
sub=新的SubmitButton();
sub.Submit(命令);
cpLabel.setText(“******设置当前位置******”);
标签[2][2]。设置图标(新图像图标(北));
//我将能够使用for循环从这里移动漫游者
//和if语句。
}
});
按钮。添加(提交);
添加(Box.createRigidArea(新维度(30,10));
撤销=新的JButton(“撤销”);
撤消.setAlignmentX(组件中心对齐);
撤消.setAlignmentY(组件.中心对齐);
按钮。添加(撤消);
添加(Box.createRigidArea(新维度(30,10));
gridLines=新的JCheckBox();
setText(“显示网格线”);
gridLines.setAlignmentX(组件中心对齐);
网格线.setAlignmentY(组件.中心对齐);
addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件arg0){
//从这里设置JLabels阵列的颜色。
System.out.println(“单击”);
}
});
按钮
@Override
public void paintComponent(Graphics g)
{
    //super.paintComponent(g);
   g.drawImage(image, 0, 0, null);
}
JPanel gridPanel = new JPanel();
JPanel gridPanel = new JPanel()
{
    @Override
    public void paintComponent(Graphics g)
    {
        //super.paintComponent(g);
       g.drawImage(image, 0, 0, null);
    }
};