Why is my 'paint' program drawing pixel separated instead of lines? + Info

Hi, I just want to know why my mousedPressed() and mouseMoved() methods are not allowing me to draw a normal line. I noticed that as soon I move the cursor over the effect of rough pixel is present and the downturn, I have to move, more it looks like a line. Also, why isn't the background color of the Panel to be changed whenever I have add the paintComponent() method and what could I do to fix the problem?

Here is the code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class MyPaint extends JPanel
{
    JButton button;
    JFrame frame;
    int firstX;
    int firstY;
    int currentX;
    int currentY;
    int counter = 1;

    public MyPaint()
    {
        createComponents();
        addComponentsToPanels();
        setFrameProperties();
        activateListeners();
    }

    public void addComponentsToPanels()
    {
        this.add(button);
    }

    public void createComponents()
    {
        button = new JButton("Button");
        button.setToolTipText("This is the first button");
    }

    public void setFrameProperties()
    {
        frame = new JFrame();
        frame.setSize(400,400);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
    }

    protected void paintComponent(Graphics g)
    {
        g.drawLine(firstX, firstY, currentX, currentY); // why is this choppy set of pixels instead of a smooth line?
    }

    public void activateListeners()
    {
        MyMouseListener listener = new MyMouseListener();

        // Make a button action listener later

        this.addMouseListener(listener);
        this.addMouseMotionListener(listener);
    }

    private class MyMouseListener implements MouseListener, MouseMotionListener
    {

        public void mouseClicked(MouseEvent e) throws UnsupportedOperationException
        {
//            JOptionPane.showMessageDialog(null,"The mouse was clicked!");
        }

        public void mousePressed(MouseEvent e) throws UnsupportedOperationException
        {
//            JOptionPane.showMessageDialog(null,"The mouse was pressed!");
            firstX = e.getX();
            firstY = e.getY();
        }

        public void mouseReleased(MouseEvent e) throws UnsupportedOperationException
        {
//            JOptionPane.showMessageDialog(null,"The mouse was released!");
        }

        public void mouseEntered(MouseEvent e) throws UnsupportedOperationException
        {
            setBackground(Color.RED); // why isn't this working when I have a paintComponent method? - I want the panel to be red
        }

        public void mouseExited(MouseEvent e) throws UnsupportedOperationException
        {
            setBackground(Color.BLUE); // why isn't this working when I have a paintComponent method? - I want the panel to be blue
        }

        public void mouseDragged(MouseEvent e) throws UnsupportedOperationException
        {
            currentX = e.getX();
            currentY = e.getY();
            repaint();
            firstX = currentX;
            firstY = currentY;
        }

        public void mouseMoved(MouseEvent e) throws UnsupportedOperationException
        {
            System.out.println("mouseMoved: " + counter);
            counter++;
        }
    }

    public static void main(String[] args)
    {
        new MyPaint();
    }
}
Any help would be greatly appreciated!
Thanks in advance!

Move these two lines:

firstX = currentX;
1st = currentY;

just after drawLine in PaintComponent.

Tags: Java

Similar Questions

  • Why ms paint program cannot recognize a twain scanner installed in my system xp network

    I have a network twain scanner installed in my system xp but ms paint program can not recognize well some other programs like acdsee can recognize him
    I have now the reason please help me thanks

    Hello

    I suggest you to refer to this article and check if it helps:

    http://support.Microsoft.com/kb/293614

    It will be useful.

  • Need help with the algorithm for my paint program

    I was doing a paint program with the help of a BufferedImage where the user draws to the BufferedImage and then I pull the BufferedImage class with that I extend JPanel. I want the user to be able to use a function of cancellation via ctrl + z and I also want them to update what type of paper, they write you on direct: a) b) graph paper c) double paper in white. I don't see how using BufferedImages. Is there something I'm missing, or should I do otherwise? I try to avoid the opposite effect because it seems too demanding on the computer, but if this is not feasible, then I guess I have to, but I think that I should ask you guys if what I do is logical or monstrous.

    What I intend to do, is make a LinkedList that has the following 4 parameters:
    (1) previous point
    (2) current point
    (3) current color
    (4) boolean connectPrevious

    This means that program essentially attracts the instant line using two points and the color specified in paintComponent(Graphics g). The Boolean value is ctrl + z (undo) purposes. I also intend to use a background thread to remove entries repeated in LinkedList except for the 25 latest LinkedList components (i.e. number could change in practice).

    What do you think guys?

    Any input would be greatly appreciated!
    Thanks in advance!

    Look at the package javax.swing.undo package - UndoableEdit interface and the UndoManager class.

    Everything to implement the interface. Store all the data necessary to perform your action (pixels, colours, forms etc.). Add all your UndoableEdits in an instance of the UndoManager class and call undo() redo() when you need it.

  • Cannot find the Microsoft Paint program on computer

    The paint program is not anywhere is on the computer at all, even if I'm looking for the painting in the start menu search bar. The only way I can open it is if I go to help and support. Why is this happen and how do I get that back to normal?

    If the paint will open help & support, then there clearly are on the computer.  Maybe somehow managed to delete or move the shortcut from the Start Menu?  I can't imagine how this could be done accidentally, but stranger things have happened.

    In Windows Explorer, browse to C:\WIndows\System32 folder, right-click on the file "MSPaint.exe" and select "create shortcut".  You will say that you cannot create a shortcut to this place and offers a choice of desktop space.  Once the new shortcut has been created, you can move it back to C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories folder, where he usually resides.

  • I need to buy an older basic paint program

    I need to buy the basic paint program which has been in the batch when buying when I bought my HP Pavilion with vista operating system.  I now have a new HP Pavilion with a new basic paint program that does not work well for my needs.  The lining in the new paint program has a system of two points which is annoying.  Make a black line, you will have a white dot at the beginning and the end of your line.  You must then click on your page to get rid of the colon in order to continue the lining in another direction.  I use it all day and am so very frustrated not speak I was wondering when the carpal tunnel syndrome will set with all the clicks!  PLEASE... ANY HELP IS GREATLY APPRECIATED!  Thank you!!

    PS... I HAVE Paintshop Pro 7 and Corel Paint x 2 but they are both too powerful for my needs.

    Hi Rhonda

    Microsoft Paint is a free program which is included in all versions of Windows, it can be purchased separately.

    I understand your problem.

    Explanation: These "white spots" which appear at the end of a drawn line are called "handles" and they allow you to adjust the line after it is drawn. This is useful in case the line was not drawn straight or is to be moved. In the version of Windows 7 paint, there is not a way to remove these points, except to do what you have already described.

    Solution: There are a few users who have managed to replace the Windows 7 version of paint with an earlier version that does not display the handles.

    Note: Replacement of the system with an earlier version files are not supported by Microsoft.

    Keeping this in mind, here's a link to the discussion on this solution.

    Trying to replace Paint in Windows 7 with Windows XP Paint - Microsoft Answers:
    http://answers.Microsoft.com/en-us/Windows/Forum/Windows_7-windows_programs/trying-to-replace-Windows-7-paint-with-Windows-XP/5976f4bf-1485-4E74-9008-bda884eff914

    Let us know if that helps.

    Concerning

  • What microsoft paint program comes with the feature in the program allowing to bend/curve a line?

    I know on XP 2000, the paint program was a lot many features like the one I'm looking for. However the default Windows 7 drawing program doesn't have the option of the curve to modify a line.

    Rgonzalez123, this could help further - http://desktoppub.about.com/od/creategraphics/ss/Draw-a-Heart-Windows-7-Paint_3.htm I think.

  • Is it possible to rotate lower image, to 90o, using the Windows paint program?

    I work on the maps of dish with the paint program in Windows and need to type the text at an angle on the plan.  I need to rotate the photo 90 °.

    I work on the maps of dish with the paint program in Windows and need to type the text at an angle on the plan.  I need to rotate the photo 90 °.

    =================================
    The freeware Paint.NET might be worth a try:

    Paint.NEThttp://www.getpaint.net/

    Paint.NET Rotate / Zoomhttp://www.getpaint.net/doc/latest/en/RotateZoom.html John Inzer - MS - MVP - Digital Media Experience

  • What is a paint program

    my computer is not a "paint" program that asked me to use in order to send a SCREEN CAPTURE of my black square problem. Please notify. Thank you

    Hello netprofits,.

    A program of 'paint' allows you to edit graphics and pictures on your PC. Alternatively, in your case, save screenshots in.

    All Windows operating systems have a paint program. I suggest you try the following:

    1. click the Windows Start button

    2. on the Start Menu, click all programs

    3. in the menu all programs, click Accessories

    4. on the menu accessories look down the list of programs until you see the paint.

    5. click on paint to open the paint program.

    Take you screenshot and then paste the screenshot into paint program.

    This forum post is my own opinion and does not necessarily reflect the opinion or the opinion of Microsoft, its employees or other MVPS.

    John Barnett MVP: Windows XP Expert associated with: Windows Expert - consumer: www.winuser.co.uk |  vistasupport.mvps.org | xphelpandsupport.mvps.org | www.silversurfer-Guide.com

  • Reaching the text option to paint program?

    How I use text using the paint program? Use the program paint - how can I use the text option in tools

    Please see the relevant section of the:

    http://windows.microsoft.com/en-GB/windows-vista/Paint-tools .

    If it does not respond to your question please explain in more detail what is the problem.

  • Why dose my computer programs, drivers and equipment dated November 2, 2006.

    Why dose my computer programs, drivers and equipment dated November 2, 2006. When this computer was not manufactured until 2007?

    Hello

    Because it's my birthday :)

    In fact it is the date of the finalization of the "generation" of these files - they were 'created' this day where
    at least the mechanism that dates of the files has been set for that day here.

    Rob - bicycle - Mark Twain said it is good.

  • missing paint program?

    Under win7 premium 64 bit on the new laptop computer. Can not find the Paint program. Not in the list of program or under the Accessories heading. Have programmed research machine for 'Paint' and Paint.exe "... 'not found '.  A machine since Nov 09. Had problems with IE and had to erase and reset user Accts... think I used Paint on this machine quite awhile, but can't find it now. Where is he?

    Thank you

    If you do not find the program in the start menu > Accessories, you can visit C:\Windows\system32\mspaint.exe. If it isn't there, you can go to the control panel > programs and features and install paint from there. You will probably need your Windows 7 installation media.

    --

    Dustin Harper
    E-mail address is removed from the privacy *.
    http://www.mstechpages.com  Microsoft News, support, FAQ, and much more!

    "pauld430" wrote in message News: 20c53d6a-74fd-4769-82b8-9e0f5b041240...

    Under win7 premium 64 bit on the new laptop computer. Can not find the Paint program. Not in the list of program or under the Accessories heading. Have programmed research machine for 'Paint' and Paint.exe "... 'not found '.  A machine since Nov 09. Had problems with IE and had to erase and reset user Accts... think I used Paint on this machine quite awhile, but can't find it now. Where is he?

    Thank you

    Dustin Harper * address email is removed from the privacy * http://www.mstechpages.com---aide Windows and Support Page

  • How can I measure the length of the lines written with the Paint program?

    I know that an earlier version of the painting software was able to measure the length of the lines written with the Paint program.   I don't know what version it was.   I have the Windows 7 Home Premium Version 6/1 (Build 7601 Service Pack 1, Copywright@2009.  How can I measure the length of the lines that I have the project using paint software?   Do I need to get the previous version?   How can I get?   PLEASE ANSWER!

    Hi Ralph,

    The description of the problem seems a little unclear and I wish I had a better understanding before you start working on it. I would really appreciate if you could answer the following questions:

    1. What is the name of the paint program?

    2 have you tried to install the software of paint in Windows 7?

    3. are you able to install it? If this is not the case, do you have an error code or message?

    4. in what version of Windows was fine paint software work?

    Please provide us with more information on the specific question to help you to fix as soon as possible.

    Your response is very important for us to ensure a proper resolution. Please get back to us with the information above to help you accordingly.

  • Printing problems with the Paint program

    Used the Paint program for years, no problem.  Suddenly, every time I print get a very tiny copy as if it has been reduced by about 75%.  If I try to enlarge before printing, etc., it gives me anywhere between 10 and 20 white sheets before give me a very tiny copy.  I used different printers with the same result.  I love this Paint program and would like to get back to where he was.

    And the program of mystery?

  • I have an invisible background and I want to just use a bursh paint to draw lines thick som to create a cache. the brushes do not seem to paint.

    I have an invisible background and I want to just use a bursh paint to draw lines thick som to create a cache. the brushes do not seem to paint.

    Hi Normannixon2,

    Could you please confirm whether the background is Transparent or white.

    Also when you use the Paintbrush tool, please check the opacity and flow of the Brush tool.

    Kind regards

    Tanuj

  • Why the Setup installation program say the serial number specified for Adobe Creative Suite Design & Web Premium in my account is not valid?

    Why the Setup installation program say the serial number specified for Adobe Creative Suite Design & Web Premium in my account is not valid?

    Please check: error "serial number is not valid for this product". Creative Suite

Maybe you are looking for

  • Satellite P100-160: problems of Vista Outlook 2007 download mail from POP3 server

    I am running Vista on a Satellite P100-160 and you just install Office 2007. Initially, Outlook work very well and then changed to send only messages but not get it back. I tried to uninstall and reinstall but no improvement. I know that the server s

  • HTC Glacier having shown that it is not connected when it is.

    Original title: HTC Glacier I have a HTC Glacier, and the synchronization part tells me that I have not plugged in my device when I plugged it. I can't do anything with my phone on the PC. What can I do?

  • Issue of Chess Titans

    does anyone else have a problem with the Titans call is inspired by games that are obviously earned?

  • Cannot connect my HP 6520 wireless printer

    I have this printer for less then a month, and although the scan is slow and printing is well it worked fine. I have recently moved to the University and I can't connect to the internet. Everytime I try, it says ' unable to connect. Unsupported authe

  • Connection issues with WPA on Windows XP Home SP3 Netbook

    Finished just set up (after the failure of the HD) a Samsnung NC10 netbook. While I am able to get it connected to my i'net connection using WPA2 - PSK, when I re - locate in my house of customers problems start to occur. They have what appears to be