gpt4 book ai didi

java - 在 MySql 中存储形状

转载 作者:行者123 更新时间:2023-11-29 10:38:34 25 4
gpt4 key购买 nike

我正在尝试将形状插入我的 mysql。

有什么方法可以将形状保存到我的数据库中。 稍后可以恢复。

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import com.mysql.jdbc.PreparedStatement;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.sql.Connection;
import java.util.LinkedList;
import java.util.List;

公共(public)类 MyApp 扩展 JFrame 实现 ChangeListener {

private static final long serialVersionUID = 1L;
private JPanel contentPane;
private List<Shape> shapes;
private Canvas canvas;
Shape shapeObject = null;
// private JButton btnClear;
private JColorChooser cc;
private JButton btnLoad;
private JButton btnSave;

Connection con;
PreparedStatement ps;

公共(public)MyApp(){

    setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1348, 660);
contentPane = new JPanel();
contentPane.setBackground(Color.LIGHT_GRAY);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JRadioButton[] shape = getShapesRadioButtons();
for (int i = 0; i < shape.length; i++) {
this.getContentPane().add(shape[i]);
}
JCheckBox[] color = getColor();
for (int i = 0; i < color.length; i++) {
this.getContentPane().add(color[i]);
}

canvas = new Canvas();
canvas.setBackground(Color.WHITE);
canvas.setBounds(608, 56, 724, 565);
contentPane.add(canvas);


canvas.addMouseListener(new MouseListener() {

@Override
public void mouseReleased(MouseEvent mr) {
// TODO Auto-generated method stub

shapeObject.pos.x1 = mr.getX();
shapeObject.pos.y1 = mr.getY();
shapeObject.pos.calculate();

if (shapeObject.pos.x > shapeObject.pos.x1) {
shapeObject.pos.x2 = shapeObject.pos.x;
shapeObject.pos.x = shapeObject.pos.x1;
shapeObject.pos.x1 = shapeObject.pos.x2;
}
if (shapeObject.pos.y > shapeObject.pos.y1) {
shapeObject.pos.y2 = shapeObject.pos.y;
shapeObject.pos.y = shapeObject.pos.y1;
shapeObject.pos.y1 = shapeObject.pos.y2;
}

shapes.add(shapeObject);
repaint();
}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

shapeObject.pos.x = e.getX();
shapeObject.pos.y = e.getY();

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent event) {

}

});

// Add Shapes jButtons

shapes = new LinkedList<>();

cc = new JColorChooser(canvas.getForeground());
cc.setBounds(10, 267, 592, 354);
cc.getSelectionModel().addChangeListener(this);
contentPane.add(cc);

btnSave = new JButton("Save");
btnSave.setBounds(513, 243, 89, 23);
btnSave.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
});
contentPane.add(btnSave);

}

private ActionListener createActionListener(String shapeType) {
ActionListener al = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (shapeType.equalsIgnoreCase("Circle")) {
shapeObject = new Circle();
} else if (shapeType.equalsIgnoreCase("Square")) {
shapeObject = new Square();
} else if (shapeType.equalsIgnoreCase("Rectangle")) {
shapeObject = new Rectangle();
} else if (shapeType.equalsIgnoreCase("Line")) {
shapeObject = new Line();
}
}
};
return al;
}

private ActionListener getColor(String color) {
ActionListener a = new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
if (color.equalsIgnoreCase("White")) {
canvas.setBackground(Color.white);
} else if (color.equalsIgnoreCase("Black")) {
canvas.setBackground(Color.black);
} else if (color.equalsIgnoreCase("Red")) {
canvas.setBackground(Color.red);
} else if (color.equalsIgnoreCase("Blue")) {
canvas.setBackground(Color.blue);
} else if (color.equalsIgnoreCase("Green")) {
canvas.setBackground(Color.green);
} else if (color.equalsIgnoreCase("Yellow")) {
canvas.setBackground(Color.yellow);
}
repaint();
}
};
return a;
}

私有(private) JCheckBox[] getColor() {

    JCheckBox[] color = new JCheckBox[6];
color[0] = new JCheckBox("White");
color[0].setBounds(500, 200, 20, 20);
color[0].addActionListener(getColor("White"));
color[0].setBackground(Color.white);

color[1] = new JCheckBox("Black");
color[1].setBounds(520, 200, 20, 20);
color[1].addActionListener(getColor("Black"));
color[1].setBackground(Color.black);

color[2] = new JCheckBox("Red");
color[2].setBounds(540, 200, 20, 20);
color[2].addActionListener(getColor("Red"));
color[2].setBackground(Color.red);

color[3] = new JCheckBox("Blue");
color[3].setBounds(500, 220, 20, 20);
color[3].addActionListener(getColor("Blue"));
color[3].setBackground(Color.blue);

color[4] = new JCheckBox("Green");
color[4].setBounds(520, 220, 20, 20);
color[4].addActionListener(getColor("Green"));
color[4].setBackground(Color.green);

color[5] = new JCheckBox("Yellow");
color[5].setBounds(540, 220, 20, 20);
color[5].addActionListener(getColor("Yellow"));
color[5].setBackground(Color.yellow);

ButtonGroup bg = new ButtonGroup();
bg.add(color[0]);
bg.add(color[1]);
bg.add(color[2]);
bg.add(color[3]);
bg.add(color[4]);
bg.add(color[5]);

return color;

}

私有(private) JRadioButton[] getShapesRadioButtons() { JRadioButton[] 形状 = new JRadioButton[4];

    shape[0] = new JRadioButton("Circle");
shape[0].addActionListener(createActionListener("Circle"));
shape[0].getColorModel();
shape[0].setBounds(10, 200, 60, 60);

shape[1] = new JRadioButton("Square");
shape[1].addActionListener(createActionListener("Square"));
shape[1].setBounds(70, 200, 80, 60);

shape[2] = new JRadioButton("Rectangle");
shape[2].addActionListener(createActionListener("Rectangle"));
shape[2].setBounds(150, 200, 100, 60);

shape[3] = new JRadioButton("Line");
shape[3].addActionListener(createActionListener("Line"));
shape[3].setBounds(250, 200, 80, 60);

ButtonGroup bg = new ButtonGroup();
bg.add(shape[0]);
bg.add(shape[1]);
bg.add(shape[2]);
bg.add(shape[3]);

return shape;
}

@Override
public void stateChanged(ChangeEvent arg0) {
// TODO Auto-generated method stub
Color newColor = cc.getColor();
canvas.setForeground(newColor);
repaint();
}

@Override
public void paint(Graphics g) {
super.paint(g);

for (Shape shape : shapes) {
shape.paintComponent((Graphics2D) canvas.getGraphics());

}

/*
* for (int i = 0; i < shapes.size(); i++) { Shape shape =
* shapes.get(i);
* shape.paintComponent((Graphics2D)canvas.getGraphics()); }
*/}}

还有一些用于计算和绘制形状的类

最佳答案

我想说,一种选择是使要保留在数据库中的对象可序列化,然后将它们存储在 BLOB 列中。

您会在网上找到很多有关序列化的教程,这只是其中之一:https://www.tutorialspoint.com/java/java_serialization.htm

关于java - 在 MySql 中存储形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45962524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com