tags:

views:

35

answers:

4

I'm developing an simple art program in Java and I was curious if it was possible to create an gray empty space around the canvas like is most art programs(Meaning, an empty space that isn't locked down and is scrollable). Is this possible and how could I go about doing this?

Example: alt text

+1  A: 

Perhaps add the canvas into a JScrollPane?

zigdon
What I want, though need to figure out how to implement it. Guess I could mix Starkey's example with a JScrollPane. :\
Unrealomega
+1  A: 

There are several ways to do this. The easiest that comes to my head is to create a JPanel and then put the Canvas in the JPanel. Set the insets in the Canvas to the size of your gray space. Set the background of the JPanel to Gray. Something like this:

Canvas canvas = new Canvas();
JPanel panel = new JPanel();
panel.setBackground(Color.GRAY);
GridBagLayout layout = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints();
gc.fill = BOTH;
gc.weightx = 1.0;
gc.weighty = 1.0;
gc.insets = new Insets(30, 30, 30, 30);  // Your gray space
layout.setConstraints(canvas, gc);
panel.add(canvas);

panel.setLayout(layout);
Starkey
Only problem with this example, without modifying much, is that the window resizes the canva. Otherwise, it's still a good example. :3
Unrealomega
+2  A: 

How to Use Borders. Create a panel with an EmptyBorder and then add a second JPanel to the panel.

Don't mix AWT components in a Swing application. Canvas is an AWT component. Just use a JPanel, you still have access to all the Graphics methods.

Edit:

Quit calling your JPanel a Canvas. A Canvas is an AWT component and it works differently than a JPanel.

If you don't want your JPanel to resize then give it a preferred size and add it to a JPanel using a FlowLayout. You can control the horizontal/vertical gaps of a FlowLayout to give the appearance of a Border.

Post your SSCCE (http://sscce.org) showing what you have tried and the problems you have encountered rather than us posting all the code and guessing what your problems are.

camickr
I'm using a JPanel already, just trying to figure out how to put a border around it. Let me see if this works. :P
Unrealomega
+2  A: 

Have you considered using a Line Border within the JPanel.

import javax.swing.*;
import java.awt.*;
class Testing
{
  public void buildGUI()
  {
    JFrame f = new JFrame();

    JPanel o_panel = new JPanel();
    JPanel panel = new JPanel();
    panel.setMinimumSize(new Dimension(500,500));
    panel.setPreferredSize(new Dimension(500,500));
    panel.setMaximumSize(new Dimension(500,500));
    panel.setBackground(Color.RED);
    panel.setBorder(BorderFactory.createLineBorder(Color.GRAY,25));

    o_panel.add(panel);

    f.getContentPane().add(o_panel);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
  public static void main(String[] args)
  {
    SwingUtilities.invokeLater(new Runnable(){
      public void run(){
        new Testing().buildGUI();
      }
    });
  }
}

alt text

Eric W
Seems to work similar to Starkey's example, but it still has the flaw of resizing the canvas when the window is resized. Is there a way to prevent the Canvas(Which is just a JPanel) from resizing with the window, as I'd like to allow the user to resize?
Unrealomega
The best way I can see doing this is without the border, but rather putting it within another panel instead, as it seems to scale the canvas if you use a border. Anyway, thanks guys.
Unrealomega