【问题标题】:Paint method java - Rectangle with outline绘制方法 java - 带轮廓的矩形
【发布时间】:2015-10-18 07:35:40
【问题描述】:

我想创建一堵蓝线轮廓和黑色填充的墙。我现在只有一面蓝色的墙,我尝试了几种 Graphics 方法,但都不起作用。

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

【问题讨论】:

  • 您使用的是 Swing 组件吗?如果有,是哪一个?

标签: java graphics paint fill rectangles


【解决方案1】:

使用Graphics#drawRect画出轮廓:-

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

【讨论】:

    【解决方案2】:

    首先,覆盖paintComponent,而不是paint。其次,没有必要像那样重新发明轮子。而是使用现有的 Swing 组件(例如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;
        }
    }
    

    【讨论】:

    • 嗯...如果您想绘制一个简单的矩形,我不建议您使用所有布局管理工具(和问题)创建一个大型 JPanel 对象。而且我不会把绘制一个矩形称为重新发明任何东西。
    【解决方案3】:

    在蓝色的上面再画一个矩形,比蓝色的小,如下图

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

      【讨论】:

      • 我使用 Graphics2D 的原因是我们可以添加新的方面/功能并使用 setStroke 方法来设置轮廓宽度。 swing 中的每个 Graphics 对象都是 Graphics2D 对象,但出于兼容性原因,它的接口被禁用。 setStroke 方法不是完全独立的,需要一个对象来支持它,而不是一个整数。所以我们创建了一个 BasicStroke 对象来确定轮廓宽度大小,方便选择。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 2013-01-11
      • 2013-12-03
      • 2018-02-13
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      相关资源
      最近更新 更多