📄 wbmpeditorframe.java
字号:
private void jMenuFileExit_mouseEntered(MouseEvent e) { statusBar.setText("Quit the program"); } /** Mouse listener for mouseEntered event @param MouseEvent e */ private void jMenuDrawingPen_mouseEntered(MouseEvent e) { statusBar.setText("Draw in black"); } /** Mouse listener for mouseEntered event @param MouseEvent e */ private void jMenuDrawingEraser_mouseEntered(MouseEvent e) { statusBar.setText("Draw using the background color"); } /** Mouse listener for mouseEntered event @param MouseEvent e */ private void jMenuHelpContents_mouseEntered(MouseEvent e) { statusBar.setText("View the help file"); } /** Mouse listener for mouseEntered event @param MouseEvent e */ private void jMenuHelpAbout_mouseEntered(MouseEvent e) { statusBar.setText("View the about box"); } // Menu actions /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuFileNew_actionPerformed(ActionEvent e) { if (Changed) { jMenuFileSave.doClick(); } else { NewDialog dlg = new NewDialog(); dlg.show(); if (dlg.GetButtonPressed()) { // Set the title this.setTitle("WBMP Editor - Untitled"); Untitled = true; //set the new document flag // clear the drawing and the previous controls Drawinglabel.removeAll(); Drawinglabel.repaint(); // get the values from the dialog int w = dlg.GetWidth(); int h = dlg.GetHeight(); // define the new matrix and initialize it to background color ColorMatrix = new int[w][h]; for (int c = 0; c < w; c++) for (int r = 0; r < h; r++) ColorMatrix[c][r] = 1; Width = w; Height = h; // set the background color BackgroundColor = bkg; // create the new labels JLabel l; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { l = new JLabel(); l.setBackground(bkg); l.setOpaque(true); l.setBorder(BorderFactory.createLineBorder(Color.lightGray,1)); l.setSize(labelSize, labelSize); l.setLocation(x * labelSize, y * labelSize); l.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { DrawingBox_mouseClicked(e); } public void mousePressed(MouseEvent e) { DrawingBox_mousePressed(e); } public void mouseReleased(MouseEvent e) { DrawingBox_mouseReleased(e); } public void mouseEntered(MouseEvent e) { DrawingBox_mouseEntered(e); } }); Drawinglabel.add(l); jMenuFilePreview.setEnabled(true); } } } Drawinglabel.repaint(); dlg.dispose(); } } /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuFileOpen_actionPerformed(ActionEvent e) { if (Changed) { jMenuFileSave.doClick(); } else { Filter f = new Filter("wbmp", "WAP Bitmap"); JFileChooser fc = new JFileChooser(); fc.addChoosableFileFilter(f); try { File d = new File("temp"); String Dir = d.getCanonicalPath(); Dir = Dir.substring(0, Dir.length() - 4); fc.setCurrentDirectory(new File(Dir)); } catch (IOException ioerror) {} fc.showOpenDialog(this); fileName = fc.getSelectedFile().getPath(); OpenFile(); } } /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuFileSave_actionPerformed(ActionEvent e) { if (Untitled) { doSaveAs(); } else { Changed = false; Encoder wbmpEncoder = new Encoder(Width, Height, ColorMatrix, fileName); } } /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuFilePreview_actionPerformed(ActionEvent e) { Viewer viewer = new Viewer(Width, Height, ColorMatrix); viewer.show(); statusBar.setText("Ready"); } /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuDrawingPen_actionPerformed(ActionEvent e) { jMenuDrawingPen.setSelected(true); ToolbarPen.setSelected(true); jMenuDrawingEraser.setSelected(false); ToolbarEraser.setSelected(false); DrawingColor = Color.black; } /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuDrawingEraser_actionPerformed(ActionEvent e) { jMenuDrawingPen.setSelected(false); ToolbarPen.setSelected(false); jMenuDrawingEraser.setSelected(true); ToolbarEraser.setSelected(true); DrawingColor = BackgroundColor; } /** Menu listener for menu Action events. @param ActionEvent e */ private void jMenuHelpContents_actionPerformed(ActionEvent e) { HelpDialog aHelpDialog = new HelpDialog(this, false); aHelpDialog.show(); } // Toolbar Actions /** Toolbar listener for toolbar Action events. @param ActionEvent e */ private void ToolbarNew_actionPerformed(ActionEvent e) { jMenuFileNew.doClick(); } /** Toolbar listener for toolbar Action events. @param ActionEvent e */ private void ToolbarSave_actionPerformed(ActionEvent e) { jMenuFileSave.doClick(); } /** Toolbar listener for toolbar Action events. @param ActionEvent e */ private void ToolbarPen_actionPerformed(ActionEvent e) { jMenuDrawingPen.doClick(); } /** Toolbar listener for toolbar Action events. @param ActionEvent e */ private void ToolbarEraser_actionPerformed(ActionEvent e) { jMenuDrawingEraser.doClick(); } /** Toolbar listener for toolbar Action events. @param ActionEvent e */ private void ToolbarHelp_actionPerformed(ActionEvent e) { jMenuHelpContents.doClick(); } /** Toolbar listener for toolbar Action events. @param ActionEvent e */ private void ToolbarOpen_actionPerformed(ActionEvent e) { jMenuFileOpen.doClick(); } // Drawing Methods /** Mouse listener for Mouse events. Handles drawing when mouse is clicked. @param MouseEvent e */ private void DrawingBox_mouseClicked(MouseEvent e) { JLabel l = (JLabel) e.getSource(); Changed = true; l.setBackground(DrawingColor); if (DrawingColor == Color.black) ColorMatrix[l.getX() /labelSize][l.getY() /labelSize] = 0; else ColorMatrix[l.getX() /labelSize][l.getY() /labelSize] = 1; } /** Mouse listener for Mouse events. Handles drawing when mouse is clicked. Also changes the cursor to reflect the change in control state. @param MouseEvent e */ private void DrawingBox_mousePressed(MouseEvent e) { Pressing = true; Drawinglabel.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); JLabel l = (JLabel) e.getSource(); if (Pressing) { Changed = true; l.setBackground(DrawingColor); if (DrawingColor == Color.black) { ColorMatrix[l.getX() /labelSize][l.getY() /labelSize] = 0; } else ColorMatrix[l.getX() /labelSize][l.getY() /labelSize] = 1; } XPoslabel.setText("X: " + Integer.toString(l.getX() /labelSize + 1)); YPoslabel.setText("Y: " + Integer.toString(l.getY() /labelSize + 1)); } /** Mouse listener for Mouse events. Changes cursor back to default cursor. @param MouseEvent e */ private void DrawingBox_mouseReleased(MouseEvent e) { Pressing = false; Drawinglabel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } /** Mouse listener for Mouse events. Handles drawing when mouse is clicked. @param MouseEvent e */ private void DrawingBox_mouseEntered(MouseEvent e) { JLabel l = (JLabel) e.getSource(); if (Pressing) { Changed = true; l.setBackground(DrawingColor); if (DrawingColor == Color.black) { ColorMatrix[l.getX() /labelSize][l.getY() /labelSize] = 0; } else ColorMatrix[l.getX() /labelSize][l.getY() /labelSize] = 1; } XPoslabel.setText("X: " + Integer.toString(l.getX() /labelSize + 1)); YPoslabel.setText("Y: " + Integer.toString(l.getY() /labelSize + 1)); } // Custom Methods /** Handles file opening and decodingrelated issues. Will display a Save As dialog box if changes where made to the previous file*/ private void OpenFile() { statusBar.setText("Opening file..."); Decoder decoder = new Decoder(fileName); if (decoder.getStatus()){ String fName = fileName.substring(new File(fileName).getParent().toString().length() + 1); this.setTitle("WBMP Editor - " + fName); statusBar.setText("Ready"); Untitled = false; CreateDrawing(decoder.getWidth(), decoder.getHeight(), decoder.getMatrix()); jMenuFilePreview.setEnabled(true); } else statusBar.setText("Error decoding file or invalid file type"); } /** Handles file saving and encoding related issues. Will display as Save As dilaog box if the file is marked as untitled. */ private void doSaveAs() { int confirm = 0; if (!Untitled) { JOptionPane opt = new JOptionPane(); confirm = opt.showConfirmDialog(this, new String("File has been changed, do you wish to save first?"), new String("Question?"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); } if (confirm == 0) { try{ Filter f = new Filter("wbmp", "WAP Bitmap"); JFileChooser fc = new JFileChooser(); fc.addChoosableFileFilter(f); try { File d = new File("temp"); String Dir = d.getCanonicalPath(); Dir = Dir.substring(0, Dir.length() - 4); fc.setCurrentDirectory(new File(Dir)); } catch (IOException ioerror) {} fc.setDialogTitle("Save As"); fc.showSaveDialog(this); String fileName = fc.getSelectedFile().getPath() + ".wbmp"; String fName = fileName.substring(new File(fileName).getParent().toString().length() + 1); this.setTitle("WBMP Editor - " + fName); statusBar.setText("Saving file..."); Changed = false; Encoder wbmpEncoder = new Encoder(Width, Height, ColorMatrix, fileName); statusBar.setText("Ready"); Untitled = false; } catch (NullPointerException e) { statusBar.setText("Error saving file"); } } } /** Does the actual drawing on the label after a file was opened. Files created using the New menu item are created within the New menu item handler. @param int Width @param int Height @param int[][] ColorMatrix */ private void CreateDrawing(int w, int h, int[][] matrix) { // clear the drawing and the previous controls Drawinglabel.removeAll(); Drawinglabel.repaint(); // define the new matrix and initialize it to background color ColorMatrix = new int[w][h]; for (int c = 0; c < w; c++) for (int r = 0; r < h; r++) ColorMatrix[c][r] = matrix[c][r]; Width = w; Height = h; // set the background color BackgroundColor = bkg; // create the new labels JLabel l; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { l = new JLabel(); if (ColorMatrix[x][y] == 1) l.setBackground(bkg); else l.setBackground(Color.black); l.setOpaque(true); l.setBorder(BorderFactory.createLineBorder(Color.lightGray,1)); l.setSize(labelSize, labelSize); l.setLocation(x * labelSize, y * labelSize); l.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(MouseEvent e) { DrawingBox_mouseClicked(e); } public void mousePressed(MouseEvent e) { DrawingBox_mousePressed(e); } public void mouseReleased(MouseEvent e) { DrawingBox_mouseReleased(e); } public void mouseEntered(MouseEvent e) { DrawingBox_mouseEntered(e); } }); Drawinglabel.add(l); } } Drawinglabel.repaint(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -