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 8414585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:12:15+00:00 2026-06-10T01:12:15+00:00

I am trying to create an applet that draws a christmas tree, then using

  • 0

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 (e.g. 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

  • 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-06-10T01:12:17+00:00Added an answer on June 10, 2026 at 1:12 am

    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…! 🙂

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

Sidebar

Related Questions

I'm trying to create an applet that draws a circle (defined as an object)
I'm trying to create a custom label subclass that draws rich text and in
I am trying to create a Java applet that animates a BTree. I have
I am trying to create a Java Applet that runs the client's webcam and
I`m trying to create a Java Applet, I was looking around on the Java
I created an audio Player using java applet but i'm stuck at trying to
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
I have a single .jar file that I create by using the runnable .jar
I'm trying to create a new launchagent file that will execute a shell script

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.