繁体   English   中英

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

[英]drawImage() on JPanel OR add Image on top of GridLayout

我有100 x 100格的标签。 我有一个创建并填充字符串数组的方法。 下一个方法创建标签数组,然后使用setText()方法将String(从上一个方法创建)添加到标签。 一些标签也包含图像。 之后的方法将那些JLabel并将它们添加到Grid Layout的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));


    }

}

非常感谢! 我真的很感谢任何帮助或建议:D

正如您所说,您需要做的是重写JPanel的paintComponent方法,并在其中放置一个drawImage(...) 所以:

@Override
public void paintComponent(Graphics g)
{
    //super.paintComponent(g);
   g.drawImage(image, 0, 0, null);
}

其中image是您先前在初始化代码中加载的Image类的实例(不要将其加载到paintComponent中,那太慢了,您只想加载一次)。

有两种方法可以完成此任务:

  • 使您自己的类扩展JPanel并将该代码放在此处。 您可能还希望创建一个方法setBackgroundImage(Image) ,可以从主类调用该方法以传递从磁盘加载的图像。

  • 创建一个匿名类该类正在执行类似的操作,但未明确定义新类。 为此,而不是像这样创建面板:

     JPanel gridPanel = new JPanel(); 

    像这样做:

     JPanel gridPanel = new JPanel() { @Override public void paintComponent(Graphics g) { //super.paintComponent(g); g.drawImage(image, 0, 0, null); } }; 

当然,您必须在实际代码中执行此操作(而不是作为静态初始化),因为您要确保先加载映像。

最后有几点建议:

  • 变量名称按照惯例以小写字母开头(与以大写字母开头的类名称相反)。 例如,您不需要在“ JPanel Grid参数和“ Comands字段中执行此操作。

  • 您违反了Swing的单线程规则。 也就是说,您必须在主包装GUI初始化代码中调用invokeLater 例如,查看Swing的Hello World 您可以在这里找到对此的详细说明。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM