Paint method java - Rectangle with outline

半腔热情 提交于 2020-01-02 10:03:46

问题


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

回答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;
    }
}




回答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);



回答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);
}



回答4:


package painting;

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

public class RectangleOutline extends JPanel {

    int x = 100;
    int y = 200;

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        outline(g);
    }

    public void outline(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(255, 0, 0));
        g2.fillRect(x, y, 30, 30);

        g2.setStroke(new BasicStroke(5));
        g2.setColor(new Color(0, 0, 0));
        g2.drawRect(x, y, 30, 30);
    }

    public static void main(String[] args){
        JFrame f = new JFrame();
        RectangleOutline graphics = new RectangleOutline();
        f.add(graphics);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        f.setSize(400, 400);

    }
}


来源:https://stackoverflow.com/questions/13629192/paint-method-java-rectangle-with-outline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!