Graphics2D在Graphics上绘制我的GUI

时间:2014-01-30 21:27:56

标签: java swing user-interface graphics2d

我正在制作一个痛苦程序,当我试图使g2D alpha友好时遇到了问题。问题是,只要刷新paint方法,它就会依赖用户绘制的内容以及用户悬停的GUI组件。这是pain方法中的代码:

    public void paintComponent(Graphics comp)
{
    Graphics2D board = (Graphics2D) comp;

    AlphaComposite ac=AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.5f);
    Composite oldComp=board.getComposite();
    board.setComposite(ac); //used composite as a suggestion found on stackover flow; 
                                //did not work.

    for(int i = 0; i < al.size(); i++)
    {
        board.setColor(cl.get(i));
        board.setStroke(new BasicStroke(tl.get(i),lineEnd.get(i),juncture.get(i)));
        board.draw((Shape) al.get(i));

    }
    board.setComposite(oldComp);

}

图片的样子:

我感觉绘图板的绝对位置在左下角,因此在更新绘制方法时将其绘制。我不知道如何解决这个问题。如果您需要,这是我设置绘图板的代码:

    public Container(String name, String text, String text2) 
{
    addMouseListener(mouseListener);
    addMouseMotionListener(this);
    setBorder(BorderFactory.createLineBorder(Color.black));

}

并在主JFrame中:

items[0].addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) { 
            JTextField name = new JTextField();
            JTextField width = new JTextField();
            JTextField height = new JTextField();
            final JComponent[] inputs = new JComponent[] { new JLabel("Creating new image: please fill in the required text fields."), new JLabel("Name: "), name, new JLabel("Width: "), width, new JLabel("Height: "), height};
            name.addAncestorListener( new RequestFocusListener() );
            JOptionPane.showMessageDialog(null, inputs, "New Image", JOptionPane.PLAIN_MESSAGE);
            Container cont = new Container(name.getText(), width.getText(), height.getText());
            addContainer(cont);
            cont.setPreferredSize(new Dimension(Integer.parseInt(width.getText()),Integer.parseInt(height.getText())));

            JScrollPane scroll = new JScrollPane();
            JPanel pane = new JPanel();
            pane.add(cont);
            scroll.setViewportView(pane);
            pane.setBackground(Color.lightGray);
            tabbed.addTab(name.getText(), scroll); 
            setCursor("pen");
            } 
        });

感谢您的帮助!

编辑:

以下是数组列表:

static ArrayList<Shape> al = new ArrayList<Shape>();
static ArrayList<Color> cl = new ArrayList<Color>();
static ArrayList<Integer> tl = new ArrayList<Integer>();
static ArrayList<Integer> lineEnd = new ArrayList<Integer>();
static ArrayList<Integer> juncture = new ArrayList<Integer>();

1 个答案:

答案 0 :(得分:2)

基本上,你已经打破了油漆链。

当绘制循环运行时,会调用顶级方法(通常为paint),然后调用一系列事件来执行实际绘制。

链中的每个元素都做一个特定的工作并相互建立,如果不尊重这个链,就会给你带来问题。

通常,窗口的Graphics上下文是共享资源,这意味着在任何给定的绘制周期中,绘制的所有组件都共享相同的Graphics上下文

解决&#34;的初始问题它依据用户绘制的内容以及用户悬停在其上的GUI组件&#34; ,您需要调用super.paintComponent,例如......

public void paintComponent(Graphics comp) {
    super.paintComponent(comp);
    Graphics2D board = (Graphics2D) comp;

这将清除之前绘制的Graphics上下文。

paintComponent应该保留protected,因为没有理由允许其他任何人直接调用它。

请查看Performing Custom PaintingPainting in AWT and Swing了解详情