I have this paint program and i'm having a very silly problem. I'm trying to set the paint area to west position using border layout but when i do that it doesn't show up when i run the program. if i set it in the center it shows up. everything works fine except that i need the paint area to be set west because something else needs to go in the center position. i know the answer is staring me in the face but i just cant figure it out. I put a comment in the code where the problem is. hope someone can help. thanks.

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

public class PaintV1
{
    public static void main(String[] args)
    {
        PaintWindowV1 frame = new PaintWindowV1();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setVisible(true);
    }

}

class PaintWindowV1 extends JFrame
{   
    public PaintWindowV1()
    {
        setTitle("Paint it");
        setSize(1000, 600);

        panel = new JPanel();
        drawPad = new PadDrawV1();

        //panel.setPreferredSize(new Dimension(32, 68));

        //Creates a new container
        Container content = this.getContentPane();
        content.setLayout(new BorderLayout()); 


        //sets the panel to the left, PadDrawV1 in the center

        content.add(drawPad, BorderLayout.CENTER); //THE PROBLEM IS HERE! If you change this to BorderLayout.WEST or any position other than center drawpad wont show up.
        content.add(panel, BorderLayout.EAST);

        makeColorButton(Color.BLUE);
        makeColorButton(Color.MAGENTA);
        makeColorButton(Color.RED);
        makeColorButton(Color.GREEN);
        makeColorButton(Color.BLACK);

        JButton clearButton = new JButton("Clear");
        clearButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                drawPad.clear();
            }
        });
        panel.add(clearButton);
    }

    /*
     * makes a button that changes the color
     * @param color the color used for the button
     */
    public void makeColorButton(final Color color)
    {
        JButton tempButton = new JButton();
        tempButton.setBackground(color);
        tempButton.setPreferredSize(new Dimension(16, 16));
        panel.add(tempButton);
        tempButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                drawPad.changeColor(color);
            }
        });
    }

    private JPanel panel;
    private PadDrawV1 drawPad;
}

class PadDrawV1 extends JComponent
{
    //this is gonna be your image that you draw on
    Image image;
    //this is what we'll be using to draw on
    Graphics2D graphics2D;
    //these are gonna hold our mouse coordinates
    int currentX, currentY, oldX, oldY;

    public PadDrawV1()
    {
        setDoubleBuffered(false);
        addMouseListener(new MouseAdapter()
        {
            //if the mouse is pressed it sets the oldX & oldY
            //coordinates as the mouses x & y coordinates
            public void mousePressed(MouseEvent e)
            {
                oldX = e.getX();
                oldY = e.getY();
            }
        });

        addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                currentX = e.getX();
                currentY = e.getY();

                graphics2D.drawLine(oldX, oldY, currentX, currentY);
                repaint();

                oldX = currentX;
                oldY = currentY;
            }
        });
    }

    //this is the painting bit
    //if it has nothing on it then
    //it creates an image the size of the window
    //sets the value of Graphics as the image
    //sets the rendering
    //runs the clear() method
    //then it draws the image
    public void paintComponent(Graphics g)
    {
        if(image == null)
        {
            image = createImage(getSize().width, getSize().height);
            graphics2D = (Graphics2D)image.getGraphics();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            clear();
        }

        g.drawImage(image, 0, 0, null);
    }

    public void clear()
    {
        graphics2D.setPaint(Color.white);
        graphics2D.fillRect(0, 0, getSize().width, getSize().height);
        graphics2D.setPaint(Color.black);
        repaint();
    }

    public void changeColor(Color theColor)
    {
        graphics2D.setPaint(theColor);
        repaint();
    }
} 

Recommended Answers

All 2 Replies

Problem is probably that because you are drawing the panel Java has no idea how big it should be. It's probably showing a preferred size of 0x0. It works in the center position because that gets all the available space after the other components have been positioned.Try setting a preferred size and/or minimum size for your paint panel.
Normally people extend JPanel rather than JComponent for this use - that may make a difference as well(?)

setting a minimum size didn't work but setting a preferred size did. thanks so much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.