1

我正在做一个项目,我必须在 Java 中创建一个图像管理器程序。该程序将具有以下功能: 程序应从用户那里获取图像作为输入。然后它应该在面板(1)中显示图像,在面板(2)中,它应该为用户提供更改宽度、高度、hgap、vgap、上边距和左边距的选项,并带有“确定”按钮最后包括一个“动作监听器”功能。使用上述功能,我们将获得图像上的网格。现在,如果用户单击特定网格,则该特定图像应该被提取到文件夹中。我附上我们到目前为止所做的程序。

我们面临的困难是:我们无法JScrollPane为图像插入 a。而且我们不知道如何将动作监听器添加到网格中,以便我们可以提取该特定图像。

package project_image_manager_imp_files;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.IOException;

class SidePanel1 extends JPanel implements ActionListener {

    JTextField top_margin = new JTextField(10);
    JTextField left_margin = new JTextField(10);
    JTextField Width = new JTextField(10);
    JTextField Height = new JTextField(10);
    JTextField Vgap = new JTextField(10);
    JTextField Hgap = new JTextField(10);
    JLabel j1 = new JLabel("Top margin");
    JLabel j2 = new JLabel("Left margin");
    JLabel j3 = new JLabel("Width");
    JLabel j4 = new JLabel("Height");
    JLabel j5 = new JLabel("Vertical gap");
    JLabel j6 = new JLabel("Horizontal gap");
    JButton b = new JButton("OK");

    public SidePanel1() {
        this.setBackground(Color.black);
        Dimension d1 = new Dimension(1000, 1000);
        this.setMaximumSize(d1);
        this.setPreferredSize(d1);

        JPanel p = new JPanel();
        JScrollPane vertical = new JScrollPane();
        vertical.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        add(vertical);
        vertical.setVerticalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        add(vertical);


        //showCoo.build(c);
        //c.add(p);
        //p.setOpaque(false);
        //t1.addActionListener(new JTextFieldEventClass() );
        //b.addActionListener(new JTextFieldEventClass() );

        // String s=t3.getText();
        //int c=Integer.parseInt(s);
        p.add(j1);
        p.add(top_margin);
        p.add(j2);
        p.add(left_margin);
        p.add(j3);
        p.add(Width);
        p.add(j4);
        p.add(Height);
        p.add(j5);
        p.add(Vgap);
        p.add(j6);
        p.add(Hgap);
        p.add(b);
        p.setLayout(new GridLayout(7, 2));
        b.addActionListener(this);
        JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(1, 2));
        frame.setBounds(200, 200, 600, 400);
        frame.setTitle("Hello World Test");
        frame.setResizable(true);
        //frame.setSize (500, 300);
        Container c = frame.getContentPane();
        frame.setLayout(new BorderLayout());
        frame.add(this, BorderLayout.WEST);
        frame.add(p, BorderLayout.EAST);
        frame.pack();
        frame.setVisible(true);
        frame.setResizable(true);
    }
    int height;
    int width;
    int hgap;
    int vgap;
    int tm;
    int lm;

    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        g.drawImage(Toolkit.getDefaultToolkit().getImage("images1.jpg"), 0, 0, this);
        Graphics2D g2d = (Graphics2D) g.create();
        //File f=new File("images1.jpg");
        //BufferedImage im=ImageIO.read(f);

        int frame_width = getWidth();
        int frame_height = getHeight();

        for (int j = 0; j < frame_height; j++) {
            int y = tm + (height * j) + (vgap * j);
            for (int i = 0; i < frame_width; i++) {
                int x = lm + (width * i) + (hgap * i);

                g.drawRect(x, y, width, height);
            }
        }
    }

    public static void main(String[] argv) {
        //static JScrollBar scrollbar;

        SidePanel1 panel = new SidePanel1();
        panel.setLayout(new FlowLayout());


        //scrollbar = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        //panel.add(scrollbar);
        try {
            while (true) {
                Thread.sleep(100);
                ///.out.println("("+MouseInfo.getPointerInfo().getLocation().x+", "+MouseInfo.getPointerInfo().getLocation().y+")");
            }
        } catch (InterruptedException e) {}
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        width = Integer.parseInt(Width.getText());
        height = Integer.parseInt(Height.getText());
        hgap = Integer.parseInt(Hgap.getText());
        vgap = Integer.parseInt(Vgap.getText());
        lm = Integer.parseInt(left_margin.getText());
        tm = Integer.parseInt(top_margin.getText());
        repaint();
    }
}
4

0 回答 0