Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Drawing Oval

843806Mar 12 2008 — edited Mar 14 2008
hi to all my friends ...

in this code ... the Oval not draw in panel plz any one help me ...thanks
package Paint;

import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;

public class Paint extends JFrame {

    ToolbarPanel toolbarPanel;
    private DrawingPanel DrawPanel;
    private JMenuBar bar;
    private JMenu fileMenu;
    private JMenuItem openItem,  saveItem;

    public Paint() {

        DrawPanel = new DrawingPanel();
        toolbarPanel = new ToolbarPanel(DrawPanel);

        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);

    }

    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();
            }
        });
    }
}
package Paint;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JToolBar;

class ToolbarPanel extends JPanel {

    private JToolBar toolbar;
    private JButton Lbutton,  Obutton,  Sbutton,  Rbutton,  Pbutton,  Tbutton;
    private DrawingPanel panel;

    public ToolbarPanel(DrawingPanel drawingPanel) {

        panel = drawingPanel;
        toolbar = new JToolBar();
        Lbutton = new JButton("Line");
        Lbutton.addActionListener(new ButtonListener());
        Obutton = new JButton("Oval");
        Sbutton = new JButton("Square");
        Rbutton = new JButton("Rectangle");
        Pbutton = new JButton("Polygan");
        Tbutton = new JButton("Triangle");

        toolbar.add(Lbutton);
        toolbar.add(Obutton);
        toolbar.add(Sbutton);
        toolbar.add(Rbutton);
        toolbar.add(Pbutton);
        toolbar.add(Tbutton);
        toolbar.addSeparator();
        add(toolbar);
    }

    private class ButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == Lbutton) {
                panel.draw(DrawingPanel.LINE);
            }
            *if (e.getSource() == Obutton) {*
                *panel.draw(DrawingPanel.OVAL);*  
          }
        }
    }
}
package Paint;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.Serializable;
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;

    public DrawingPanel() {

        vLine = 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();*
                            *}*
                            *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 implements Serializable {

        private int x1,  y1,  x2,  y2;

        public Coordinate(int inx1, int iny1, int inx2, int iny2) {
            x1 = inx1;
            y1 = iny1;
            x2 = inx2;
            y2 = iny2;

        }
    }
}
   
    
            
i wait answer from you .....thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2008
Added on Mar 12 2008
19 comments
314 views