[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


JAVA  fairly simple awt graphics code

Posted on 2005-04-26
Medium Priority
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

aozarov earned 500 total points
ID: 13873109
You should see the Graphics API: http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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