用JAVA画一个大区,如一个大的矩形。但这个矩形中间有几块是被挖了的不画的,这个时候不知该如何画出这个区出来。例如:
int[] px = {100,500,500,100,100,150,250,250,150,150,300,400,400,300,300,150,250,250,150,150};
int[] py = {100,100,500,500,100,150,150,250,250,150,250,250,350,350,250,300,300,400,400,300};
g.setColor(Color.red);
Polygon pg = new Polygon(px,py,px.length);
g.fillPolygon(pg);
我是想画一个去掉了中间三个小矩形的一个大矩形,可现在画出来区的关系都乱了,不知该如何处理了。
int[] px = {100,500,500,100,100,150,250,250,150,150,300,400,400,300,300,150,250,250,150,150};
int[] py = {100,100,500,500,100,150,150,250,250,150,250,250,350,350,250,300,300,400,400,300};
g.setColor(Color.red);
Polygon pg = new Polygon(px,py,px.length);
g.fillPolygon(pg);
我是想画一个去掉了中间三个小矩形的一个大矩形,可现在画出来区的关系都乱了,不知该如何处理了。
import java.awt.geom.GeneralPath;import javax.swing.JFrame;
import javax.swing.JPanel;public class Test
{
public static void main(String[] args)
{
JPanel p = new JPanel()
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g); int[] px = {100, 500, 500, 100};
int[] py = {100, 100, 500, 500}; Polygon pg = new Polygon(px, py, px.length);
GeneralPath generalPath = new GeneralPath(pg);
px = new int []{150, 350, 350, 150};
py = new int []{150, 150, 250, 250};
pg = new Polygon(px, py, px.length);
generalPath.append(pg, false); px = new int []{300, 400, 400, 300};
py = new int []{250, 250, 350, 350};
pg = new Polygon(px, py, px.length);
generalPath.append(pg, false); px = new int []{250, 250, 150, 150};
py = new int []{300, 400, 400, 300};
pg = new Polygon(px, py, px.length);
generalPath.append(pg, false); Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
g2d.fill(generalPath);
}
};
JFrame f = new JFrame();
f.getContentPane().add(p, BorderLayout.CENTER);
f.setSize(600, 600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}