Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:09:28+00:00 2026-05-26T04:09:28+00:00

So I have 4 classes, in my paint program and I am trying to

  • 0

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

    }


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:09:28+00:00Added an answer on May 26, 2026 at 4:09 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that is essentially like a paint application. However, my program
I'm trying to add plugins to my program, and this looks good, except that
I have a Java program that loads thirdparty class files (classes I did not
I have been working on a program that has 3 classes of which 2
I'm trying to make a program that check the ISBN number and sees if
I have 5 classes that are part of a point of sales program: Customer,
I have classes that store data, and methods to go get data for individual
I have classes that are named exactly the same across different plug-ins that I
I have classes that are needed in both my web service and my server.
i'm trying to create a bridge program in twisted.web that receives data from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.