缓冲图像上的 Java ScrollPane

发布于 2024-12-08 19:23:19 字数 15137 浏览 0 评论 0原文

所以我有 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();


     }
}

//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);
        }

}


}

//MENUPANEL//

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) {

    }


}

So I have 4 classes, in my paint program and I am trying to add scrollbars to the canvas so that I can scroll around, When I make my buffered Image it is set to the size of the desktop, but the JFrame is only I think 700 or so pixels so as you expand or resize you are still on canvas. But I cant figure out how to add a JScrollPane to the buffered image so I can scroll around.

//MAIN//

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

public class Main 
{
    public static int choice;

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


     }
}

//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);
        }

}


}

//MENUPANEL//

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){}



}

//DRAWING CANVAS PANEL//

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) {

    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞梦里开花 2024-12-15 19:23:19

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

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

如何使用 JScrollPane

I haven't tested your code but it looks like you're trying to add add JScrollPane to your JPanel (Drawing). It should be the other way around you should add the JPanel to the JScrollPane and then add the JScrollPane to the content pane of the frame:

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

How to use JScrollPane

冰火雁神 2024-12-15 19:23:19

您需要重写 Drawing 类的 getPreferredSize() 方法以返回 BufferedImages 的大小。

仅当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,才会出现滚动条。

You need to override the getPreferredSize() method of your Drawing class to return the size of the BufferedImages.

Scrollbars only appear when the preferred size of the component added to the scroll pane is greater than the size of the scroll pane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文