这个画图的程序在我的机器上显示不出来,面板上显不出所要画的内容,只显示黑面板。但是在我的朋友的机器上运行
一点错误也没有。
后来我又在面板上加了一个按钮(注释部分),按钮和我以前所要画的内容都显示出来了,不知道这是怎么回事。
这是代码部分:
//Map.java
package graphics.Map;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Map extends JFrame{
public Map()
{
super("Map");
setSize(350,400);
MapPane map = new MapPane();
//JButton jb = new JButton("按钮");
Container con = this.getContentPane();
con.add(map);
//con.add(jb,"South");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Map frame = new Map();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
}
class MapPane extends JPanel
{
public void paintComponent(Graphics comp)
{
Graphics2D comp2D = (Graphics2D)comp;
comp2D.drawString("Florida",185,75);
comp2D.drawLine(185,80,222,80);
comp2D.drawRect(2,2,335,320);
comp2D.drawRoundRect(182,61,43,24,10,8);
int x[] ={10,234,253,261,333,326,295,259,205,211,195,191,120,94,81,12,10};
int y[] ={12,15,25,71,209,278,310,274,188,171,174,118,56,68,49,37,12};
int pts = x.length;
Polygon poly = new Polygon(x,y,pts);
comp2D.drawPolygon(poly);
comp2D.fillOval(235,140,15,15);
comp2D.fillOval(225,130,15,15);
comp2D.fillOval(245,130,15,15);
for(int ax = 50;ax<150;ax+=10)
{
for(int ay = 120;ay<320;ay+=10)
{
comp2D.drawArc(ax,ay,10,10,0,-180);
}
}
}
}