如何在形状内绘制形状?

时间:2012-07-09 17:23:39

标签: java

我正在尝试构建一个我打算创建一些基本动画的应用程序。我的目标是在顶部设置一个面板,绘制图形,下面的面板带有常规按钮和标签。

我已经完成了下面介绍的代码,但是我无法在之前绘制的矩形内绘制矩形。我相信我的问题是我正在调用一个类(Rectangle)来绘制扩展JPanel的矩形,这意味着每当我尝试绘制一个新的矩形时,新的面板将创建不与前一个面板重叠!

修改

我已经看了一些更多的例子,最后可以想出一个代码来生成形状(这种情况下是矩形),因为我需要它们在同一个面板上。我对代码做了一些更改,下面是新代码。

L.java

import java.awt.Color;
import java.awt.Container;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

class L extends JFrame {

public static final int ww = 1000;
public static final int wh = 600;
public static final String wt = "Teste";
Container pane = getContentPane();
DrawRectangle rectangle = new DrawRectangle();

public L() {
    setSize(ww,wh);
    this.setTitle(wt);
    Sim();
    pane.add(rectangle);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}

public void addRectangle(int px, int py, int pwh, Color pc) {
    this.rectangle.addRectangle( px, py, pwh, pc);
}

public void Sim() {
    addRectangle(100,100,250,Color.red);
    addRectangle(200,200,50,Color.green);
}

public static void main(String[] args) {

    L l = new L();

    try {
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    } catch (UnsupportedLookAndFeelException ex) {
        ex.printStackTrace();
    } catch (IllegalAccessException ex) {
        ex.printStackTrace();
    } catch (InstantiationException ex) {
        ex.printStackTrace();
    } catch (ClassNotFoundException ex) {
        ex.printStackTrace();
    }

    //} 

}

}  

DrawRectangle.java

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JPanel;

class DrawRectangle extends JPanel {
private java.util.List<Rectangle2D> squares;
private java.util.List<Color> colors;
//private int a, startX, startY;
public DrawRectangle(){
    squares = new ArrayList<Rectangle2D>();
    colors = new ArrayList<Color>();
}

public void addRectangle(int px, int py, int pwh, Color pc)  { // square
    squares.add( new Rectangle2D.Double(px, py, pwh, pwh) ) ;
    colors.add(pc);
    //this.a = a;
    //this.startX = startX;
    //this.startY = startY;
}
public void paintComponent(Graphics g) {
    Graphics2D g1 = (Graphics2D) g;
    for( Rectangle2D rect : squares ) {
        System.out.println(colors);
        g1.setColor(colors);
        g1.draw(rect);
    }
}
}

现在出现了一个新问题!我做错了什么我不能使用我传递给方法来绘制不同颜色的形状的颜色?

2 个答案:

答案 0 :(得分:2)

嗯,也许可以尝试创建一个接受Rectangles数组的JPanel,然后像这样绘制那些面板:

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RectangleDrawingTest extends JFrame {

    public RectangleDrawingTest() {
        createAndShowUI();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                RectangleDrawingTest drawingTest = new RectangleDrawingTest();
            }
        });
    }

    private void createAndShowUI() {
        setTitle("Rectangle Drawing Test");
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setLocationRelativeTo(null);
        addComponentsToContentPane(this.getContentPane());
        setVisible(true);
    }

    private void addComponentsToContentPane(Container contentPane) {

        Rectangle[] recs = new Rectangle[3];
        recs[0] = new Rectangle(100, 100, 150, 100);
        recs[1] = new Rectangle(100, 100, 125, 75);
        recs[2] = new Rectangle(100, 100, 100, 50);

        MyPanel mr = new MyPanel(recs);

        contentPane.add(mr);
    }
}

class MyPanel extends JPanel {

    private final Rectangle[] recs;

    public MyPanel(Rectangle[] recs) {
        this.recs = recs;
    }

    @Override
    protected void paintComponent(Graphics grphcs) {
        super.paintComponent(grphcs);
        grphcs.setColor(Color.BLACK);
        for (int i = 0; i < recs.length; i++) {
            grphcs.drawRect(recs[i].x, recs[i].y, recs[i].width, recs[i].height);
        }
    }
}

<强>附录:

因为你同时使用squarescolor ArrayList横向使用这样的for循环并使用递增变量(正方形大小和颜色大小当然相同):

 for( int i=0;i<squares.size();i++) {
       // System.out.println(colors);
        g1.setColor(colors.get(i));
        g1.draw(squares.get(i));
    }

答案 1 :(得分:1)

创建自己的面板,如

class MyPanel extends JPanel{
Rectangulo one;

Rectangulo two;
public MyPanel(){
     one = new Rect...etc.;
     two = new Rect...etc.;
}

public paint(Graphics g){
     one.paint(g);
     two.paint(g);
     //two is overlapping one
}

并将其添加到您的JFrame ...

编辑: ...或者将paint(Graphics g)和repaint()添加到重新绘制panel1的JFrame中...

希望它有所帮助...