/ Forside / Teknologi / Udvikling / Java / Nyhedsindlæg
Login
Glemt dit kodeord?
Brugernavn

Kodeord


Reklame
Top 10 brugere
Java
#NavnPoint
molokyle 3688
Klaudi 855
strarup 740
Forvirret 660
gøgeungen 500
Teil 373
Stouenberg 360
vnc 360
pmbruun 341
10  mccracken 320
Drawing
Fra : Max Rotvel


Dato : 15-04-02 21:21

After I finished a thread assignment in school I got interested
in the drawing part of it (See code below).

I'm interested in creating some collision detection. Is is
possible to do this be looking at the colors somehow? (I know,
a bit vague question .

Each ball is obviously started in a separate thread that paints
on the JPanel. Would it be more effective to calculate the
positions of all the balls and then draw them all at the same
time? This would make it possible to do collision detecting
using g2.hit(...), right?

I'm also interested in creating a fancier ball. I'm thinking
about a Shape object (a circle) with some gradient colors, but
I would like some pointers on how to do this.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

class Ball extends Thread
{
   private JPanel box;
   private static final int XSIZE = 10;
   private static final int YSIZE = 10;
   private int x, y, dx, dy;
   
   private static final Color[] COLORS =
      {Color.blue, Color.green, Color.yellow,    
       Color.red};
   private Color ballColor;
   private Color boxBgColor;
   
   private volatile boolean run;
   
   private Graphics2D g;
   private Dimension d;
   
   private Random rnd;
            
   public Ball(ThreadGroup tg, JPanel b)
   {
      super(tg, "");
      box = b;

      rnd = new Random(new Date().getTime());
      
      ballColor = COLORS[rnd.nextInt(4)];
      boxBgColor = box.getBackground();
            
      initBox();
      
      x = y = 0;
      dx = dy = 2;;
      
      run = true;

      box.addComponentListener(new ComponentAdapter()
      {
         public void componentResized       
             (ComponentEvent e)
         {
            initBox();
         }            
      });
   }

   private void initBox()
   {
      d = box.getSize();
   
      g = (Graphics2D)box.getGraphics();
      g.setBackground(box.getBackground());   
      g.setColor(ballColor);
   }
   
   private void draw()
   {
      g.fillOval(x, y, XSIZE, YSIZE);
   }
   
   private void clear()
   {
      g.clearRect(x, y, XSIZE, YSIZE);
   }
      
   private void move()
   {
      clear();
      
      x += dx;
      y += dy;
   
      if (x < 0)
      {
         x = 0;
         dx = -dx;
      }
      else if (x + XSIZE >= d.width)
      {
         x = d.width - XSIZE;
         dx = -dx;
      }
   
      if (y < 0)
      {
         y = 0;
         dy = -dy;
      }
      else if (y + YSIZE >= d.height)
      {
         y = d.height - YSIZE;
         dy = -dy;
      }
      
      //else if (box.getForeground()
      
      draw();
   }
   
   public void run()
   {
      draw();
   
      while (true)
      {
         try
         {
            sleep(7);

            if (!run)
               synchronized (this) {wait();}
         }
         catch (InterruptedException e) {}

         move();
      }
   }
   public void interrupt()
   {
      run = !run;
      
      if (run) synchronized (this) {notify();}
   }
}

Kind regards
--
Max

 
 
Max Rotvel (16-04-2002)
Kommentar
Fra : Max Rotvel


Dato : 16-04-02 10:19

Max Rotvel <rotvel@mail.dk> wrote:

> After I finished a thread assignment in school I got interested
> in the drawing part of it (See code below).

Oops, forkert gruppe. Sorry, men svar endelig hvis I skulle få
lyst

Venlig hilsen
--
Max

Søg
Reklame
Statistik
Spørgsmål : 177501
Tips : 31968
Nyheder : 719565
Indlæg : 6408522
Brugere : 218887

Månedens bedste
Årets bedste
Sidste års bedste