简体   繁体   中英

Draw object to screen using mouse listener java applet

I am trying to create an applet that draws a christmas tree, then using buttons the user is able to draw decorations to the tree (a different button for each decoration).

I had it so that it would draw a circle on the tree but this circle would disappear when a new one was drawn. It was suggested to me that the decorations need to be defined as objects with variable (eg mouse click coordinates) and then each time the mouse is clicked a new instance is added to an array of objects.

Firstly I'm trying to create the decoration or "ball" object and draw this to the screen, once I've done this I'll work on adding it to an array, so far it draws the ball but in the top left corner and mouse clicks have no effect.

Any help or hints would be greatly appreciated, its starting to do my head in a bit now! Here is the code so far. (I'm aware there is some pointless code it there, its from previous attempts at getting it working.)

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class christmasTree extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
    int[] xPoints = {200,50,350};
    int[] yPoints = {35,400,400};
    Button lights;
    Button decorations;
    Button stars;
    int mx;
    int my;
    Object source;
    ArrayList lightArray;
    Ball ball;



    public void init()
    {
        lights = new Button("Add Lights");
        decorations = new Button("Add Decorations");
        stars = new Button("Add Stars");
        add(lights);
        add(decorations);
        add(stars);
        addMouseListener( this );
        addMouseMotionListener( this );
        lights.addActionListener(this);
        decorations.addActionListener(this);
        lightArray = new ArrayList();
        ball = new Ball();

    }

    public void paint (Graphics g)
    {
        super.paint(g);
        g.setColor(Color.green);
        g.fillPolygon(xPoints, yPoints, 3);
        g.setColor(Color.black);
        g.fillRect(175, 400, 50, 50);
        g.drawString(Integer.toString(mx), 25, 85);
        g.drawString(Integer.toString(my), 25, 100);
        ball.display(g);

    }

    public void actionPerformed(ActionEvent ev)
    {
        if (ev.getSource() == lights){
            source = lights;
        }
        if (ev.getSource() == decorations){
            source = decorations;
        }
        repaint();

    }

    public void mousePressed(MouseEvent e)
    {
        mx = e.getX();
        my = e.getY();
        repaint();


    }

    public void mouseReleased(MouseEvent e)
    {

    }
    public void mouseEntered(MouseEvent e)
    {}
    public void mouseExited(MouseEvent e)
    {}
    public void mouseMoved(MouseEvent e)
    {}
    public void mouseClicked(MouseEvent e)
    {

    }
    public void mouseDragged(MouseEvent e)
    {}



}

class Ball implements MouseListener, MouseMotionListener
{
    int mx1;
    int my1;


    public Ball()
    {


    }

    public void display(Graphics g)
    {
        g.setColor(Color.yellow);
        g.fillOval(mx1, my1, 20, 20);
    }

        public void mousePressed(MouseEvent e)
        {
            mx1 = e.getX();
            my1 = e.getY();



        }

        public void mouseReleased(MouseEvent e)
        {

        }
        public void mouseEntered(MouseEvent e)
        {}
        public void mouseExited(MouseEvent e)
        {}
        public void mouseMoved(MouseEvent e)
        {}
        public void mouseClicked(MouseEvent e)
        {

        }
        public void mouseDragged(MouseEvent e)
    {}
}

Thanks

Fix the coordinates of the Ball location by adding:

public void setLocation(int x, int y) {
   mx1 = x;
   my1 = y;
}

(Purists would probably go for setX , setY .)

Otherwise, they are defaulted to 0 and 0. (Java default for ints)

then call:

ball.setLocation(mx, my); 

in your MouseListener .

Also, you probably will want to create more than one Ball decoration... so don't create any until the mouse is clicked. I shall leave this as an exercise...! :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM