Search code examples
javagraphics2dgraphic

How can I use BasicStroke Method while using Java Graphics for an Applet?


This is what I have done so far.

import java.applet.Applet;
import java.awt.*;

public class myFirstAppletRun extends Applet
{public void paint (Graphics page)
{
page.drawLine(100, 0, 100, 600);
page.fillArc(7, 234, 115, 100, -20, 180);
}
}

How would I modify it so that it runs as an applet using Java Graphics2D because i would like to use the BasicStroke method found only in Graphics2D. When I enter BasicStroke method into this current block of code, I get an error. Can someone please modify the code to show me what I would need to do if I would like to make the line and the arc above thicker? Or maybe guide me through the use of casting. (Please note that I have called it Graphics page, and not Graphics g.) Thank you.


Solution

  • Use the setStroke-method:

    Graphics2D g2 = (Graphics2D) page;
    g2.setStroke(new BasicStroke(2));
    

    Result:

    import java.applet.Applet;
    import java.awt.*;
    
    public class myFirstAppletRun extends Applet
    {
        public void paint (Graphics page)
        {
            Graphics2D g2 = (Graphics2D) page;
            g2.setStroke(new BasicStroke(2));
            g2.drawLine(100, 0, 100, 600);
            g2.fillArc(7, 234, 115, 100, -20, 180);
        }
    }