0

我在 Java 中使用 Application simple Form。我有这样的代码。

Graphics g = null;
Graphics g1;
boolean conPaint, temp = false;
int x, y;
BufferedImage img;
public project() {
    initComponents();
    super.paintComponents(g);
    img = new BufferedImage(1366 , 768,BufferedImage.TYPE_INT_RGB);
    g = img.getGraphics();
    g.setColor(Color.white);
    g1 = getGraphics();
    conPaint = false;
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    menuBar = new javax.swing.JMenuBar();
    fileMenu = new javax.swing.JMenu();
    openMenuItem = new javax.swing.JMenuItem();
    saveMenuItem = new javax.swing.JMenuItem();
    exitMenuItem = new javax.swing.JMenuItem();
    editMenu = new javax.swing.JMenu();
    cutMenuItem = new javax.swing.JMenuItem();
    copyMenuItem = new javax.swing.JMenuItem();
    pasteMenuItem = new javax.swing.JMenuItem();
    deleteMenuItem = new javax.swing.JMenuItem();
    helpMenu = new javax.swing.JMenu();
    contentsMenuItem = new javax.swing.JMenuItem();
    aboutMenuItem = new javax.swing.JMenuItem();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setToolTipText("");
    jLabel1.addMouseListener(new java.awt.event.MouseAdapter() {
        public void mouseEntered(java.awt.event.MouseEvent evt) {
            jLabel1MouseEntered(evt);
        }
    });
    jLabel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseDragged(java.awt.event.MouseEvent evt) {
            jLabel1MouseDragged(evt);
        }
        public void mouseMoved(java.awt.event.MouseEvent evt) {
            jLabel1MouseMoved(evt);
        }
    });

    fileMenu.setMnemonic('f');
    fileMenu.setText("File");

    openMenuItem.setMnemonic('o');
    openMenuItem.setText("Open");
    openMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            openMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(openMenuItem);

    saveMenuItem.setMnemonic('s');
    saveMenuItem.setText("Save");
    saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(saveMenuItem);

    exitMenuItem.setMnemonic('x');
    exitMenuItem.setText("Exit");
    exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            exitMenuItemActionPerformed(evt);
        }
    });
    fileMenu.add(exitMenuItem);

    menuBar.add(fileMenu);

    editMenu.setMnemonic('e');
    editMenu.setText("Edit");

    cutMenuItem.setMnemonic('t');
    cutMenuItem.setText("Cut");
    editMenu.add(cutMenuItem);

    copyMenuItem.setMnemonic('y');
    copyMenuItem.setText("Copy");
    editMenu.add(copyMenuItem);

    pasteMenuItem.setMnemonic('p');
    pasteMenuItem.setText("Paste");
    editMenu.add(pasteMenuItem);

    deleteMenuItem.setMnemonic('d');
    deleteMenuItem.setText("Delete");
    editMenu.add(deleteMenuItem);

    menuBar.add(editMenu);

    helpMenu.setMnemonic('h');
    helpMenu.setText("Help");

    contentsMenuItem.setMnemonic('c');
    contentsMenuItem.setText("Contents");
    helpMenu.add(contentsMenuItem);

    aboutMenuItem.setMnemonic('a');
    aboutMenuItem.setText("About");
    helpMenu.add(aboutMenuItem);

    menuBar.add(helpMenu);

    setJMenuBar(menuBar);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
    System.exit(0);
}                                            

private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
    JFileChooser chooser= new JFileChooser();
    FileNameExtensionFilter ff = new FileNameExtensionFilter("*.jpg *.png *.jpeg *.gif *.bmp files", "jpg", "png","jpeg","gif","bmp");
    chooser.setFileFilter(ff);
    chooser.showOpenDialog(this);
    File linkimage = chooser.getSelectedFile();
    Image image = Toolkit.getDefaultToolkit().getImage(linkimage.getPath());
    jLabel1.setSize( 1366 , 768);
    jLabel1.setIcon(new ImageIcon(image));
    Graphics2D gr2d = img.createGraphics();
    jLabel1.paint(gr2d);
}                                            

private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {                                             
    JFileChooser jFile = new JFileChooser();
    jFile.showSaveDialog(null);
    Path pth = jFile.getSelectedFile().toPath();
    JOptionPane.showMessageDialog(null, "Save Completed!!!");
    try {
        ImageIO.write(img, "jpg", new File(pth.toString()));
    } catch (IOException ex) {
    }
}                                            

private void jLabel1MouseDragged(java.awt.event.MouseEvent evt) {                                     
    if (temp)
    {
    if (conPaint)
    g.drawLine(x, y, evt.getX(), evt.getY());
    g1.drawLine(x + 8, y + 53, evt.getX() + 8, evt.getY() + 53);
    x = evt.getX();
    y = evt.getY();
    }
}                                    
private void jLabel1MouseMoved(java.awt.event.MouseEvent evt) {                                   
    if (temp)
    {
    conPaint = true;
    x = evt.getX();
    y = evt.getY();
    }
}                                  

private void jLabel1MouseEntered(java.awt.event.MouseEvent evt) {                                     
    temp = true;
}

有2个问题:

  1. 完成后,保存图像背景颜色为黑色,如何使其变为白色。

  2. 当鼠标拖出时jLabel,它怎么没有画这个。

4

1 回答 1

0

设置背景颜色: setBackground(Color.WHITE);

至于第二个问题,您确定在拖动之前将 temp 和 conPaint 都设置为 true 吗?确保在 MouseMoved 之前调用 MouseEntered,它应该在 MouseDragged 之前调用。否则它根本不起作用(首先你需要 temp 为真,然后 conPaint 然后只有 MouseDragged 才能工作。你确定你需要这么复杂的逻辑吗?你真的需要 temp 和 conPaint 变量吗?)

并且对于未来:在您的代码中添加一些适当的表格以使其可读。

于 2013-11-06T17:15:28.850 回答