Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
缓冲图像上的Java滚动窗格_Java_Swing_Jpanel_Jscrollpane_Bufferedimage - Fatal编程技术网

缓冲图像上的Java滚动窗格

缓冲图像上的Java滚动窗格,java,swing,jpanel,jscrollpane,bufferedimage,Java,Swing,Jpanel,Jscrollpane,Bufferedimage,所以我有4个类,在我的画图程序中,我试图在画布上添加滚动条,这样我可以滚动,当我制作缓冲图像时,它被设置为桌面的大小,但是JFrame只有700左右像素,所以当你展开或调整大小时,你仍然在画布上。但我不知道如何将JScrollPane添加到缓冲图像中,以便可以滚动 //MAIN// import javax.swing.SwingUtilities; import javax.swing.UIManager; public class Main { public static int

所以我有4个类,在我的画图程序中,我试图在画布上添加滚动条,这样我可以滚动,当我制作缓冲图像时,它被设置为桌面的大小,但是JFrame只有700左右像素,所以当你展开或调整大小时,你仍然在画布上。但我不知道如何将JScrollPane添加到缓冲图像中,以便可以滚动

//MAIN//

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main 
{
    public static int choice;

     public static void main(String[] args) 
     {
            Board.getInstance();


     }
}
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Menu extends JPanel implements MouseListener, ActionListener{

    public static Color bgColor = null;
    public static int stroke = 0;
    public static Menu instance;
    private JButton clear;
    private JButton line;
    private JButton color;
    private JButton erase;
    private JButton pen;
    public static boolean once = true;
    public static BufferedImage buf = new BufferedImage(50,25,2);
    public static Graphics2D gr = buf.createGraphics();
    public static BufferedImage buf2 = new BufferedImage(50,25,2);
    public static Graphics2D gr2 = buf2.createGraphics();
    public static BufferedImage buf3 = new BufferedImage(50,25,2);
    public static Graphics2D gr3 = buf3.createGraphics();
    public static BufferedImage buf1 = new BufferedImage(50,25,2);
    public static Graphics2D gr1 = buf1.createGraphics();
    Menu()
    {
        setBackground(Color.GRAY);
        TitledBorder titledBorder = BorderFactory.createTitledBorder("Toolbox");
        setBorder(titledBorder);


        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
;


       clear = new JButton("Clear");
       clear.setActionCommand("Clear");
       clear.addActionListener(this);

       pen = new JButton("Pen");
       pen.setActionCommand("Pen");
       pen.addActionListener(this);

       erase = new JButton("Erase");
       erase.setActionCommand("e");
       erase.addActionListener(this);

       color = new JButton("Color");
       color.setActionCommand("Color");
       color.addActionListener(this);

       ImageIcon ir2 = new ImageIcon(buf3);
       JButton emptyR = new JButton(ir2);
       emptyR.addActionListener(this);
       emptyR.setActionCommand("ER");

       ImageIcon ir1 = new ImageIcon(buf2);
       JButton emptyO = new JButton(ir1);
       emptyO.addActionListener(this);
       emptyO.setActionCommand("EO");

       ImageIcon ir = new ImageIcon(buf);
       JButton fillR = new JButton(ir);
       fillR.addActionListener(this);
       fillR.setActionCommand("FR");

       ImageIcon io = new ImageIcon(buf1);
       JButton fillO = new JButton(io);
       fillO.addActionListener(this);
       fillO.setActionCommand("FO");


       line = new JButton("Line");
       line.setActionCommand("L");
       line.addActionListener(this);


       JButton button6 = new JButton("Line");
       button6.addActionListener(this);
       JRadioButton thin = new JRadioButton("Thin Line");
       thin.addActionListener(this);
       JRadioButton medium = new JRadioButton("Medium Line");
       medium.addActionListener(this);
       JRadioButton thick = new JRadioButton("Thick Line");
       thick.addActionListener(this);

        ButtonGroup lineOption = new ButtonGroup( );
        lineOption.add(thin);
        lineOption.add(medium);
        lineOption.add(thick);

        add(clear);
        add(erase);
        add(color);
        add(pen);
        add(line);
        add(thin);
        add(medium);
        add(thick);
        add(emptyR);
        add(emptyO);
        add(fillR);
        add(fillO);
        buttonGraphics();
        once = false;

    }


    public static Menu getInstance()
    {
        if(instance == null)
           instance =  new Menu();
        return instance;
    }
    public static void buttonGraphics()
    {
        if (once == true)
        {
            gr.setColor(Color.RED);
            gr1.setColor(Color.RED);
            gr2.setColor(Color.RED);
            gr3.setColor(Color.RED);
        }else

        gr3.setColor(bgColor);
        gr3.drawRect(0, 0, 48, 23);
        gr2.setColor(bgColor);
        gr2.drawOval(0, 0, 47, 23);
        gr.setColor(bgColor);
        gr.fillRect(0, 0, 50, 23);
        gr1.setColor(bgColor);
        gr1.fillOval(0, 0, 50, 25);

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Clear"))
        {
        Drawing.getInstance().clear();
        repaint();
        }

    if (e.getActionCommand().equals("e"))
    {
        Main.choice = 8;
        Drawing.getInstance().draw();
    }
    if (e.getActionCommand().equals("Color"))
        {
        bgColor = JColorChooser.showDialog(this,"Choose Background Color", getBackground());
        gr.setColor(bgColor);
        gr1.setColor(bgColor);
        gr2.setColor(bgColor);
        gr3.setColor(bgColor);
        Main.choice = 7;
        buttonGraphics();
        repaint();
        Drawing.getInstance().draw();
        }
        if (e.getActionCommand().equals("L"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 1;
        repaint();
        }
    if (e.getActionCommand().equals("FR"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 2;
        repaint();
        }
    if (e.getActionCommand().equals("EO"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 3;
        repaint();
        }
    if (e.getActionCommand().equals("FO"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 4;
        repaint();
        }
    if(e.getActionCommand().equals("ER"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 5;

        repaint();
        }
    if (e.getActionCommand().equals("Thin Line"))
      {
      stroke = 0;
      Drawing.getInstance().eraserHeight = 5;
      Drawing.getInstance().eraserWidth = 5;
      }

    if (e.getActionCommand().equals("Medium Line"))
        {
        stroke = 1;
          Drawing.getInstance().eraserHeight = 15;
          Drawing.getInstance().eraserWidth = 15;
        }

    if (e.getActionCommand().equals("Thick Line"))
        {
        stroke = 2;
          Drawing.getInstance().eraserHeight = 25;
          Drawing.getInstance().eraserWidth = 25;
        }
    if(e.getActionCommand().equals("Pen"))
    {

        Main.choice = 10;
        Drawing.getInstance().draw();

    }


}

    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}



}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Drawing extends JPanel implements MouseListener, ActionListener, MouseMotionListener
{
    public int x1, x2 ,y1, y2;
    public static Drawing instance;
    public int FoE;
    public int eraserWidth = 15;
    public int eraserHeight = 15;

    BufferedImage grid;
    static Graphics2D gc;




    Drawing()
    {
        setBackground(Color.RED);
        addMouseListener(this);   
    }
    public static Drawing getInstance()
    {
        if(instance == null)
            instance =  new Drawing();
         return instance;

    }
    public void paintComponent(Graphics g)
    { 
         super.paintComponent(g);  

         Graphics2D g2 = (Graphics2D)g;
         if(grid == null)
         {
            Toolkit toolkit =  Toolkit.getDefaultToolkit ();
            Dimension dim = toolkit.getScreenSize();
            grid = (BufferedImage)(this.createImage(dim.width,dim.height));
            gc = grid.createGraphics();
            gc.setColor(Color.RED);
            BufferedImage grid;
            Graphics2D gc;
         }
         g2.drawImage(grid, null, 0, 0);
    }
public void draw()
{
    Graphics2D g = (Graphics2D)getGraphics();
    int w = x2 - x1;
    if (w<0)
    w = w *(-1);
    int h = y2-y1;
    if (h<0)
    h=  h*(-1);
    gc.setColor(Menu.bgColor);
    switch(Main.choice)
    {
        case 1:
        {
            removeMouseMotionListener(instance);
            if (Menu.stroke == 0)
                gc.setStroke(new BasicStroke (1));
                if (Menu.stroke == 1)
                gc.setStroke(new BasicStroke (3));
                if (Menu.stroke == 2)
                gc.setStroke(new BasicStroke (6));
            gc.drawLine(x1, y1, x2, y2);
            repaint();
            break;
        }
        case 2:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawRect(x1, y1, w, h);
            gc.fillRect(x1, y1, w, h);
            repaint();
            break;
        }
        case 3:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawOval(x1, y1, w, h);
            repaint();
            break;
        }
        case 4:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawOval(x1, y1, w, h);
            gc.fillOval(x1, y1, w, h);
            repaint();
            break;
        }
        case 5:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawRect(x1, y1, w, h);
            repaint();
            break;
        }
        case 6:
        {
            removeMouseMotionListener(instance);
            repaint();
            Color temp = gc.getColor();
            gc.setColor(Color.WHITE);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;
        }
        case 7:
        {
            removeMouseMotionListener(instance);
            gc.setColor(Menu.bgColor);
            break;
        }
        case 8:
        {
            FoE = 0;
            addMouseMotionListener(instance);
            break;
        }
        case 10:
        {
            FoE = 1;
            addMouseMotionListener(instance);
            break;
        }

    }
}


    public void check()
    {
        if (Menu.stroke == 0)
            gc.setStroke(new BasicStroke (1));
            if (Menu.stroke == 1)
            gc.setStroke(new BasicStroke (3));
            if (Menu.stroke == 2)
            gc.setStroke(new BasicStroke (6));


        if (x1 > x2)
        {
            int z = 0;
            z = x1;
            x1 = x2;
            x2 =z;
        }
        if (y1 > y2)
        {
            int z = 0;
            z = y1;
            y1 = y2;
            y2 = z;
        }
    }

    public void clear()
    {
        repaint();
        Color temp = gc.getColor();
        gc.setColor(Color.WHITE);
        gc.fillRect(0, 0, getWidth(), getHeight());
        gc.setColor(temp);
        repaint();
    }

    public void mouseExited(MouseEvent e){ }
    public void mouseEntered(MouseEvent e){}
    public void mouseClicked(MouseEvent e){ 
    }

    public void mousePressed(MouseEvent evt)
    {
        x1 = evt.getX();
        y1= evt.getY();
    }
    public void mouseReleased(MouseEvent evt)
    {
        x2 = evt.getX();
        y2 = evt.getY();
        removeMouseMotionListener(instance);
        draw();
    }


    public void mouseDragged(MouseEvent me)
    {
        check();
        if (FoE == 0)
        {

        Color c = gc.getColor();
        gc.setColor(Color.WHITE);
        gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.setColor(c);
        repaint();
        }
        else if (FoE == 1)
        {
         gc.setColor(gc.getColor());
         gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
         gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
         repaint();
        }
        else
        {

        }
    }

    public void mouseMoved(MouseEvent arg0)
    {

    }
    public void actionPerformed(ActionEvent arg0) {

    }


}
//FRAME//

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author Calvin Moss
 */
public class Board extends JFrame implements ActionListener
{

     public static Board inst;

    Board()
    {


         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         Container c = getContentPane();
         c.add(Menu.getInstance(), BorderLayout.NORTH);
         getContentPane().add(Drawing.getInstance(), BorderLayout.CENTER);

         Drawing.getInstance().add(new JScrollPane());

         setSize(1200, 800);
         setBackground(Color.WHITE);
         setVisible(true);


         JMenuBar menuBar = new JMenuBar();
         setJMenuBar(menuBar);
         JMenu help = new JMenu("Help");
         menuBar.add(help);
         JMenuItem about = new JMenuItem("About");
         help.add(about);
         about.addActionListener(this);
    }


public static Board getInstance()
    {
        if(inst == null)
           inst = new Board();
         return inst;
    }

public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("About"))
        {
            JFrame about = new JFrame("About");
            about.setSize(300, 300);
            JButton picture = new JButton(new ImageIcon("C:/Users/TehRobot/Desktop/Logo.png"));
            about.add(picture);
            about.setVisible(true);
        }

}


}
//菜单面板//

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main 
{
    public static int choice;

     public static void main(String[] args) 
     {
            Board.getInstance();


     }
}
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Menu extends JPanel implements MouseListener, ActionListener{

    public static Color bgColor = null;
    public static int stroke = 0;
    public static Menu instance;
    private JButton clear;
    private JButton line;
    private JButton color;
    private JButton erase;
    private JButton pen;
    public static boolean once = true;
    public static BufferedImage buf = new BufferedImage(50,25,2);
    public static Graphics2D gr = buf.createGraphics();
    public static BufferedImage buf2 = new BufferedImage(50,25,2);
    public static Graphics2D gr2 = buf2.createGraphics();
    public static BufferedImage buf3 = new BufferedImage(50,25,2);
    public static Graphics2D gr3 = buf3.createGraphics();
    public static BufferedImage buf1 = new BufferedImage(50,25,2);
    public static Graphics2D gr1 = buf1.createGraphics();
    Menu()
    {
        setBackground(Color.GRAY);
        TitledBorder titledBorder = BorderFactory.createTitledBorder("Toolbox");
        setBorder(titledBorder);


        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
;


       clear = new JButton("Clear");
       clear.setActionCommand("Clear");
       clear.addActionListener(this);

       pen = new JButton("Pen");
       pen.setActionCommand("Pen");
       pen.addActionListener(this);

       erase = new JButton("Erase");
       erase.setActionCommand("e");
       erase.addActionListener(this);

       color = new JButton("Color");
       color.setActionCommand("Color");
       color.addActionListener(this);

       ImageIcon ir2 = new ImageIcon(buf3);
       JButton emptyR = new JButton(ir2);
       emptyR.addActionListener(this);
       emptyR.setActionCommand("ER");

       ImageIcon ir1 = new ImageIcon(buf2);
       JButton emptyO = new JButton(ir1);
       emptyO.addActionListener(this);
       emptyO.setActionCommand("EO");

       ImageIcon ir = new ImageIcon(buf);
       JButton fillR = new JButton(ir);
       fillR.addActionListener(this);
       fillR.setActionCommand("FR");

       ImageIcon io = new ImageIcon(buf1);
       JButton fillO = new JButton(io);
       fillO.addActionListener(this);
       fillO.setActionCommand("FO");


       line = new JButton("Line");
       line.setActionCommand("L");
       line.addActionListener(this);


       JButton button6 = new JButton("Line");
       button6.addActionListener(this);
       JRadioButton thin = new JRadioButton("Thin Line");
       thin.addActionListener(this);
       JRadioButton medium = new JRadioButton("Medium Line");
       medium.addActionListener(this);
       JRadioButton thick = new JRadioButton("Thick Line");
       thick.addActionListener(this);

        ButtonGroup lineOption = new ButtonGroup( );
        lineOption.add(thin);
        lineOption.add(medium);
        lineOption.add(thick);

        add(clear);
        add(erase);
        add(color);
        add(pen);
        add(line);
        add(thin);
        add(medium);
        add(thick);
        add(emptyR);
        add(emptyO);
        add(fillR);
        add(fillO);
        buttonGraphics();
        once = false;

    }


    public static Menu getInstance()
    {
        if(instance == null)
           instance =  new Menu();
        return instance;
    }
    public static void buttonGraphics()
    {
        if (once == true)
        {
            gr.setColor(Color.RED);
            gr1.setColor(Color.RED);
            gr2.setColor(Color.RED);
            gr3.setColor(Color.RED);
        }else

        gr3.setColor(bgColor);
        gr3.drawRect(0, 0, 48, 23);
        gr2.setColor(bgColor);
        gr2.drawOval(0, 0, 47, 23);
        gr.setColor(bgColor);
        gr.fillRect(0, 0, 50, 23);
        gr1.setColor(bgColor);
        gr1.fillOval(0, 0, 50, 25);

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Clear"))
        {
        Drawing.getInstance().clear();
        repaint();
        }

    if (e.getActionCommand().equals("e"))
    {
        Main.choice = 8;
        Drawing.getInstance().draw();
    }
    if (e.getActionCommand().equals("Color"))
        {
        bgColor = JColorChooser.showDialog(this,"Choose Background Color", getBackground());
        gr.setColor(bgColor);
        gr1.setColor(bgColor);
        gr2.setColor(bgColor);
        gr3.setColor(bgColor);
        Main.choice = 7;
        buttonGraphics();
        repaint();
        Drawing.getInstance().draw();
        }
        if (e.getActionCommand().equals("L"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 1;
        repaint();
        }
    if (e.getActionCommand().equals("FR"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 2;
        repaint();
        }
    if (e.getActionCommand().equals("EO"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 3;
        repaint();
        }
    if (e.getActionCommand().equals("FO"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 4;
        repaint();
        }
    if(e.getActionCommand().equals("ER"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 5;

        repaint();
        }
    if (e.getActionCommand().equals("Thin Line"))
      {
      stroke = 0;
      Drawing.getInstance().eraserHeight = 5;
      Drawing.getInstance().eraserWidth = 5;
      }

    if (e.getActionCommand().equals("Medium Line"))
        {
        stroke = 1;
          Drawing.getInstance().eraserHeight = 15;
          Drawing.getInstance().eraserWidth = 15;
        }

    if (e.getActionCommand().equals("Thick Line"))
        {
        stroke = 2;
          Drawing.getInstance().eraserHeight = 25;
          Drawing.getInstance().eraserWidth = 25;
        }
    if(e.getActionCommand().equals("Pen"))
    {

        Main.choice = 10;
        Drawing.getInstance().draw();

    }


}

    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}



}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Drawing extends JPanel implements MouseListener, ActionListener, MouseMotionListener
{
    public int x1, x2 ,y1, y2;
    public static Drawing instance;
    public int FoE;
    public int eraserWidth = 15;
    public int eraserHeight = 15;

    BufferedImage grid;
    static Graphics2D gc;




    Drawing()
    {
        setBackground(Color.RED);
        addMouseListener(this);   
    }
    public static Drawing getInstance()
    {
        if(instance == null)
            instance =  new Drawing();
         return instance;

    }
    public void paintComponent(Graphics g)
    { 
         super.paintComponent(g);  

         Graphics2D g2 = (Graphics2D)g;
         if(grid == null)
         {
            Toolkit toolkit =  Toolkit.getDefaultToolkit ();
            Dimension dim = toolkit.getScreenSize();
            grid = (BufferedImage)(this.createImage(dim.width,dim.height));
            gc = grid.createGraphics();
            gc.setColor(Color.RED);
            BufferedImage grid;
            Graphics2D gc;
         }
         g2.drawImage(grid, null, 0, 0);
    }
public void draw()
{
    Graphics2D g = (Graphics2D)getGraphics();
    int w = x2 - x1;
    if (w<0)
    w = w *(-1);
    int h = y2-y1;
    if (h<0)
    h=  h*(-1);
    gc.setColor(Menu.bgColor);
    switch(Main.choice)
    {
        case 1:
        {
            removeMouseMotionListener(instance);
            if (Menu.stroke == 0)
                gc.setStroke(new BasicStroke (1));
                if (Menu.stroke == 1)
                gc.setStroke(new BasicStroke (3));
                if (Menu.stroke == 2)
                gc.setStroke(new BasicStroke (6));
            gc.drawLine(x1, y1, x2, y2);
            repaint();
            break;
        }
        case 2:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawRect(x1, y1, w, h);
            gc.fillRect(x1, y1, w, h);
            repaint();
            break;
        }
        case 3:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawOval(x1, y1, w, h);
            repaint();
            break;
        }
        case 4:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawOval(x1, y1, w, h);
            gc.fillOval(x1, y1, w, h);
            repaint();
            break;
        }
        case 5:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawRect(x1, y1, w, h);
            repaint();
            break;
        }
        case 6:
        {
            removeMouseMotionListener(instance);
            repaint();
            Color temp = gc.getColor();
            gc.setColor(Color.WHITE);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;
        }
        case 7:
        {
            removeMouseMotionListener(instance);
            gc.setColor(Menu.bgColor);
            break;
        }
        case 8:
        {
            FoE = 0;
            addMouseMotionListener(instance);
            break;
        }
        case 10:
        {
            FoE = 1;
            addMouseMotionListener(instance);
            break;
        }

    }
}


    public void check()
    {
        if (Menu.stroke == 0)
            gc.setStroke(new BasicStroke (1));
            if (Menu.stroke == 1)
            gc.setStroke(new BasicStroke (3));
            if (Menu.stroke == 2)
            gc.setStroke(new BasicStroke (6));


        if (x1 > x2)
        {
            int z = 0;
            z = x1;
            x1 = x2;
            x2 =z;
        }
        if (y1 > y2)
        {
            int z = 0;
            z = y1;
            y1 = y2;
            y2 = z;
        }
    }

    public void clear()
    {
        repaint();
        Color temp = gc.getColor();
        gc.setColor(Color.WHITE);
        gc.fillRect(0, 0, getWidth(), getHeight());
        gc.setColor(temp);
        repaint();
    }

    public void mouseExited(MouseEvent e){ }
    public void mouseEntered(MouseEvent e){}
    public void mouseClicked(MouseEvent e){ 
    }

    public void mousePressed(MouseEvent evt)
    {
        x1 = evt.getX();
        y1= evt.getY();
    }
    public void mouseReleased(MouseEvent evt)
    {
        x2 = evt.getX();
        y2 = evt.getY();
        removeMouseMotionListener(instance);
        draw();
    }


    public void mouseDragged(MouseEvent me)
    {
        check();
        if (FoE == 0)
        {

        Color c = gc.getColor();
        gc.setColor(Color.WHITE);
        gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.setColor(c);
        repaint();
        }
        else if (FoE == 1)
        {
         gc.setColor(gc.getColor());
         gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
         gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
         repaint();
        }
        else
        {

        }
    }

    public void mouseMoved(MouseEvent arg0)
    {

    }
    public void actionPerformed(ActionEvent arg0) {

    }


}
//绘图画布面板//

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main 
{
    public static int choice;

     public static void main(String[] args) 
     {
            Board.getInstance();


     }
}
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.border.TitledBorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Menu extends JPanel implements MouseListener, ActionListener{

    public static Color bgColor = null;
    public static int stroke = 0;
    public static Menu instance;
    private JButton clear;
    private JButton line;
    private JButton color;
    private JButton erase;
    private JButton pen;
    public static boolean once = true;
    public static BufferedImage buf = new BufferedImage(50,25,2);
    public static Graphics2D gr = buf.createGraphics();
    public static BufferedImage buf2 = new BufferedImage(50,25,2);
    public static Graphics2D gr2 = buf2.createGraphics();
    public static BufferedImage buf3 = new BufferedImage(50,25,2);
    public static Graphics2D gr3 = buf3.createGraphics();
    public static BufferedImage buf1 = new BufferedImage(50,25,2);
    public static Graphics2D gr1 = buf1.createGraphics();
    Menu()
    {
        setBackground(Color.GRAY);
        TitledBorder titledBorder = BorderFactory.createTitledBorder("Toolbox");
        setBorder(titledBorder);


        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
;


       clear = new JButton("Clear");
       clear.setActionCommand("Clear");
       clear.addActionListener(this);

       pen = new JButton("Pen");
       pen.setActionCommand("Pen");
       pen.addActionListener(this);

       erase = new JButton("Erase");
       erase.setActionCommand("e");
       erase.addActionListener(this);

       color = new JButton("Color");
       color.setActionCommand("Color");
       color.addActionListener(this);

       ImageIcon ir2 = new ImageIcon(buf3);
       JButton emptyR = new JButton(ir2);
       emptyR.addActionListener(this);
       emptyR.setActionCommand("ER");

       ImageIcon ir1 = new ImageIcon(buf2);
       JButton emptyO = new JButton(ir1);
       emptyO.addActionListener(this);
       emptyO.setActionCommand("EO");

       ImageIcon ir = new ImageIcon(buf);
       JButton fillR = new JButton(ir);
       fillR.addActionListener(this);
       fillR.setActionCommand("FR");

       ImageIcon io = new ImageIcon(buf1);
       JButton fillO = new JButton(io);
       fillO.addActionListener(this);
       fillO.setActionCommand("FO");


       line = new JButton("Line");
       line.setActionCommand("L");
       line.addActionListener(this);


       JButton button6 = new JButton("Line");
       button6.addActionListener(this);
       JRadioButton thin = new JRadioButton("Thin Line");
       thin.addActionListener(this);
       JRadioButton medium = new JRadioButton("Medium Line");
       medium.addActionListener(this);
       JRadioButton thick = new JRadioButton("Thick Line");
       thick.addActionListener(this);

        ButtonGroup lineOption = new ButtonGroup( );
        lineOption.add(thin);
        lineOption.add(medium);
        lineOption.add(thick);

        add(clear);
        add(erase);
        add(color);
        add(pen);
        add(line);
        add(thin);
        add(medium);
        add(thick);
        add(emptyR);
        add(emptyO);
        add(fillR);
        add(fillO);
        buttonGraphics();
        once = false;

    }


    public static Menu getInstance()
    {
        if(instance == null)
           instance =  new Menu();
        return instance;
    }
    public static void buttonGraphics()
    {
        if (once == true)
        {
            gr.setColor(Color.RED);
            gr1.setColor(Color.RED);
            gr2.setColor(Color.RED);
            gr3.setColor(Color.RED);
        }else

        gr3.setColor(bgColor);
        gr3.drawRect(0, 0, 48, 23);
        gr2.setColor(bgColor);
        gr2.drawOval(0, 0, 47, 23);
        gr.setColor(bgColor);
        gr.fillRect(0, 0, 50, 23);
        gr1.setColor(bgColor);
        gr1.fillOval(0, 0, 50, 25);

    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Clear"))
        {
        Drawing.getInstance().clear();
        repaint();
        }

    if (e.getActionCommand().equals("e"))
    {
        Main.choice = 8;
        Drawing.getInstance().draw();
    }
    if (e.getActionCommand().equals("Color"))
        {
        bgColor = JColorChooser.showDialog(this,"Choose Background Color", getBackground());
        gr.setColor(bgColor);
        gr1.setColor(bgColor);
        gr2.setColor(bgColor);
        gr3.setColor(bgColor);
        Main.choice = 7;
        buttonGraphics();
        repaint();
        Drawing.getInstance().draw();
        }
        if (e.getActionCommand().equals("L"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 1;
        repaint();
        }
    if (e.getActionCommand().equals("FR"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 2;
        repaint();
        }
    if (e.getActionCommand().equals("EO"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 3;
        repaint();
        }
    if (e.getActionCommand().equals("FO"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 4;
        repaint();
        }
    if(e.getActionCommand().equals("ER"))
        {
        Drawing.getInstance().FoE = 2;
        Main.choice = 5;

        repaint();
        }
    if (e.getActionCommand().equals("Thin Line"))
      {
      stroke = 0;
      Drawing.getInstance().eraserHeight = 5;
      Drawing.getInstance().eraserWidth = 5;
      }

    if (e.getActionCommand().equals("Medium Line"))
        {
        stroke = 1;
          Drawing.getInstance().eraserHeight = 15;
          Drawing.getInstance().eraserWidth = 15;
        }

    if (e.getActionCommand().equals("Thick Line"))
        {
        stroke = 2;
          Drawing.getInstance().eraserHeight = 25;
          Drawing.getInstance().eraserWidth = 25;
        }
    if(e.getActionCommand().equals("Pen"))
    {

        Main.choice = 10;
        Drawing.getInstance().draw();

    }


}

    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){}
    public void mouseReleased(MouseEvent e){}



}
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;

import javax.swing.JColorChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import java.awt.event.*;
import java.awt.image.BufferedImage;


/**
 *
 * @author Calvin Moss
 */
public class Drawing extends JPanel implements MouseListener, ActionListener, MouseMotionListener
{
    public int x1, x2 ,y1, y2;
    public static Drawing instance;
    public int FoE;
    public int eraserWidth = 15;
    public int eraserHeight = 15;

    BufferedImage grid;
    static Graphics2D gc;




    Drawing()
    {
        setBackground(Color.RED);
        addMouseListener(this);   
    }
    public static Drawing getInstance()
    {
        if(instance == null)
            instance =  new Drawing();
         return instance;

    }
    public void paintComponent(Graphics g)
    { 
         super.paintComponent(g);  

         Graphics2D g2 = (Graphics2D)g;
         if(grid == null)
         {
            Toolkit toolkit =  Toolkit.getDefaultToolkit ();
            Dimension dim = toolkit.getScreenSize();
            grid = (BufferedImage)(this.createImage(dim.width,dim.height));
            gc = grid.createGraphics();
            gc.setColor(Color.RED);
            BufferedImage grid;
            Graphics2D gc;
         }
         g2.drawImage(grid, null, 0, 0);
    }
public void draw()
{
    Graphics2D g = (Graphics2D)getGraphics();
    int w = x2 - x1;
    if (w<0)
    w = w *(-1);
    int h = y2-y1;
    if (h<0)
    h=  h*(-1);
    gc.setColor(Menu.bgColor);
    switch(Main.choice)
    {
        case 1:
        {
            removeMouseMotionListener(instance);
            if (Menu.stroke == 0)
                gc.setStroke(new BasicStroke (1));
                if (Menu.stroke == 1)
                gc.setStroke(new BasicStroke (3));
                if (Menu.stroke == 2)
                gc.setStroke(new BasicStroke (6));
            gc.drawLine(x1, y1, x2, y2);
            repaint();
            break;
        }
        case 2:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawRect(x1, y1, w, h);
            gc.fillRect(x1, y1, w, h);
            repaint();
            break;
        }
        case 3:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawOval(x1, y1, w, h);
            repaint();
            break;
        }
        case 4:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawOval(x1, y1, w, h);
            gc.fillOval(x1, y1, w, h);
            repaint();
            break;
        }
        case 5:
        {
            removeMouseMotionListener(instance);
            check();
            gc.drawRect(x1, y1, w, h);
            repaint();
            break;
        }
        case 6:
        {
            removeMouseMotionListener(instance);
            repaint();
            Color temp = gc.getColor();
            gc.setColor(Color.WHITE);
            gc.fillRect(0, 0, getWidth(), getHeight());
            gc.setColor(temp);
            repaint();
            break;
        }
        case 7:
        {
            removeMouseMotionListener(instance);
            gc.setColor(Menu.bgColor);
            break;
        }
        case 8:
        {
            FoE = 0;
            addMouseMotionListener(instance);
            break;
        }
        case 10:
        {
            FoE = 1;
            addMouseMotionListener(instance);
            break;
        }

    }
}


    public void check()
    {
        if (Menu.stroke == 0)
            gc.setStroke(new BasicStroke (1));
            if (Menu.stroke == 1)
            gc.setStroke(new BasicStroke (3));
            if (Menu.stroke == 2)
            gc.setStroke(new BasicStroke (6));


        if (x1 > x2)
        {
            int z = 0;
            z = x1;
            x1 = x2;
            x2 =z;
        }
        if (y1 > y2)
        {
            int z = 0;
            z = y1;
            y1 = y2;
            y2 = z;
        }
    }

    public void clear()
    {
        repaint();
        Color temp = gc.getColor();
        gc.setColor(Color.WHITE);
        gc.fillRect(0, 0, getWidth(), getHeight());
        gc.setColor(temp);
        repaint();
    }

    public void mouseExited(MouseEvent e){ }
    public void mouseEntered(MouseEvent e){}
    public void mouseClicked(MouseEvent e){ 
    }

    public void mousePressed(MouseEvent evt)
    {
        x1 = evt.getX();
        y1= evt.getY();
    }
    public void mouseReleased(MouseEvent evt)
    {
        x2 = evt.getX();
        y2 = evt.getY();
        removeMouseMotionListener(instance);
        draw();
    }


    public void mouseDragged(MouseEvent me)
    {
        check();
        if (FoE == 0)
        {

        Color c = gc.getColor();
        gc.setColor(Color.WHITE);
        gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
        gc.setColor(c);
        repaint();
        }
        else if (FoE == 1)
        {
         gc.setColor(gc.getColor());
         gc.drawOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
         gc.fillOval(me.getX(), me.getY(), eraserWidth, eraserHeight);
         repaint();
        }
        else
        {

        }
    }

    public void mouseMoved(MouseEvent arg0)
    {

    }
    public void actionPerformed(ActionEvent arg0) {

    }


}
import java.awt.BasicStroke;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Toolkit;
导入javax.swing.JColorChooser;
导入javax.swing.JPanel;
导入javax.swing.JScrollPane;
导入java.awt.event.*;
导入java.awt.image.buffereImage;
/**
*
*@作者卡尔文·莫斯
*/
公共类绘图扩展JPanel实现MouseListener、ActionListener、MouseMotionListener
{
公共int x1、x2、y1、y2;
公共静态绘图实例;
公敌;
公共整数橡皮擦宽度=15;
公共照明=15;
缓冲图像网格;
静态图形2d-gc;
图纸()
{
挫折地面(颜色:红色);
addMouseListener(这个);
}
公共静态图形getInstance()
{
if(实例==null)
实例=新图形();
返回实例;
}
公共组件(图形g)
{ 
超级组件(g);
图形2d g2=(图形2d)g;
if(grid==null)
{
Toolkit=Toolkit.getDefaultToolkit();
维度dim=toolkit.getScreenSize();
grid=(BuffereImage)(this.createImage(dim.width,dim.height));
gc=grid.createGraphics();
gc.setColor(Color.RED);
缓冲图像网格;
图2d-gc;
}
g2.drawImage(网格,null,0,0);
}
公众抽签()
{
Graphics2D g=(Graphics2D)getGraphics();
int w=x2-x1;
如果(带y2)
{
int z=0;
z=y1;
y1=y2;
y2=z;
}
}
公共空间清除()
{
重新油漆();
Color temp=gc.getColor();
gc.setColor(Color.WHITE);
gc.fillRect(0,0,getWidth(),getHeight());
gc.setColor(温度);
重新油漆();
}
公共无效mouseExited(MouseEvent e){}
公共无效mouseenterned(MouseEvent e){}
公共无效mouseClicked(MouseEvent e){
}
公共无效鼠标按下(MouseEvent evt)
{
x1=evt.getX();
y1=evt.getY();
}
公共无效MouseEvent evt(MouseEvent evt)
{
x2=evt.getX();
y2=evt.getY();
removeMouseMotionListener(实例);
draw();
}
公共无效鼠标标记(MouseEvent me)
{
检查();
如果(FoE==0)
{
Color c=gc.getColor();
gc.setColor(Color.WHITE);
gc.drawOval(me.getX(),me.getY(),橡皮擦宽度,橡皮擦亮度);
gc.fillOval(me.getX(),me.getY(),橡皮擦宽度,橡皮擦亮度);
gc.setColor(c);
重新油漆();
}
否则如果(FoE==1)
{
gc.setColor(gc.getColor());
gc.drawOval(me.getX(),me.getY(),橡皮擦宽度,橡皮擦亮度);
gc.fillOval(me.getX(),me.getY(),橡皮擦宽度,橡皮擦亮度);
重新油漆();
}
其他的
{
}
}
public void mouseMoved(MouseEvent arg0)
{
}
已执行的公共无效操作(操作事件arg0){
}
}

我尚未测试您的代码,但看起来您正在尝试将add添加到
JPanel
(绘图)中。相反,您应该将
JPanel
添加到,然后将添加到框架的内容窗格:

JScrollPane scrollPane = new JScrollPane(Drawing.getInstance());
getContentPane().add(scrollPane, BorderLayout.CENTER);

您需要重写绘图类的getPreferredSize()方法以返回BuffereImage的大小

只有当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,才会显示滚动条