我对以下代码有一点疑问。在这种情况下,其中一项任务是创建一个简单的绘图程序。基本部分在这里实现:画一条线,画一个点并使用一个所谓的clear()方法来重新绘制面板。额外的部分是按下一个按钮,通过按下该按钮,您的绘图将突出显示自己,然后将有复制粘贴功能,重做和撤消。 目前我不知道,从哪里开始额外的任务。请有人指导和帮助我。
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();
}
}
答案 0 :(得分:0)
您想要保存并加载绘制图像的区域。
copyArea
中有一个java.awt.Graphics2D
用于'复制粘贴'。
create
中有一个java.awt.Graphics2D
来复制图片。