Hi,
I've been having some computer graphics lectures recently where we start off with 2D. I am able to draw certain shapes with the drawLine-method in paint, but I am still unable to have any visual content other than the background color when I draw a few lines. The code below is real simple and I have been trying to figure out what's wrong with it for hours now and I'd really appreciate some feedback.
Problem is
- I can't get the two lines printed
- I don't understand what's wrong with the code
- I'm sleepy
public class generalpathtest extends JApplet{ public static void main (String[]args) { JFrame frame = new JFrame(); frame.setTitle("GeneralPath"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JApplet applet = new generalpathtest(); applet.init(); frame.getContentPane().add(applet); frame.pack(); frame.setVisible(true); } public void init() { JPanel panel = new generalpanel(); getContentPane().add(panel); } } class generalpanel extends JPanel { public generalpanel() { setBackground(Color.white); setPreferredSize(new Dimension(300,400)); } public void paintCompontent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; GeneralPath path = new GeneralPath(GeneralPath.WIND_NON_ZERO); g2.setColor(Color.black); float w = 500, h = 500; float x1 = 0.0f*w; float y1 = 0.0f*h; float x2 = 0.5f*w; float y2 = 0.5f*h; float x3 = 0.2f*w; float y3 = 0.1f*h; path.moveTo(x1, y1); path.lineTo(x2, y2); path.lineTo(x3, y3); path.closePath(); g2.draw(path); g2.translate(120, 120); g2.fill(path); } }
regards
Ole Martin