<!-- jbb_cong_country = "EG"; -->
Painter code
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Paint extends JFrame {
ToolbarPanel toolbarPanel;
private DrawingPanel DrawPanel;
private JMenuBar bar;
private JMenu fileMenu;
private JMenuItem openItem, saveItem;
private JPanel panel;
public Paint() {
DrawPanel = new DrawingPanel();
toolbarPanel = new ToolbarPanel(DrawPanel);
panel=new JPanel();
fileMenu = new JMenu("file");
fileMenu.addSeparator();
openItem = new JMenuItem("open");
fileMenu.add(openItem);
saveItem = new JMenu("save");
fileMenu.add(saveItem);
bar = new JMenuBar();
setJMenuBar(bar);
bar.add(fileMenu);
add(toolbarPanel, BorderLayout.NORTH);
DrawPanel.setBackground(Color.WHITE);
add(DrawPanel, BorderLayout.CENTER);
panel.setBackground(Color.GRAY);
panel.setPreferredSize(new Dimension(200,300));
add(panel,BorderLayout.EAST);
}
private static void creatAndShowGUI() {
JFrame frame = new Paint();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
creatAndShowGUI();
}
});
}
}
****************************
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.TitledBorder;
class ToolbarPanel extends JPanel {
private JToolBar toolbar;
private String[] labels = {"Line", "Oval", "Square", "Rectangle", "Polygan", "Triangle"};
private JButton button;
private DrawingPanel panel;
public ToolbarPanel(DrawingPanel panel) {
this.panel = panel;
toolbar = new JToolBar();
for (int i = 0; i < labels.length;++i) {
button = new JButton(labels[i]);
toolbar.add(button);
toolbar.add(button);
button.addActionListener(new ButtonListener());
add(toolbar);
}
for(int i=0;i<labels.length;++i){
button.addActionListener(new ButtonListener());
}
TitledBorder title;
title = new TitledBorder("Shapes");
title = BorderFactory.createTitledBorder(title);
setBorder(title);
}
private class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == labels[0]) {
panel.draw(DrawingPanel.LINE);
}
}
}
}
*****************************************
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JPanel;
public class DrawingPanel extends JPanel {
private int x1, y1, x2, y2, width_I, width_F, Height_I, Height_F, Oval_w, Oval_h, Oval_x, Oval_y;
public final static int LINE = 1, OVAL = 2;
private int shape;
private Vector vLine, vOval;
public DrawingPanel() {
vLine = new Vector();
vOval = new Vector();
addMouseListener(
new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if (shape == LINE) {
x1 = e.getX();
y1 = e.getY();
}
if (shape == OVAL) {
width_I = e.getX();
Height_I = e.getY();
Oval_x = e.getX();
Oval_y = e.getY();
}
}
@Override
public void mouseReleased(MouseEvent e) {
x2 = e.getX();
y2 = e.getY();
if (shape == LINE) {
vLine.add(new Coordinate(x1, y1, x2, y2));
}
repaint();
if (shape == OVAL) {
width_F = e.getX();
Height_F = e.getY();
/* if (width_F < width_I) {
Oval_x = e.getX();
}
if (Height_F < Height_I) {
Oval_y = e.getY();
}*/
/* vOval.add(new Coordinate(width_I,Height_I,width_F,Height_F));*/
repaint();
}
}
});
Dimension dimension = new Dimension(500, 300);
setMinimumSize(dimension);
setPreferredSize(dimension);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < vLine.size(); ++i) {
Coordinate coordinate = (DrawingPanel.Coordinate) vLine.elementAt(i);
g.drawLine(coordinate.x1, coordinate.y1, coordinate.x2, coordinate.y2);
}
Oval_w = Math.abs(width_F - width_I);
Oval_h = Math.abs(Height_F - Height_I);
g.drawOval(Oval_x, Oval_y, Oval_w, Oval_h);
}
public void draw(int shapeTodraw) {
shape = shapeTodraw;
repaint();
}
private class Coordinate {
private int x1, y1, x2, y2;
public Coordinate(int inx1, int iny1, int inx2, int iny2) {
x1 = inx1;
y1 = iny1;
x2 = inx2;
y2 = iny2;
}
}
}
please in the this code why the the action not affect in the button and draw line in the panel ....
plz help me ...
thanks in advance
beshoy




