简体   繁体   English

使用鼠标侦听器Java小程序在屏幕上绘制对象

[英]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: 通过添加以下内容来固定Ball位置的坐标:

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

(Purists would probably go for setX , setY .) (纯粹主义者可能会选择setXsetY 。)

Otherwise, they are defaulted to 0 and 0. (Java default for ints) 否则,它们默认为0和0。(int的Java默认值)

then call: 然后致电:

ball.setLocation(mx, my); 

in your MouseListener . 在您的MouseListener

Also, you probably will want to create more than one Ball decoration... so don't create any until the mouse is clicked. 另外,您可能希望创建多个Ball装饰...因此,在单击鼠标之前不要创建任何装饰。 I shall leave this as an exercise...! 我将把它留作练习...! :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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