• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to display components as well as draw?

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should one display a standard Java component like a button and also draw graphics like draw a line or rectangle or an arc?
Can you use BorderLayout as the Layout manager and do one of the above in the North region and the other in the south region? Can
someone please briefly give a snippet of the code. Thanks
 
deekasha gunwant
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi attire,
i'm posting a piece of code may be that's what u want.
code starts here
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class DrawButton extends JFrame{
JButton jb = new JButton("change shape");
boolean oval = true;
DrawButton()
{
getContentPane().add(jb,"South");
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae)
{
changeFigure();
}
});
}
public static void main(String h[])
{
DrawButton fr = new DrawButton();
fr.setSize(400,400);
fr.setVisible(true);
}
public void changeFigure()
{
repaint();
}
public void paint(Graphics g)
{
if(oval)
{
g.setColor(Color.green);
g.drawString("Oval form", 100,100);
g.drawOval(100,100,20,20);
oval = false;
}
else
{
g.setColor(Color.red);
g.drawString("Rectangle form", 100,100);
g.drawRect(100,100,20,20);
oval = true;
}
}
}
code ends here
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!