Paint method java - Rectangle with outline

21,476

Solution 1

First, override paintComponent, not paint. Second, there's no need to re-invent the wheel like that. Instead, use an existing Swing component (e.g. JPanel),

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Main 
{
    public static void main(String[] args) 
    {        
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(getWallComponent());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private static JPanel getWallComponent()
    {
        JPanel panel = new JPanel();

        panel.setBackground(Color.black);
        panel.setBorder(BorderFactory.createLineBorder(Color.blue, 5));
        panel.setPreferredSize(new Dimension(200, 200)); // for demo purposes only

        return panel;
    }
}

enter image description here

Solution 2

Use Graphics#drawRect to draw the outline: -

g.setColor(Color.black);
g.fillRect(x, y, size, size);
g.setColor(Color.blue);
g.drawRect(x, y, size, size);

Solution 3

Just paint another rectangle over the blue one, smaller than the blue one, like below

public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.fillRect(x, y, size, size);
    g.setColor(Color.black);
    g.fillRect(x-width/2,y-width/x,size-width,size-with);
}
Share:
21,476
redundant6939
Author by

redundant6939

Updated on October 18, 2020

Comments

  • redundant6939
    redundant6939 over 3 years

    I want to create a wall with a blue line outline and black filling. I have only a blue wall now and I tried a couple of the Graphics methods but wasn't working.

    public void paint(Graphics g) {
        g.setColor(Color.blue);
        g.fillRect(x, y, size, size);
    }
    
  • MavericksFoundation
    MavericksFoundation over 8 years
    The reason I am using Graphics2D is so we can add new aspects/features and use the setStroke method to set the outline width. Every Graphics object in swing is a Graphics2D object, but it's interface is disabled for compatibility reasons. The setStroke method is not fully independent and needs an object to support it, instead of an integer. So we create a BasicStroke object to determine the outline width size with easy selection.
  • Franz D.
    Franz D. almost 6 years
    Hmmm... creating a massive JPanel object with all the layout management facilities (and issues) is not what I'd recomment if you want to draw a simple rectangle. And I wouldn't call drawing a rectangle re-inventing anything.