java paint绘图添加组件不能显示_Java:无法显示paintComponent

我一直在试图开发一个带有箭头的人类简笔画的程序。所以这里的问题是,添加了ImageIcon作为背景后,paintComponent下的图形不会显示。我如何在背景图片上显示绘画。我的编码如下。Java:无法显示paintComponent

public class Drawing

{

public Drawing()

{

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//f.getContentPane().setBackground(new Color(204,229,255));

f.getContentPane().add(new ArrowPanel());

f.setSize(1000,600);

//f.setLocation(200,200);

f.setLayout(new BorderLayout());

f.setContentPane(new JLabel(new ImageIcon("/Users/marian/NetBeansProjects/Drawing/src/drawing/wall.jpg")));

f.setLayout(new FlowLayout());

f.setVisible(true);

}

public static void main(String[] args)

{

new Drawing();

}

}

class ArrowPanel extends JPanel

{

double phi;

int barb;

public ArrowPanel()

{

phi = Math.toRadians(40);

barb = 30;

}

protected void paintComponent(Graphics g)

{

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

Point sw = new Point(w/8, h*7/8);

Point ne = new Point(w*7/8, h/8);

g2.draw(new Line2D.Double(sw, ne));

//drawArrowHead(g2, sw, ne, Color.red);

drawArrowHead(g2, ne, sw, Color.blue);

Ellipse2D.Double head = new Ellipse2D.Double(90,60,20,20);

g2.draw(head);

Line2D.Double body=new Line2D.Double(100,80,100,120);

g2.draw(body);

Line2D.Double arm1=new Line2D.Double(100,100,80,100);

g2.draw(arm1);

Line2D.Double arm2=new Line2D.Double(100,100,120,75);

g2.draw(arm2);

Line2D.Double leg1=new Line2D.Double(100,120,85,135);

g2.draw(leg1);

Line2D.Double leg2=new Line2D.Double(100,120,115,135);

g2.draw(leg2);

}

private void drawArrowHead(Graphics2D g2, Point tip, Point tail, Color color)

{

g2.setPaint(color);

double dy = tip.y - tail.y;

double dx = tip.x - tail.x;

double theta = Math.atan2(dy, dx);

//System.out.println("theta = " + Math.toDegrees(theta));

double x, y, rho = theta + phi;

for(int j = 0; j < 2; j++)

{

x = tip.x - barb * Math.cos(rho);

y = tip.y - barb * Math.sin(rho);

g2.draw(new Line2D.Double(tip.x, tip.y, x, y));

rho = theta - phi;

}

}

}

我还在学习Java,有人可以帮助解决这个问题。谢谢。

2017-05-12

marian

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值