• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

JAVA fairly simple awt graphics code

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.
0
usumle
Asked:
usumle
1 Solution
 
aozarovCommented:
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);
}
});

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now