У меня есть небольшой вопрос о следующем коде. Одним из заданий в этом случае было создание простой программы рисования. Основная часть реализована здесь: что бы было нарисовать линию, нарисовать точку и использовать так называемый метод clear(), который перерисовывает панель. Дополнительная часть состоит в том, чтобы нажимать кнопку и нажатием этой кнопки, ваш рисунок будет выделяться, а затем будет возможность копирования и вставки и повторить и отменить. На данный момент я понятия не имею, с чего начать с дополнительных заданий. Пожалуйста, помогите мне.Как сделать краску copy-paste-cut-redo?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import javax.swing.*;
import javax.swing.text.DefaultEditorKit;
// Lahingplaan:
public class MAIN{
private void displayGUI()
{ // Loon JFrame'i:
final JFrame frame = new JFrame("GIMP");
// Addin frame'le windowlisteneri:
frame.addWindowListener(new WindowAdapter() {
// Loon akna, mis hakkab kasutajat tüütama, kui see tahab panna applicationi kinni:
public void windowClosing(WindowEvent we)
{
int result = JOptionPane.showConfirmDialog(
frame, "Olete kindel, et soovite applikatsioonist väljuda :(((((:OOOOO ?"
, "YOU REALLY WANNA QUIT?", JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
else if (result == JOptionPane.NO_OPTION)
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
});
frame.setVisible(true);
// Escape'i nupp sulgeb programmi:
frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "PROGRAMM SULGUB!");
frame.getRootPane().getActionMap().put("PROGRAMM SULGUB!", new AbstractAction(){
public void actionPerformed(ActionEvent e)
{
frame.dispose();
}
});
// (1) Võtab ContentPane'i ning määrab selle layouti:
Container content = frame.getContentPane();
content.setLayout(new BorderLayout());
// Funktsioon, mis viitab joonistamisele:
final PadDraw drawPad = new PadDraw();
content.add(drawPad, BorderLayout.CENTER);
// JPanel ja tema propertis:
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(80, 80));
panel.setMinimumSize(new Dimension(80, 80));
panel.setMaximumSize(new Dimension(80, 80));
content.add(panel, BorderLayout.WEST);
// Joonista nuppu funktsioon + propertis:
JButton drawButton = new JButton("DRAW!");
drawButton.setPreferredSize(new Dimension(70, 70));
panel.add(drawButton);
//same thing except this is the black button
drawButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.black();
}
});
//Copy button funktsioon ja propertis:
//Paste button funtksioon ja propertis:
//Clear button funktsioon + propertis:
JButton clearButton = new JButton("CLEAR!");
clearButton.setPreferredSize(new Dimension(70, 70));
panel.add(clearButton);
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.clear();
}
});
frame.setSize(600, 480);
frame.setLocation(100,100);
frame.setVisible(true);
}
public static void main(String[] args)
{ // New runnable, mis paneb tööle EXITI ja loadib funktsiooni, mis loob painti:
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new MAIN().displayGUI();
helloworld();
}
});
}
/*
Võib osutuda vajalikuks, aga ei pruugi:
public JMenuBar createMenuBar() {
JMenuItem menuItem = null;
JMenuBar menuBar = new JMenuBar();
JMenu mainMenu = new JMenu("Edit");
mainMenu.setMnemonic(KeyEvent.VK_E);
menuItem = new JMenuItem(new DefaultEditorKit.CutAction());
menuItem.setText("Cut");
menuItem.setMnemonic(KeyEvent.VK_T);
mainMenu.add(menuItem);
menuItem = new JMenuItem(new DefaultEditorKit.CopyAction());
menuItem.setText("Copy");
menuItem.setMnemonic(KeyEvent.VK_C);
mainMenu.add(menuItem);
menuItem = new JMenuItem(new DefaultEditorKit.PasteAction());
menuItem.setText("Paste");
menuItem.setMnemonic(KeyEvent.VK_P);
mainMenu.add(menuItem);
menuBar.add(mainMenu);
return menuBar;
}
*/
private static void helloworld() {
String a,b,c,d;
a = "xxx";
b = "xxx";
c = "xxx";
d = "xxx";
}
}
@SuppressWarnings("serial")
class PadDraw extends JComponent{
Image image;
Graphics2D graphics2D;
int currentX, currentY, oldX, oldY;
Point2D.Double point;
//Now for the constructors
public PadDraw(){
setDoubleBuffered(true);
// Ainult punkti joonistamine:
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
if(graphics2D !=null)
graphics2D.drawLine(oldX, oldY, oldX, oldY);
repaint();
}
});
// Joone joonistamine:
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
//Draw line:
currentX = e.getX();
currentY = e.getY();
if(graphics2D != null)
graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
//while the mouse is dragged it sets currentX & currentY as the mouses x and y
//then it draws a line at the coordinates
//it repaints it and sets oldX and oldY as currentX and currentY
}
public void paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D)image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 0, 0, null);
}
//this is the painting bit
//if it has nothing on it then
//it creates an image the size of the window
//sets the value of Graphics as the image
//sets the rendering
//runs the clear() method
//then it draws the image
public void clear(){
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
public void black(){
graphics2D.setPaint(Color.black);
repaint();
}
}