我想在一些点上画一个星星。我的问题是它没有显示我的小星星的线条。我错过了什么?我正在画每个点,画线条,设置颜色,它只是没有显示我的星星。它确实显示了框架,但我认为问题不是框架,而是代码的实际主体。你们建议尝试什么?
public class StarClass
implements Icon {
static JFrame frame;
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setTitle("My Star");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
Graphics2D g2 = (Graphics2D) g;
//points
Point2D.Double pt1
= new Point2D.Double(100, 10);
Point2D.Double pt2
= new Point2D.Double(125, 75);
Point2D.Double pt3
= new Point2D.Double(200, 85);
Point2D.Double pt4
= new Point2D.Double(150, 125);
Point2D.Double pt5
= new Point2D.Double(160, 190);
Point2D.Double pt6
= new Point2D.Double(100, 150);
Point2D.Double pt7
= new Point2D.Double(40, 190);
Point2D.Double pt8
= new Point2D.Double(50, 125);
Point2D.Double pt9
= new Point2D.Double(0, 85);
//lines
Line2D.Double ln1
= new Line2D.Double(pt1, pt2);
Line2D.Double ln2
= new Line2D.Double(pt2, pt3);
Line2D.Double ln3
= new Line2D.Double(pt3, pt4);
Line2D.Double ln4
= new Line2D.Double(pt4, pt5);
Line2D.Double ln5
= new Line2D.Double(pt5, pt6);
Line2D.Double ln6
= new Line2D.Double(pt6, pt7);
Line2D.Double ln7
= new Line2D.Double(pt7, pt8);
Line2D.Double ln8
= new Line2D.Double(pt8, pt9);
//color of lines
g2.setColor(Color.BLUE);
//draw the lines
g2.draw(ln1);
g2.draw(ln2);
g2.draw(ln3);
g2.draw(ln4);
g2.draw(ln5);
g2.draw(ln6);
g2.draw(ln7);
g2.draw(ln8);
}
@Override
public int getIconWidth() {
return 200;
}
@Override
public int getIconHeight() {
return 200;
}
}
2条答案
按热度按时间b1zrtrql1#
您应该找到一个比paintIcon更好的地方来完成这一切。
在您的情况下根本不调用此方法。
这里是你的代码修复。似乎你只是缺少1-2行
从星星开始(这个问题你应该可以自己调试)。
olmpazwi2#