Graphic interface java help

Graphic interface java help

A modified checkers program with class name Checkers using double buffering. Somehow the program must have two top squares and two bottom squares. There should be two checkers of different color. The two checkers always move in opposite directions. When a checker reaches the end of its path, it should start moving backwards in the opposite direction.

Somehow need to enhance the Checkers program above to respond to mouse clicks. The checkers must stop moving whenever the user clicks and holds down the mouse. Then start moving again whenever the mouse is released. Cannot figure this out at all, please help.

View Answers

April 26, 2012 at 12:41 PM

1)Checkers.java:

 import java.awt.Graphics;
    import java.awt.Color;
    import java.awt.Image;
    import java.awt.event.*;

    public class Checkers extends java.applet.Applet implements Runnable, MouseListener
    {

      Thread thread;
      int x;
      int y;
      Image image;
      Graphics graphics;

      public void init()
      {
        addMouseListener(this);
        x = 5;
        y = 100;
       image = createImage(this.size().width, this.size().height);
       graphics = image.getGraphics();
      }

      public void start()
      {
        if (thread == null);
         {
          thread = new Thread(this);
          thread.start();
        }
      }

      public void stop()
      {
        if(thread != null)
         {
          thread.stop();
          thread = null;
        }
      }

      public void run()
      {
        while (true)
         {
          while(x <= 105)
            {
            repaint();
            try { Thread.sleep(100); }
            catch (InterruptedException e) { }
              y-=4;
              x+=4;
          }

            while(x >= 5)
            {
            repaint();
            try { Thread.sleep(100); }
            catch (InterruptedException e) { }
              x-=4;
              y+=4;
          }
        }
      }

      public void update(Graphics g)
      {
         paint(g);
      }


      public void paint(Graphics g)
      {
        graphics.setColor(Color.black);
        graphics.fillRect(0,0,100,100);

         graphics.setColor(Color.black);
         graphics.fillRect(100,100,100,100);

        graphics.setColor(Color.gray);
        graphics.fillRect(100,0,100,100);
         graphics.setColor(Color.gray);
         graphics.fillRect(0,100,100,100);    

        graphics.setColor(Color.red);
        graphics.fillOval(x,5,90,90);

         graphics.setColor(Color.blue);
         graphics.fillOval(y,100,90,90);

        g.drawImage(image,0,0,this);
      }
      public void destroy()
      {
         graphics.dispose();
      }

      public void mousePressed (MouseEvent e)
       {
            this.stop(); 
        }
        public void mouseReleased (MouseEvent e)
       {
            this.start();
        }

      public void mouseClicked(MouseEvent arg0) {}
        public void mouseEntered(MouseEvent arg0) {}
        public void mouseExited(MouseEvent arg0) {}
    }

2)applet.html:

<HTML>
<BODY>
<APPLET CODE ="Checkers.class" WIDTH="800" HEIGHT="500"></APPLET>
</BODY>
</HTML>

Ads









Related Tutorials/Questions & Answers:
Graphic interface java help
Graphic interface java help  A modified checkers program with class name Checkers using double buffering. Somehow the program must have two top..., please help
Interface in Java
Interface in Java Interface in java programming languages is a collection... oriented concept. Through interface only abstraction is achieved in java. Syntax...; Java. In an interface, only abstract method can be declared. Interface
Advertisements
java interface
java interface  Create a interface called student Define methods like get roll number Get name Get subject Create two classes Electronic student And history student Enter 5 students in the main class 2 of history and 3 elec
Interface in java
Interface in java  An Interface method implemented in more than one different class with same method name. To avoid overriding of methods, we use ObjectReference for that class
Marker Interface,Java Marker Interface
Interface in Java       In this section we will learn about Interface and Marker Interfaces in Java... is also the interface's example. But in java programming language interface is nothing
interface.
interface.  Write short note on interface.   Please visit the following link: Java Interface
java help!
java help!   I need help starting off a program I am trying to write for class. The class should contain a private instance variable to represent temperature in Degrees Celsius and it should have the following methods
Java Interface
Java Interface In this section we will discuss about the interface in Java... interfaces. Interface in Java is created for referencing something to other. To create an interface in Java the keyword "interface" is used
Help With Java...
Help With Java...  Write a java program that can take a positive integer greater than 2 as input and write out the number of times one must repeatedly divide this number by 2 before getting a value less than 2
help in java
help in java   Rational Numbers Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber
INTERFACE
INTERFACE  how interface support multiple inheritance in java
help in java
help in java  Write an application that prompt the user to enter a number to use as an array size, and then attempt to declare an array using the entered size. Java generates a NegativeArraySizeException if wou attempt to create
help for java
help for java  1.convert the following Pseudo code to java if/else structure if student?s grade is greater than or equal to 90 Print ?A? else if student?s grade is greater than or equal to 80 Print ?B? else
help in java
help in java  3.Flights Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight's origin and destination cities
help in java
help in java  Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consist of a number, followed by an operator, followed
help in java
help in java  Design and implement a class called Sphere that contains instance data that represents the sphere's diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter
help in java
help in java  The factorial of a nonnegative integer n is written n! (pronounced √Ę‚?¨Ň? n factorial√Ę‚?¨¬?) and is defined as follows: n!=n . (n-1) . (n-2) . .... . 1 (for values of n greater than or equal to 1) and n!=1 (for n
interface
interface  what the use of interface?   An interface is one... variables.Any class can implement(inherit)the interface and make use of the methods(functions) and variables within it. In java, multiple inheritance
help in java
help in java  Gross And Dozens Design and implement a class called GrossAndDozens to convert a given number of eggs into the number of gross, the number of dozens, and to the number of left over eggs. If you have N eggs, then you
help in java
help in java  Shopping cart Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use
help in java
help in java   Team Roster Create a program called TeamRoster that uses a class called Roster. Roster should contain a team name and the names of all... or help me on this pleas.. 7. Gross And Dozens Design and implement a class
help in java
help in java  Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have a number
help in java
help in java  Temperature Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have
help in java
help in java  Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use getter and setter
help in java
help in java  Define a class named Money whose objects represent amounts of U.S. money. The class will have two instance variables of type int for the dollars and cents in the amount of money. Include a constructor with two
help in java
help in java  Create a class named Order that performs order processing of a single item. The class has five instance variables (fields) : customer name, customer number, quantity ordered, unit price, and total price. Define
interface
interface  What is marker interface ?? what is its use in java programming?? is this us in programming ??Explain is implementation with code
java help
java help  How to Open CSV Files in a Microsoft Excel Application Using Java Code with example pgm
interface
interface   what is the use of marking interface
java help
java help  1) build class(es) for the dresses, choose (dress ID, color, price, designer)( link lists) 2) construct the driver class by establishing two separate objects for each sister 3) let them both to enter the dresses
java help
java help  Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber for representing such numbers

Ads