Task: Write a Java program to implement a simple graphics editor that can be used to draw a
Pythagoras. The editor has a pull-down menu on top of the screen, with 2 buttons: \"Pythagoras\"
and \"Quit\". 1) When the user selects \"Pythagoras\", he/she can draw a tree of Pythagoras on
the screen. 2) The editor terminates/quits execution if the user selects \"Quit\" from the pulldown
menu. 3) The editor has a horizontal and a vertical sliding bar that when slided, moves the tree
on the canvas accordingly.
Solution
HOpe this will help--
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuExp extends JFrame {
public MenuExp() {
setTitle(\"Menu Example\");
setSize(150, 150);
JSlider framesPerSecond = new JSlider(JSlider.VERTICAL,FPS_MIN, FPS_MAX,
FPS_INIT);
framesPerSecond.addChangeListener(this);
JSlider framesPerSecond1= new JSlider(JSlider.HORIZONTAL,FPS_MIN, FPS_MAX,
FPS_INIT);
framesPerSecond1.addChangeListener(this);
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
// Add the menubar to the frame
setJMenuBar(menuBar);
// Define and add two drop down menu to the menubar
JMenu fileMenu = new JMenu(\"Process\");
menuBar.add(fileMenu);
// Create and add simple menu item to one of the drop down menu
JMenuItem newAction = new JMenuItem(\"Pythagoras\");
JMenuItem openAction = new JMenuItem(\"Quit\");
fileMenu.add(newAction);
fileMenu.add(openAction);
newAction.addActionListner(new PythagorusListener());
openAction.addActionListner(new CloseListener());
// Add a listener to the New menu item. actionPerformed() method will
// invoked, if user triggred this menu item
newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(\"You have clicked on the new action\");
}
});
}
public static void main(String[] args) {
MenuExp me = new MenuExp();
me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
me.setVisible(true);
}
}
public class CloseListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//DO SOMETHING
System.exit(0);
}
}
public class PythagorusListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
setSize(900, 900);
setTitle(\"Pythagoras tree\");
add(new Draw(n));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
class Draw extends JComponent {
private int height = 800;
private int width = 800;
private int steps;
public Draw(int n) {
steps = n;
Dimension d = new Dimension(width, height);
setMinimumSize(d);
setPreferredSize(d);
setMaximumSize(d);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
int x1, x2, x3, y1, y2, y3;
int base = width/7;
x1 = (width/2)-(base/2);
x2 = (width/2)+(base/2);
x3 = w.
Salient Features of India constitution especially power and functions
Task Write a Java program to implement a simple graphics editor tha.pdf
1. Task: Write a Java program to implement a simple graphics editor that can be used to draw a
Pythagoras. The editor has a pull-down menu on top of the screen, with 2 buttons: "Pythagoras"
and "Quit". 1) When the user selects "Pythagoras", he/she can draw a tree of Pythagoras on
the screen. 2) The editor terminates/quits execution if the user selects "Quit" from the pulldown
menu. 3) The editor has a horizontal and a vertical sliding bar that when slided, moves the tree
on the canvas accordingly.
Solution
HOpe this will help--
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class MenuExp extends JFrame {
public MenuExp() {
setTitle("Menu Example");
setSize(150, 150);
JSlider framesPerSecond = new JSlider(JSlider.VERTICAL,FPS_MIN, FPS_MAX,
FPS_INIT);
framesPerSecond.addChangeListener(this);
JSlider framesPerSecond1= new JSlider(JSlider.HORIZONTAL,FPS_MIN, FPS_MAX,
FPS_INIT);
framesPerSecond1.addChangeListener(this);
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
2. // Add the menubar to the frame
setJMenuBar(menuBar);
// Define and add two drop down menu to the menubar
JMenu fileMenu = new JMenu("Process");
menuBar.add(fileMenu);
// Create and add simple menu item to one of the drop down menu
JMenuItem newAction = new JMenuItem("Pythagoras");
JMenuItem openAction = new JMenuItem("Quit");
fileMenu.add(newAction);
fileMenu.add(openAction);
newAction.addActionListner(new PythagorusListener());
openAction.addActionListner(new CloseListener());
// Add a listener to the New menu item. actionPerformed() method will
// invoked, if user triggred this menu item
newAction.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("You have clicked on the new action");
}
});
}
public static void main(String[] args) {
MenuExp me = new MenuExp();
me.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
me.setVisible(true);
}
}
public class CloseListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
//DO SOMETHING
3. System.exit(0);
}
}
public class PythagorusListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
setSize(900, 900);
setTitle("Pythagoras tree");
add(new Draw(n));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
class Draw extends JComponent {
private int height = 800;
private int width = 800;
private int steps;
public Draw(int n) {
steps = n;
Dimension d = new Dimension(width, height);
setMinimumSize(d);
setPreferredSize(d);
setMaximumSize(d);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
g.setColor(Color.black);
int x1, x2, x3, y1, y2, y3;
int base = width/7;
x1 = (width/2)-(base/2);
x2 = (width/2)+(base/2);
x3 = width/2;