Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 680287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:21:20+00:00 2026-05-14T01:21:20+00:00

package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class

  • 0
package test;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class test_bmp extends JPanel implements MouseListener,MouseMotionListener,ActionListener
{
 static BufferedImage image;
 Color color;
 Point start=new Point();
 Point end =new Point();
 JButton elipse=new JButton("Elipse");
 JButton rectangle=new JButton("Rectangle");
 JButton line=new JButton("Line");
 String selected;
 public test_bmp()
    {
  color = Color.black; 
  setBorder(BorderFactory.createLineBorder(Color.black));   
  addMouseListener(this);
  addMouseMotionListener(this);
    }
 public void paintComponent(Graphics g) 
 {
  //super.paintComponent(g);
  g.drawImage(image, 0, 0, this);
  Graphics2D g2 = (Graphics2D)g;
  g2.setPaint(Color.black);
  if(selected=="elipse")
        {
         g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
         System.out.println("Start : "+start.x+","+start.y);
         System.out.println("End   : "+end.x+","+end.y);
        }
        if(selected=="line")
         g2.drawLine(start.x,start.y,end.x,end.y);
 }
 //Draw on Buffered image
 public void draw()
    {
        Graphics2D g2 = image.createGraphics();
        g2.setPaint(color);
      System.out.println("draw");
        if(selected=="line")
         g2.drawLine(start.x, start.y, end.x, end.y);
        if(selected=="elipse")
        {
         g2.drawOval(start.x, start.y, (end.x-start.x),(end.y-start.y));
            System.out.println("Start : "+start.x+","+start.y);
         System.out.println("End   : "+end.x+","+end.y);
        }
        repaint();
        g2.dispose();
        }  
 public JPanel addButtons()
 {
  JPanel buttonpanel=new JPanel();
  buttonpanel.setBackground(color.lightGray);
  buttonpanel.setLayout(new BoxLayout(buttonpanel,BoxLayout.Y_AXIS));
  elipse.addActionListener(this);
  rectangle.addActionListener(this);
  line.addActionListener(this);
  buttonpanel.add(elipse);
  buttonpanel.add(Box.createRigidArea(new Dimension(15,15)));
  buttonpanel.add(rectangle);
  buttonpanel.add(Box.createRigidArea(new Dimension(15,15)));
  buttonpanel.add(line);
  return buttonpanel;
 }
 public static void main(String args[]) 
 {
   test_bmp application=new test_bmp();
   //Main window
   JFrame frame=new JFrame("Whiteboard");   
   frame.setLayout(new BorderLayout());
   frame.add(application.addButtons(),BorderLayout.WEST);
   frame.add(application);
   //size of the window
   frame.setSize(600,400);
   frame.setLocation(0,0);
   frame.setVisible(true);
   int w = frame.getWidth();
      int h = frame.getHeight();
      image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      Graphics2D g2 = image.createGraphics();
      g2.setPaint(Color.white);
      g2.fillRect(0,0,w,h);
      g2.dispose();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }
 @Override
 public void mouseClicked(MouseEvent arg0) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mouseEntered(MouseEvent arg0) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mouseExited(MouseEvent arg0) {
  // TODO Auto-generated method stub
 }
 @Override
 public void mousePressed(MouseEvent event) 
 {
  start = event.getPoint();
 }
 @Override
 public void mouseReleased(MouseEvent event) 
 {
  end = event.getPoint();
  draw();
 }
 @Override
 public void mouseDragged(MouseEvent e) 
 {
  end=e.getPoint();
  repaint();
 }
 @Override
 public void mouseMoved(MouseEvent arg0) {
  // TODO Auto-generated method stub

 }
 @Override
 public void actionPerformed(ActionEvent e) 
 {
  if(e.getSource()==elipse)
   selected="elipse";
  if(e.getSource()==line)
   selected="line";
  draw();

 }
}

I need to create a paint application. When I draw ellipse by dragging mouse from left to right it displays nothing. Why? Should I use any other function here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:21:20+00:00Added an answer on May 14, 2026 at 1:21 am

    Your program does draw an ellipse when you drag the mouse down and to the right. It’s dragging up and/or left that does not work, because Graphics.drawOval does not work with a negative width or height.

    Try adding a method like this:

    private Shape createEllipse() {
        Ellipse2D e = new Ellipse2D.Double();
        e.setFrameFromDiagonal(start, end);
        return e;
    }
    

    Then call it from draw and paintComponent like this:

    if(selected=="elipse") {
        g2.draw(createEllipse());
    }
    

    Also you probably do not need the call to draw() at the end of actionPerformed. If you switch between line and ellipse mode it will draw an ellipse with the same coordinates as the most recent line or vice-versa.

    And one coding style issue: Using string literals for selected is confusing (although it does work.) I would define an enum instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://developer.android.com/guide/topics/fundamentals.html#lcycles is a description of the Life Cycle of an… May 15, 2026 at 3:08 pm
  • Editorial Team
    Editorial Team added an answer HTTPService is for fetching a url and parsing the retrieved… May 15, 2026 at 3:08 pm
  • Editorial Team
    Editorial Team added an answer This is what I do: Using m2eclipse plugin for Maven… May 15, 2026 at 3:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.