Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to display components as well as draw?

 
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
 
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
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic