I have a project that ask me to create button,and user enter the radius and location of the circle, then user click button to draw the circle

Johnny

This program is asking the user to input the data of a circle. The following are included: the position of x, position of y, and the width and height of the circle.

So when I test this program, I enter the location and its size; and then I hit the draw button. The circle did not appear.

Here is my code!

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Draw extends JFrame
{
    private JButton draw;
    private JTextField posOfX;
    private JTextField posOfY;
    private JTextField Jwidth;
    private JTextField Jheight;
    private ActionListener listener;
    private JLabel JLx;
    private JPanel drawingPanel;
    private JLabel JLy;
    private JLabel JLwidth;
    private JLabel JLheight;
    private JComponent component;
    public int x =100 ;
    public  int y =100 ;
    public  int width = 100;
    public  int height = 100 ;
    private JPanel panel;

    public Draw()
    {
        listener  = new actionPerform();
        component = new drawCircle();
        panel = new JPanel();
        draw = new JButton ("Draw");
        draw.addActionListener(listener);
        posOfX = new JTextField( 15);
        posOfY = new JTextField(15);
        Jwidth = new JTextField(15);
        Jheight = new JTextField(15);
        JLx = new JLabel("X");
        JLy = new JLabel("Y");
        JLwidth = new JLabel("Width");
        JLheight = new JLabel("Height");
        panel.add(JLx);
        panel.add(posOfX);
        panel.add(JLy);
        panel.add(posOfY);
        panel.add(JLwidth);
        panel.add(Jwidth);
        panel.add(JLheight);
        panel.add(Jheight);
        panel.add(draw);
        panel.add(component);
        add(drawingPanel,BorderLayout.SOUTH);
        add(panel,BorderLayout.NORTH);
    }

    class drawCircle extends JComponent
    {
        public void paintComponent(Graphics g)
        {
            g.drawOval(x,y,width,height);
        }
    }

    class actionPerform implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            try{
            if(e.getSource() == draw)
            {
                width = width + Integer.parseInt(Jwidth.getText());
                height = height + Integer.parseInt(Jheight.getText());
                x = Integer.parseInt(posOfX.getText()) + width;
                y = Integer.parseInt(posOfY.getText())+ height;
                Jwidth.setText("");
                Jheight.setText("");
                posOfX.setText("");
                posOfY.setText("");
            }
            }

            catch (Exception except)
            {
                Jwidth.setText("");
                Jheight.setText("");
                posOfX.setText("");
                posOfY.setText("");
            JOptionPane.showMessageDialog(null,"You should enter numbers only","Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}


import java.awt.*;
import javax.swing.*;


public class DrawViewer
{
    public static void main(String []args)
    {
        Draw d = new Draw();

        d.setVisible(true);
        d.setTitle("Draw circle");
        d.setSize(1000,1000);
        d.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
TNT

When creating a new JComponent object without a layout manager, the width of its preferred size is initially 0 and its height 0, so it's not at all visible. To fix this, your drawCircle class should override the getPreferredSize method so that the component you add it to knows what its size should be. For example:

@Override
public Dimension getPreferredSize() {
    return new Dimension(width, height);
}

It is strongly encouraged that you use a layout manager of some sort, however, so that you won't end up with various problems such as the size of components being wrong.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

App that moves circle button to random location if user clicks near circle

How to draw a circle in custom view on button click

Javascript - Canvas - Create Circle on button click

Trigger a button when the user click in Enter button

Draw circle with a radius in meter

Cannot get a circle on the Leaflet Map when user clicks on the button - Blazor

How do I fill the button background with a circle on click

Draw a semi-circle button iOS

Enable user to draw a circle on Google map

Draw a perfect circle from user's touch

Draw Circle where user clicks with UIBezierPath

draw circle and update radius by seekbar

How do I ask the user for String inputs ? (if they want to enter center and radius, or endpoints of diameter?) (Read an input)

Create Circle button with network image and ripple effect

How to create a Circle input button with ring outside it?

Find circles which the user is in, according to each circle's relative radius

How to build a "Circle" object that lets the user determine the radius?

How to extract only circular ROI portion of the image and show Radius of the circle with a button click in Tkinter window of Python OpenCV GUI

I am trying to create circle avatar in Flutter. I have tried this but not giving me circle please check the code bellow:

I have a button in my Flutter project that I want the phone number to appear when pressed by the user

I want to draw a circle at the middle of the viewed canvas when user clicks Ctrl

my project is virtual piano I need when user click keyboard key the piano button get hover color

How do I create a circle with latitude, longitude and radius with GeoTools?

Custom circle button

Circle button css

HTML circle button with border

centering text on circle button

Metro Circle Button Background

Drawing a circle in a submit button

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive