大家。我创建了自己的图形编辑器(Autocad mini)。想要运行,可以将其保存在想要的磁盘上,并且可以在任何编辑器(Autocad,Paint等)上打开。
这是我的JAVA代码(对不起的代码,我在5个小时内就完成了)。
坚持要以某种格式保存图形,以便可以在此程序(或其他编辑器,如果可能)中将其打开
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import static java.awt.Color.BLUE;
class ModelOfFigure {
public int x;
public int y;
public int w;
public int h;
public String status;
public int xEnd;
public int yEnd;
public Color color;
public int rotateRadius;
public ModelOfFigure(int x, int y, int w, int h, String status, int xEnd, int yEnd, Color color, int rotateRadius){
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.status = status;
this.xEnd = xEnd;
this.yEnd = yEnd;
this.color = color;
this.rotateRadius = rotateRadius;
}
}
//Смотреть строку 90
public class PaintOwn extends JPanel {
private static JPanel panelGlav = new JPanel(new BorderLayout());
private static Painting p = new Painting();
private static ArrayList<ModelOfFigure> listOfFigure = new ArrayList<>();
private static JButton btnAddRect = new JButton("RECT");
private static JButton btnAddOval = new JButton("OVAL");
private static JButton btnAddLine = new JButton("LINE");
private static JPanel panelGrid = new JPanel(new GridLayout(1, 3));
private static JPanel panelGridGlav = new JPanel(new GridLayout(2, 1));
private static JMenuBar mBar = new JMenuBar();
private static JMenu menuFile = new JMenu("File");
private static JMenuItem itemSave = new JMenuItem("Save");
private static JMenuItem itemNew = new JMenuItem("New");
private static boolean tfwhTF = false, isRectAdded = false, isOvalAdded = false, isLineAdded = false, isFigurePressed = false;
private static int x = 0, y = 0, w = 0, h = 0, j = 0, i = 0, objNum = 0, clickedOnEnd = 0, rotateRadius = 0, clicksForLine = 0, xEnd = 0, yEnd = 0;
private static String figureStatus = "";
public PaintOwn(){
JFrame frame = new JFrame();
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// frame.setResizable(false);
panelGrid.add(btnAddRect);
panelGrid.add(btnAddOval);
panelGrid.add(btnAddLine);
// panelGrid.add(btnEnd);
// panelGlav.add(panelGrid, BorderLayout.NORTH);
menuFile.add(itemSave);
menuFile.add(itemNew);
mBar.add(menuFile);
panelGridGlav.add(mBar);
panelGridGlav.add(panelGrid);
panelGlav.add(panelGridGlav, BorderLayout.NORTH);
clickButtons();
itemSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
filesClick();
} catch (IOException ex) {
ex.printStackTrace();
} catch (AWTException ex) {
ex.printStackTrace();
}
}
});
panelGlav.add(p, BorderLayout.CENTER);
frame.add(new JScrollPane(panelGlav));
}
public static void main(String[] args) {
new PaintOwn();
}
////////////////////////FUNCTION//////////////////////////////
/////////////////////////////////////////////////////////////
////////// BUTTON CLICKS FOR ADD COMPONENT IN PANEL/////////
///////////////////////////////////////////////////////////
private void filesClick() throws IOException, AWTException {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
final File f = chooser.getSelectedFile();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "png", new File(String.valueOf(f)));
}
}
private static void clickButtons(){
figureStatus = "NON";
clickedOnEnd++;
p.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(e.getClickCount() == 2){
// System.out.println("two clicks!");
for(i = 0; i<listOfFigure.size(); i++){
x = e.getX();
y = e.getY();
if(x>=listOfFigure.get(i).x && x<=listOfFigure.get(i).xEnd && y>=listOfFigure.get(i).y && y<=listOfFigure.get(i).yEnd){
// System.out.println(x);
if(isFigurePressed && !listOfFigure.get(i).status.equals("LINE")){
JFrame sizeFrame = new JFrame();
sizeFrame.setSize(200, 100);
sizeFrame.setLocationRelativeTo(null);
sizeFrame.setVisible(true);
sizeFrame.setResizable(false);
JTextField txtWidth = new JTextField(String.valueOf(listOfFigure.get(i).w));
JTextField txtHeight = new JTextField(String.valueOf(listOfFigure.get(i).h));
JButton btnEnter = new JButton("ENTER");
JButton btnRemove = new JButton("REMOVE");
JPanel panelGrid = new JPanel(new GridLayout(3, 1));
JPanel panelGridForBtn = new JPanel(new GridLayout(1, 2));
panelGrid.add(txtWidth);
panelGrid.add(txtHeight);
// panelGrid.add(btnEnter);
// panelGrid.add(btnRemove);
panelGridForBtn.add(btnEnter);
panelGridForBtn.add(btnRemove);
panelGrid.add(panelGridForBtn);
sizeFrame.add(panelGrid);
btnEnter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listOfFigure.get(objNum).w = Integer.valueOf(txtWidth.getText().toString());
listOfFigure.get(objNum).h = Integer.valueOf(txtHeight.getText().toString());
listOfFigure.get(objNum).xEnd = listOfFigure.get(objNum).x+listOfFigure.get(objNum).w;
listOfFigure.get(objNum).yEnd = listOfFigure.get(objNum).y+listOfFigure.get(objNum).h;
listOfFigure.get(objNum).rotateRadius = 0;
p.repaint();
sizeFrame.dispose();
}
});
btnRemove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listOfFigure.remove(objNum);
p.repaint();
sizeFrame.dispose();
}
});
tfwhTF = true;
objNum = i;
listOfFigure.get(i).color = Color.RED;
return;
}
else if(listOfFigure.get(i).color == Color.RED && listOfFigure.get(i).status.equals("LINE")){
System.out.println(listOfFigure.get(i).xEnd+" => xEnd\n"+listOfFigure.get(i).yEnd+" => yEnd");
JFrame sizeFrame = new JFrame();
sizeFrame.setSize(200, 100);
sizeFrame.setLocationRelativeTo(null);
sizeFrame.setVisible(true);
sizeFrame.setResizable(false);
JTextField txtWidth = new JTextField(String.valueOf(listOfFigure.get(i).xEnd-listOfFigure.get(i).x));
JTextField txtHeight = new JTextField(String.valueOf(listOfFigure.get(i).rotateRadius));
JButton btnEnter = new JButton("ENTER");
JButton btnRemove = new JButton("REMOVE");
JPanel panelGrid = new JPanel(new GridLayout(3, 1));
JPanel panelGridForBtn = new JPanel(new GridLayout(1, 2));
panelGrid.add(txtWidth);
panelGrid.add(txtHeight);
// panelGrid.add(btnEnter);
// panelGrid.add(btnRemove);
panelGridForBtn.add(btnEnter);
panelGridForBtn.add(btnRemove);
panelGrid.add(panelGridForBtn);
sizeFrame.add(panelGrid);
btnEnter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listOfFigure.get(objNum).w = Integer.valueOf(txtWidth.getText().toString());
listOfFigure.get(objNum).rotateRadius = Integer.valueOf(txtHeight.getText().toString());
// listOfFigure.get(objNum).xEnd += (int)((listOfFigure.get(objNum).xEnd - listOfFigure.get(objNum).x)*Math.cos(Math.toRadians(listOfFigure.get(objNum).rotateRadius))-(listOfFigure.get(objNum).yEnd - listOfFigure.get(objNum).y)*Math.sin(Math.toRadians(listOfFigure.get(objNum).rotateRadius)));
// listOfFigure.get(objNum).yEnd += (int)((listOfFigure.get(objNum).xEnd-listOfFigure.get(objNum).x)*Math.sin(Math.toRadians(listOfFigure.get(objNum).rotateRadius))+(listOfFigure.get(objNum).yEnd - listOfFigure.get(objNum).y)*Math.cos(Math.toRadians(listOfFigure.get(objNum).rotateRadius)));
// listOfFigure.get(objNum).xEnd = (int)(listOfFigure.get(objNum).x*Math.cos(Math.toRadians(listOfFigure.get(objNum).rotateRadius))-listOfFigure.get(objNum).y*Math.sin(Math.toRadians(listOfFigure.get(objNum).rotateRadius)));
// listOfFigure.get(objNum).yEnd = (int)( listOfFigure.get(objNum).x*Math.sin(Math.toRadians(listOfFigure.get(objNum).rotateRadius)) + listOfFigure.get(objNum).y*Math.cos(Math.toRadians(listOfFigure.get(objNum).rotateRadius)) );
listOfFigure.get(objNum).x = x;
listOfFigure.get(objNum).y = y;
listOfFigure.get(objNum).xEnd = listOfFigure.get(objNum).x+100;
System.out.println(listOfFigure.get(objNum).xEnd+" => x'END\n"+listOfFigure.get(objNum).yEnd+" => yEnd");
p.repaint();
sizeFrame.dispose();
}
});
btnRemove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
listOfFigure.remove(objNum);
p.repaint();
sizeFrame.dispose();
}
});
tfwhTF = true;
objNum = i;
listOfFigure.get(i).color = Color.RED;
return;
}
else{
tfwhTF = false;
listOfFigure.get(i).color = BLUE;
}
}
else{
listOfFigure.get(i).color = BLUE;
System.out.println("NOOO");
// tfwhTF = false;
}
}
}
}
});
p.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
super.mouseDragged(e);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for(i = 0; i<listOfFigure.size(); i++){
x = e.getX();
y = e.getY();
if(x>=listOfFigure.get(i).x-20 && x<=listOfFigure.get(i).xEnd+20 && y>=listOfFigure.get(i).y-20 && y<=listOfFigure.get(i).yEnd+20){
System.out.println(x);
if(isFigurePressed && !listOfFigure.get(i).status.equals("LINE")){
listOfFigure.get(i).x = x;
listOfFigure.get(i).y = y;
listOfFigure.get(i).xEnd = listOfFigure.get(i).x+listOfFigure.get(i).w;
listOfFigure.get(i).yEnd = listOfFigure.get(i).y+listOfFigure.get(i).h;
listOfFigure.get(i).color = Color.RED;
}else if(isFigurePressed && listOfFigure.get(i).status.equals("LINE")){
listOfFigure.get(i).x = x;
listOfFigure.get(i).y = y;
listOfFigure.get(i).xEnd = x+(int)((listOfFigure.get(i).w)*Math.cos(Math.toRadians(listOfFigure.get(i).rotateRadius))-(listOfFigure.get(i).h)*Math.sin(Math.toRadians(listOfFigure.get(i).rotateRadius)));
listOfFigure.get(i).yEnd =y+(int)((listOfFigure.get(i).w)*Math.sin(Math.toRadians(listOfFigure.get(i).rotateRadius))+(listOfFigure.get(i).h)*Math.cos(Math.toRadians(listOfFigure.get(i).rotateRadius)));
listOfFigure.get(i).color = Color.RED;
}else{
listOfFigure.get(i).color = BLUE;
}
}else{
listOfFigure.get(i).color = BLUE;
}
}
p.repaint();
}
});
p.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
x = e.getX();
y = e.getY();
w = 0;
h = 0;
// listOfFigure.add(new ModelOfFigure(x, y, w, h, figureStatus, x+50, y+50, Color.BLACK));
for(i = 0; i<listOfFigure.size(); i++){
if(x>listOfFigure.get(i).x && x<listOfFigure.get(i).xEnd &&
y>listOfFigure.get(i).y && y<listOfFigure.get(i).yEnd){
listOfFigure.get(i).color = Color.RED;
isFigurePressed = true;
}else{
listOfFigure.get(i).color = BLUE;
isFigurePressed = false;
}
}
p.repaint();
}
});
btnAddLine.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
figureStatus = "LINE";
isLineAdded = true;
clicksForLine = 0;
p.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseDragged(e);
if(isLineAdded){
x = e.getX();
y = e.getY();
w = 100;
h = 5;
xEnd = x+w;
yEnd = y+h;
listOfFigure.add(new ModelOfFigure(x, y, w, h, figureStatus, xEnd, yEnd, BLUE, 0));
p.repaint();
p.validate();
isLineAdded = false;
}
}
});
}
});
btnAddRect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
figureStatus = "RECT";
isRectAdded = true;
p.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(isRectAdded){
x = e.getX();
y = e.getY();
w = 50;
h = 50;
listOfFigure.add(new ModelOfFigure(x, y, w, h, figureStatus, x+w, y+h, BLUE, 0));
p.repaint();
p.validate();
isRectAdded = false;
}
}
});
}
});
btnAddOval.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
figureStatus = "OVAL";
isOvalAdded = true;
p.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if(isOvalAdded){
x = e.getX();
y = e.getY();
w = 50;
h = 50;
listOfFigure.add(new ModelOfFigure(x, y, w, h, figureStatus, x+w, y+h, BLUE, 0));
p.repaint();
p.validate();
isOvalAdded = false;
}
}
});
}
});
}
///////////////////////////////////////////////////////////////
/////////////END OF FUNCTION//////////////////////////////////
/////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static class Painting extends JComponent {
@Override
public void paintComponent(final Graphics g){
Graphics2D g2dFigure = (Graphics2D)g;
Graphics2D g2dLinesHorizontally = (Graphics2D)g;
Graphics2D g2dLinesVertically = (Graphics2D)g;
Graphics2D g2dFigureLine = (Graphics2D)g;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double sizeWidth = screenSize.getWidth();
double sizeHeight = screenSize.getHeight();
for(int i = 0; i<(int)sizeWidth; i+=20){
g2dLinesVertically.drawLine((int)i, 0, (int)i, (int)sizeHeight);
g2dLinesVertically.setColor(Color.BLACK);
}
for(int i = 0; i<(int)sizeHeight; i+=20){
g2dLinesHorizontally.drawLine(0, i, (int)sizeWidth, i);
g2dLinesHorizontally.setColor(Color.BLACK);
}
for(int i = 0; i<listOfFigure.size(); i++){
if(listOfFigure.get(i).status.equals("RECT")){
BasicStroke basicStrokeRect = new BasicStroke(3.0f);
g2dFigure.setStroke(basicStrokeRect);
g2dFigure.setColor(listOfFigure.get(i).color);
g2dFigure.drawRect(listOfFigure.get(i).x, listOfFigure.get(i).y, listOfFigure.get(i).w, listOfFigure.get(i).h);
}
if(listOfFigure.get(i).status.equals("OVAL")){
BasicStroke basicStrokeOval = new BasicStroke(3.0f);
g2dFigure.setStroke(basicStrokeOval);
g2dFigure.setColor(listOfFigure.get(i).color);
g2dFigure.drawOval(listOfFigure.get(i).x, listOfFigure.get(i).y, listOfFigure.get(i).w, listOfFigure.get(i).h);
}
if(listOfFigure.get(i).status.equals("LINE")){
BasicStroke basicStrokeLine = new BasicStroke(5.0f);
g2dFigureLine.setStroke(basicStrokeLine);
g2dFigureLine.setColor(listOfFigure.get(i).color);
g2dFigureLine.drawLine(listOfFigure.get(i).x, listOfFigure.get(i).y, listOfFigure.get(i).xEnd, listOfFigure.get(i).yEnd);
}
}
}
}
}