最小化之后画板内容会消失
已经设立了一个shape来装图片对象
但是还是不管用
主体部分
public class mainBody extends JFrame {
private ArrayList<Shape>listshape;
private JFrame frame;
private JPanel panel;
private JButton button;
private DrawListener d1; 
private Graphics2D g;
public mainBody() {
d1=new DrawListener();
setTitle("画图软件");
setSize(1200, 900);
setLocationRelativeTo(null);
FlowLayout layout = new FlowLayout(FlowLayout.CENTER);   
  setLayout(layout);//主窗体
JPanel panel2=new JPanel();
JPanel panel1=new JPanel();
JPanel panel3=new JPanel();
JPanel panel4=new JPanel();
panel1=new JPanel(new FlowLayout(FlowLayout.LEADING));
panel2=new JPanel(new FlowLayout(FlowLayout.CENTER));
panel3=new JPanel(new FlowLayout(FlowLayout.CENTER)); panel4=new JPanel(new FlowLayout(FlowLayout.LEFT)); JButton usingbutton1 = new JButton("保存"); panel4.add(usingbutton1); JButton button1 = new JButton("圆形"); JButton button2 = new JButton("矩形"); JButton button3 = new JButton("橡皮"); JButton button4 = new JButton("铅笔1f"); JButton button5 = new JButton("铅笔3f"); JButton button6 = new JButton("铅笔5f"); JButton button7 = new JButton("铅笔7f");//按钮 panel3.add(button1); panel3.add(button2); panel3.add(button3); panel3.add(button4); panel3.add(button5);  panel3.add(button6); panel3.add(button7);//添加图形按钮 button1.addActionListener(d1); button2.addActionListener(d1); button3.addActionListener(d1); button4.addActionListener(d1); button5.addActionListener(d1); button6.addActionListener(d1); button7.addActionListener(d1);//加入监听 JButton colorButton1 = new JButton(" "); JButton colorButton2 = new JButton(" "); JButton colorButton3 = new JButton(" "); JButton colorButton4 = new JButton(" "); JButton colorButton5 = new JButton(" ");//颜色按钮 colorButton1.setBackground(Color.BLACK); colorButton2.setBackground(Color.yellow); colorButton3.setBackground(Color.blue); colorButton4.setBackground(Color.green); colorButton5.setBackground(Color.gray);//设定颜色 panel1.add(colorButton1); panel1.add(colorButton2); panel1.add(colorButton3); panel1.add(colorButton4); panel1.add(colorButton5);//加入颜色按钮 colorButton1.addActionListener(d1); colorButton2.addActionListener(d1); colorButton3.addActionListener(d1); colorButton4.addActionListener(d1); colorButton5.addActionListener(d1); panel2.setPreferredSize(new Dimension(1000,700));     panel2.setBackground(Color.WHITE);//画画板 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel1); this.add(panel2); this.add(panel3); this.add(panel4); this.setResizable(false);//无法改变窗体大小 setVisible(true);  g=(Graphics2D) panel2.getGraphics();     d1.G(g);     //为面板添加鼠标监听     panel2.addMouseListener(d1);     panel2.addMouseMotionListener(d1);     } public void paint(Graphics2D g) { DrawListener d=new DrawListener(); ArrayList<Shape>listshape=d.getlist(); super.paint(g); for(int i=0;i<listshape.size();i++) { Shape shape=listshape.get(i);//依次取出对象  if (shape != null) {                 shape.drawShape(g);//调用对象的画的方法             } else {                 break;                 }  } } public static void main(String[] args) { mainBody test=new mainBody(); test.setVisible(true); }}

解决方案 »

  1.   

    鼠标监听器
    public class DrawListener implements ActionListener, MouseListener,MouseMotionListener{  private Color color;//颜色  private Graphics2D g;//画笔  private String str;//区分  private int x1,y1,x2,y2;//(x1,y1)按下的坐标,(x2,y2)释放时的坐标  private JButton nowColor;//当前颜色按钮  private ArrayList<Shape>listshape; public void C(JButton nowColor) { this.nowColor=nowColor;//当前颜色 } public void G(Graphics2D g) { this.g=g; } public void actionPerformed1(ActionEvent e) {//监听方法        if ("".equals(e.getActionCommand())) {//如果按钮的文本内容为""            JButton jbu = (JButton) e.getSource();            color = jbu.getBackground();//获取按钮的背景色            g.setColor(color);        } else {//按钮文本内容不为空            str = e.getActionCommand();//将按钮的文本内容赋给name        }    } @Override public void mouseDragged(MouseEvent e) { x2=e.getX(); y2=e.getY(); if("铅笔1f".equals(str)) { Stroke stroke = new BasicStroke(1f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);            g.setStroke(stroke);            g.drawLine(x1, y1, x2, y2);            x1=x2; y1=y2;            Shape shape=new Shape();            shape.setShape(x1, x2, y1, y2, str, color);           listshape.add(shape);      } else if("铅笔3f".equals(str)) { Stroke stroke = new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);            g.setStroke(stroke);            g.drawLine(x1, y1, x2, y2);            x1=x2; y1=y2;            Shape shape=new Shape();            shape.setShape(x1, x2, y1, y2, str, color);           listshape.add(shape);     } else if("铅笔5f".equals(str)) { Stroke stroke = new BasicStroke(5f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);            g.setStroke(stroke);            g.drawLine(x1, y1, x2, y2);            x1=x2; y1=y2;            Shape shape=new Shape();            shape.setShape(x1, x2, y1, y2, str, color);           listshape.add(shape);      } else if("铅笔7f".equals(str)) { Stroke stroke = new BasicStroke(7f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);            g.setStroke(stroke);            g.drawLine(x1, y1, x2, y2);            g.drawLine(x1, y1, x2, y2);            x1=x2; y1=y2;            Shape shape=new Shape();            shape.setShape(x1, x2, y1, y2, str, color);           listshape.add(shape);      } else if("橡皮".equals(str)) { g.setColor(Color.white); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; Shape shape=new Shape();        shape.setShape(x1, x2, y1, y2, str, color);       listshape.add(shape); }   } @Override public void mouseMoved(MouseEvent e) { // TODO Auto-generated method stub //当鼠标光标移动到组件上但没有按钮被按下时调用。 } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub //在组件上单击(按下并释放)鼠标按钮时调用。 } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub //当鼠标进入组件时调用。  } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub //当鼠标退出组件时调用。  } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub //在组件上按下鼠标按钮时调用。  g.setColor(color); x1=e.getX(); y1=e.getY(); } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub //在组件上释放鼠标按钮时调用。 x2=e.getX(); y2=e.getY(); if("圆形".equals(str)) { g.drawOval(Math.min(x2, x1),Math.min(y2, y1),Math.abs(x2-x1),Math.abs(y2-y1)); Shape shape=new Shape();            shape.setShape(x1, x2, y1, y2, str, color);           listshape.add(shape); //引用draw画圆形 } else if("矩形".equals(str)) { g.drawRect(Math.min(x2, x1),Math.min(y2, y1),Math.abs(x2-x1),Math.abs(y2-y1)); Shape shape=new Shape();            shape.setShape(x1, x2, y1, y2, str, color);           listshape.add(shape); }   } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub  if (" ".equals(e.getActionCommand())) {       JButton jb = (JButton) e.getSource();       color = jb.getBackground();       nowColor.setBackground(color);//处理当前颜色     } else {       str = e.getActionCommand();     }   } public ArrayList<Shape>getlist(){ return listshape; } }