我在 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个问题:
完成后,保存图像背景颜色为黑色,如何使其变为白色。
当鼠标拖出时
jLabel
,它怎么没有画这个。