Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
绘制方法java-带轮廓的矩形_Java_Graphics_Paint_Fill_Rectangles - Fatal编程技术网

绘制方法java-带轮廓的矩形

绘制方法java-带轮廓的矩形,java,graphics,paint,fill,rectangles,Java,Graphics,Paint,Fill,Rectangles,我想创建一个带有蓝色线条轮廓和黑色填充的墙。我现在只有一个蓝色的墙,我尝试了一些图形方法,但并没有奏效 public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(x, y, size, size); } 只需在蓝色的上面画一个比蓝色的小的矩形,如下图所示 public void paint(Graphics g) { g.setColor(Color.blue); g.fillRect(

我想创建一个带有蓝色线条轮廓和黑色填充的墙。我现在只有一个蓝色的墙,我尝试了一些图形方法,但并没有奏效

public void paint(Graphics g) {
    g.setColor(Color.blue);
    g.fillRect(x, y, size, size);
}

只需在蓝色的上面画一个比蓝色的小的矩形,如下图所示

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);
}
用于绘制轮廓:-

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

首先,覆盖
paintComponent
,而不是
paint
。第二,没有必要像那样重新发明轮子。相反,请使用现有的Swing组件(例如
JPanel


您使用的是Swing组件吗?如果是,是哪一个?我使用Graphics2D的原因是,我们可以添加新的方面/功能,并使用setStroke方法设置轮廓宽度。swing中的每个图形对象都是Graphics2D对象,但出于兼容性原因,它的接口被禁用。setStroke方法不是完全独立的,需要一个对象来支持它,而不是一个整数。因此,我们创建了一个BasicStroke对象来确定轮廓宽度大小,并且易于选择。。。如果你想画一个简单的矩形,我建议不要创建一个包含所有布局管理工具(和问题)的大型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;
    }
}
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);

    }
}