JAVA  fairly simple awt graphics code

Posted on 2005-04-26
Last Modified: 2010-03-31
my class is as follows:

class myCanvas extends Canvas
     public void paint(Graphics g)

         (............all my text needs to go here...)


I need to write the text " This works great!  "   at x = 20  and y = 60

and then draw a horizontal line 75 pixels wide at 60 pixels down.  Also,
an oval 45 pixels wide by 30 pixels high and position it at x = 40 and y = 80.

   A button (named quitButton with text "quit") needs to be declared  and placed
onto the screen at location "East", the listener also needs to be added to the button.
Question by:usumle
    1 Comment
    LVL 15

    Accepted Solution

    You should see the Graphics API:

    g.drawString(" This works great! ", 20, 60)
    g.drawLine(0, 60, 75, 60) // draw 75 pixels at line 60
    g.drawOval(40, 80, 45, 30)

    >> A button (named quitButton with text "quit") needs to be declared  and placed
    >> onto the screen at location "East", the listener also needs to be added to the button
    You need to declare that button on the parent of the canvas (Panel, Frame, ..)
    something like this:
    Button button = new Button("quit");

    Make sure your canvas container (parent) has a BorderLayout (setLayout(new BorderLayout()))

    You can add the button to the container like this:
    add(button, BorderLayout.EAST);

    You can add an action listener to your button this way:
    button.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e)
    // Do you action here like System.exit(0);


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    dividesSelf challange 15 58
    IT Company 5 55
    Books that can get me started on JAVA 2 33
    word0 challenge 3 30
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now