首页 > 解决方案 > 最小化窗口时如何阻止摆动从窗口中删除所有内容?

问题描述

我有这个运行良好的代码,除了我有一个问题。每当我最小化选项卡并重新打开它时,它都会删除我在那里绘制的所有内容。有想法该怎么解决这个吗?如果我移动标签也会发生这种情况,它只会删除所有内容。此外,如果我在已绘制的内容上打开其中一个菜单,则它会删除菜单选项卡所经过的那块绘图。让我知道是否需要澄清。

代码:

import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;

// This class inherits properties from JPanel (its parent).
// It also implements two interfaces: ActionListener (for the menu) and MouseMotionListener (for the mouse)
public class Lesson28 extends JPanel implements ActionListener, MouseMotionListener
{
    
    // INSTANCE VARIABLES
    
    JFrame f; // frame
    
    int windowWidth; // window width
    int windowHeight; // window width
    
    JMenuBar mb; // menu bar
    
    JMenu size; // option in the menu bar
    JMenu help; // option in the menu bar
    JMenu color;
    JMenu shape;
    JMenu background;
    
    JMenuItem thin; // menu action item
    JMenuItem medium; // menu action item
    JMenuItem thick; // menu action item
    JMenuItem epic; // menu action item
    JMenuItem red;
    JMenuItem green;
    JMenuItem blue;
    JMenuItem circle;
    JMenuItem square;
    JMenuItem erase;
    JMenuItem refresh;
    
    int radius; // drawing circle radius
    // boolean r, gr, b, er;
    boolean c, s;
    Random rand; // random number generator
    Graphics g;
    Graphics2D g2;
    boolean penDown;
    int prevx, prevy;
    
    // CONSTRUCTOR
    Lesson28()
    {
        
        windowWidth = 800;
        windowHeight = 600;
        
        radius = 10;
        c = true;
        penDown = false;
        prevx = prevy = 0;
        
        rand = new Random();
        
        f = new JFrame("Epic App"); // create a frame
        f.setIconImage(Toolkit.getDefaultToolkit().getImage("cool.jpg")); // OPTIONAL: use your own icon (stored in the same folder)
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // exit on closing
        setPreferredSize(new Dimension(windowWidth, windowHeight)); // window size
        f.add(this); // add the panel to the frame (remember: "this" class is an "extended" panel!)
        f.setBackground(new Color(255, 255, 0)); // add background (R,G,B); also can use Color.BLACK, etc.
        
        f.setResizable(false); // OPTIONAL: allow or prevent manual resizing
        
        // Create menu action items (inside dropdown menus).
        thin = new JMenuItem("Thin");
        medium = new JMenuItem("Medium");
        thick = new JMenuItem("Thick");
        epic = new JMenuItem("Epic");
        red = new JMenuItem("Red");
        green = new JMenuItem("Green");
        blue = new JMenuItem("Blue");
        circle = new JMenuItem("Circle");
        square = new JMenuItem("Square");
        erase = new JMenuItem("Eraser");
        refresh = new JMenuItem("Erase All");
        
        // Make action items produce an "event" when selected.
        thin.addActionListener(this);
        medium.addActionListener(this);
        thick.addActionListener(this);
        epic.addActionListener(this);
        red.addActionListener(this);
        green.addActionListener(this);
        blue.addActionListener(this);
        circle.addActionListener(this);
        square.addActionListener(this);
        erase.addActionListener(this);
        refresh.addActionListener(this);
        // Add options to the menu bar.
        size = new JMenu("Size");
        color = new JMenu("Color");
        shape = new JMenu("Shape");
        background = new JMenu("Background");
        // help=new JMenu("Help");
        
        // Bind action items to an option in the menu bar (size).
        size.add(thin);
        size.add(medium);
        size.add(thick);
        size.add(epic);
        color.add(red);
        color.add(green);
        color.add(blue);
        color.add(erase);
        shape.add(circle);
        shape.add(square);
        background.add(refresh);
        // Add all menu options to the menu bar.
        mb = new JMenuBar();
        mb.add(size);
        mb.add(color);
        mb.add(shape);
        mb.add(background);
        // refresh= new JMenu("Erase Everything");mb.add(help);
        
        // Add the menu bar to the frame.
        f.setJMenuBar(mb);
        
        // Add the mouse listener
        addMouseMotionListener(this);
        
        // Adjust the frame to contain the full panel (after the menu!)
        f.pack();
        
        // Make the frame visible
        f.setVisible(true);
        
        g = getGraphics();
        g2 = (Graphics2D) g;
        g2.setColor(Color.RED);
        
    }
    
    // This method is not empty! It is simply not overriden here. We need it to draw.
    // paintComponent is a protected method in JPanel and in its parent, JComponent.
    @Override
    public void paintComponent(Graphics g)
    {
    }
    
    // MENU CONTROL
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == thin)
            g2.setStroke(new BasicStroke(2));
        if(e.getSource() == medium)
            g2.setStroke(new BasicStroke(4));
        if(e.getSource() == thick)
            g2.setStroke(new BasicStroke(8));
        if(e.getSource() == epic)
            g2.setStroke(new BasicStroke(16));
        if(e.getSource() == red)
            g2.setColor(Color.RED);
        if(e.getSource() == green)
            g2.setColor(Color.GREEN);
        if(e.getSource() == blue)
            g2.setColor(Color.BLUE);
        if(e.getSource() == refresh)
            f.repaint();
        // if(e.getSource()==circle){
        // c = true;
        // s = false;
        // }
        // if(e.getSource()==square){
        // s = true;
        // c = false;
        // }
        if(e.getSource() == erase)
            g2.setColor(Color.YELLOW);
    }
    
    // MOUSE CONTROL
    @Override
    public void mouseMoved(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        penDown = false;
        // System.out.println("Mouse Move Event: x = " + x + " y = " + y);
    }
    
    @Override
    public void mouseDragged(MouseEvent e)
    {
        int x = e.getX();
        int y = e.getY();
        // System.out.println("Mouse Drag Event: x = " + x + " y = " + y);
        // not f.Graphics() which would produce coordinate mismatch
        // int r = rand.nextInt(8);
        // if(r == true) g2.setColor(Color.RED);
        // if(gr == true) g2.setColor(Color.GREEN);
        // if(b == true) g2.setColor(Color.BLUE);
        // if(er == true) g2.setColor(Color.YELLOW);
        // if(c == true) g2.fillOval(x, y, radius, radius);
        // if(s == true) g2.fillRect(x, y, radius, radius);
        if(penDown == false)
            penDown = true;
        else
            g2.drawLine(prevx, prevy, x, y);
        prevx = x;
        prevy = y;
    }
    
    public static void main(String[] args)
    {
        new Lesson28();
    }
    
}

标签: javaswingawt

解决方案


推荐阅读