使用JComponent绘制唯一的矩形

时间:2013-05-09 03:21:46

标签: java graphics awt java-2d

如何在椭圆内绘制唯一的矩形(立方体):

http://i.stack.imgur.com/pmHTl.jpg

enter image description here

对我来说,使用图形绘制如图所示的矩形是很棘手的。关于该怎么做的任何建议。

确定。我会努力使自己尽可能清楚。到目前为止,我所拥有的是橙色椭圆形和背后的纤细灰色椭圆形。我只需要创建其中一个" dot"在一个班级,我将制作他们的许多对象。我需要帮助的任务是绘制"矩形"您可以看到使用J Component在橙色点中的形状。根据要求,如果此编辑无法满足您理解我的问题的需要,我将添加到目前为止的图片。

由于

编辑:如果你感兴趣的话,这是我创建椭圆的代码 -

public void paint(Graphics g)  {
    Color c = (Color.orange);
    g.setColor(Color.gray);
    g.fillOval(3,3,60,60);
    g.setColor(c);
   g.fillOval(0,0,60,60);
  } 

编辑:我在SSCCE的尝试 - > NanoBot类(我在油漆中创建机器人的地方)

/**
 * @author (Omar Ahmed) 
 */
 import javax.swing.*;
 import java.awt.*;
 public class NanoBot extends Image
 {


public NanoBot(int x, int y, int w, int h)
{
    super(x,y,w,h);
}

public void paint(Graphics g)  {
    Color c = (Color.orange);
    g.setColor(Color.gray);
    g.fillOval(3,3,60,60);
    g.setColor(c);
    g.fillOval(0,0,60,60);
    //g.setColor(Color.black);
    //g.fillOval(10,20,10,10);
    //g.fillOval(40,20,10,10);

} 
}

和司机:

/** Bot Swarm
 *  Date: May, 2013
 *  Author: Omar Ahmed
 */
import java.awt.*;
import javax.swing.*;
public class Driver  { 
    private JFrame win;
    private NanoBot bot1;
        public Driver()   {
        win = new JFrame(" Swarm ");
        win.setLayout(null);
        win.setVisible(true);
        win.setBounds( 20, 20, 800, 700);
        win.getContentPane().setBackground(Color.white);
        bot1=new NanoBot(50,50,70,70);
        win.add(bot1,0);

}

Hope This Helps

1 个答案:

答案 0 :(得分:2)

你的第一步是打破你的要求......

enter image description here

你需要绘制3个形状,正面,顶部,侧面。

前方的y位置偏移总高度的0.412。它的宽度是整个宽度的0.77。

顶部高度为整体高度的0.412,并且其整体宽度的水平插入为0.2 ......

边的x位置偏移了整个宽度的0.77,并且整个宽度的插入点为0.47。

这一点非常重要,因为您希望确保形状可以合理地调整大小......

现在,您可以简单地使用Graphics#drawLineGraphics#drawRectangle来构建形状,但对我来说,这是一项很多工作......

相反,2D Graphics非常强大并且包含许多精彩的东西,今天感兴趣的是Shape API,它允许您定义许多不同的形状,可以绘制或填充。< / p>

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestChip {

    public static void main(String[] args) {
        new TestChip();
    }

    public TestChip() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();

            int width = 44;
            int height = 17;
            Front front = new Front(width, height);
            Top top = new Top(width, height);
            Side side = new Side(width, height);

            draw(g2d, front, Color.BLACK, Color.YELLOW);
            draw(g2d, top, Color.BLACK, Color.GRAY);
            draw(g2d, side, Color.BLACK, Color.DARK_GRAY);

            g2d.dispose();
        }

        protected void draw(Graphics2D g2d, Shape shape, Color foreground, Color background) {
            g2d.setColor(background);
            g2d.fill(shape);
            g2d.setColor(foreground);
            g2d.draw(shape);
        }
    }

    public class Front extends Path2D.Float {

        public Front(float width, float height) {
            float frontWidth = width * 0.77f;
            float yOffset = height * 0.412f;

            moveTo(0, yOffset);
            lineTo(frontWidth, yOffset);
            lineTo(frontWidth, height);
            lineTo(0, height);

            closePath();
        }
    }

    public class Side extends Path2D.Float {

        public Side(float width, float height) {
            float xOffset = width * 0.77f;
            float inset = height * 0.47f;
            moveTo(xOffset, inset);
            lineTo(width, 0);
            lineTo(width, inset);
            lineTo(xOffset, height);
            closePath();
        }
    }

    public class Top extends Path2D.Float {

        public Top(float width, float height) {
            float inset = width * 0.2f;
            float shapeHeight = height * 0.412f;
            moveTo(inset, 0);
            lineTo(width, 0);
            lineTo(width - inset, shapeHeight);
            lineTo(0, shapeHeight);
            closePath();
        }
    }
}

你现在的工作就是消失,研究这个例子,研究引用的教程,研究相关的API文档,并弄清楚如何在你的圆圈中对齐上面的形状并画出它的腿......

提示。制作一个知道如何渲染所有这些的“Bug”类,并根据需要简单地翻译Graphics的位置......

相关问题