如何将Ellipse2D和Rectangle表示/使用为一个形状?

时间:2016-02-02 00:44:53

标签: java swing awt java-2d

我想画一个简单的棒图。这需要一个圆圈和一些矩形。我可以用许多g2d.fill()调用分别绘制头部,躯干等。但是,如果可能的话,我希望能够将其表示为一个形状,并通过一次填充调用绘制它。此外,如果我想将棒图向右移动10个像素,我当前必须更改椭圆+ 10上的x坐标和矩形上的x坐标+ 10,依此类推。我希望能够更改单个x坐标,然后将所有单独的组件移出。

我怎样才能做到这一点?

代码:

public class StickFigure{
    Ellipse2D head = new Ellipse2D.Double(0, 0, 100, 100);
    Rectangle torso = new Rectangle(100, 100, 50, 110); 
    Rectangle rightArm;
    ...
}

我必须做什么:

g2d.fill(sf.head);
g2d.fill(sf.torso);
...

我想做什么:

g2d.fill(sf.figure) //figure being a shape which includes both head and torso

我必须做什么:

     sf.head.x = 3;
     sf.head.y = 4;
     sf.torso.x = 3;
     sf.torso.y = 4;
     g2d.fill(sf.head);
     g2d.fill(sf.torso);
     ...

我想做什么:

sf.figure.x = 3; //which shifts both the head and the torso
sf.figure.y = 4; // ^^
g2d.fill(sf.figure);

2 个答案:

答案 0 :(得分:2)

您可以创建一个BufferedImage来表示您的简笔画。然后将矩形和省略号绘制到BufferedImage上。

library(data.table)
res1 <- setDT(exampledf)[, c(tstrsplit(Col1[-1], 
        '/'),Country = Col1[1L]), .(i2=cumsum(i1))][,i2:= NULL][]
setnames(res1, 1:2, c('year', 'month'))

BufferedImage将是您在构造类时创建的实例变量。然后你可以在paintComponent()方法中绘制BufferedImage。

 exampledf<-data.frame(Col1=c("Argentina","2005/12","2005/11",
          "Bolivia","2006/12"),stringsAsFactors=FALSE)

然后,只要你想移动图像,你就可以改变drawImage(...)方法中的x / y值。

答案 1 :(得分:2)

The different shapes that make up a stick figure can be turned into a single Area.

screenshot

class StickFigure {

    private Area stickFigure;
    private Color color;

    StickFigure(Color color, int x) {
        this.color = color;
        stickFigure = new Area(new Ellipse2D.Double(x, 0, 100, 100));
        stickFigure.add(new Area(new Rectangle(x+25, 100, 50, 110)));
    }

    public Area getStickFigure() {
        return stickFigure;
    }

    public void draw(Graphics2D g) {
        g.setColor(color);
        g.fill(stickFigure);
    }
}

MCVE

import java.awt.*;
import java.awt.geom.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class StickFigurePainter {

    private JComponent ui = null;

    StickFigurePainter() {
        initUI();
    }

    public void initUI() {
        if (ui != null) {
            return;
        }

        ui = new JPanel(new BorderLayout(4, 4));
        ui.setBorder(new EmptyBorder(4, 4, 4, 4));

        ArrayList<StickFigure> list = new ArrayList<StickFigure>();
        list.add(new StickFigure(Color.RED, 0));
        list.add(new StickFigure(Color.GREEN, 110));
        list.add(new StickFigure(Color.BLUE, 220));

        ui.add(new StickCanvas(list));
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                StickFigurePainter o = new StickFigurePainter();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

class StickCanvas extends JPanel {

    private ArrayList<StickFigure> stickFigures;

    StickCanvas(ArrayList<StickFigure> stickFigures) {
        this.stickFigures = stickFigures;
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        for (StickFigure stickFigure : stickFigures) {
            stickFigure.draw(g2);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        Area area = new Area();
        for (StickFigure stickFigure : stickFigures) {
            area.add(stickFigure.getStickFigure());
        }
        return area.getBounds().getSize();
    }
}

class StickFigure {

    private Area stickFigure;
    private Color color;

    StickFigure(Color color, int x) {
        this.color = color;
        stickFigure = new Area(new Ellipse2D.Double(x, 0, 100, 100));
        stickFigure.add(new Area(new Rectangle(x+25, 100, 50, 110)));
    }

    public Area getStickFigure() {
        return stickFigure;
    }

    public void draw(Graphics2D g) {
        g.setColor(color);
        g.fill(stickFigure);
    }
}