2013-05-20 2 views
-2

поэтому я написал этот код, но я не уверен, где идет прослушиватель мыши, а также как петля программы. im пытается достичь взрыва частицы, передавая координаты x и y мыши при нажатии. также я хочу, чтобы цикл его, пока программа не закрывается, но я не могу понять это. до сих пор все, что я получил это один взрыв основной класс (рама)java анимация проблемы с получением x и y корней мыши

import java.awt.Color; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import javax.swing.JFrame; 


public class AnimationOfExplosingSquares extends JFrame implements MouseListener{ 
private int x; 
private int y; 
AnimationOfExplosingSquares(){ 

    add(new ExplosingSquares(x,y)); 

     setBackground(Color.BLACK); 
} 

public static void main (String[] args){ 
AnimationOfExplosingSquares frame = new AnimationOfExplosingSquares(); 
frame.setTitle("Squares"); 
frame.setLocationRelativeTo(null); // Center the frame 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setSize(1280, 800); 
frame.setVisible(true); 
} 

@Override 
public void mouseClicked(MouseEvent me) { 
    while(x>0){ 
    this.x=me.getX(); 
    this.y=me.getX(); 
    } 
} 

@Override 
public void mousePressed(MouseEvent me) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

@Override 
public void mouseReleased(MouseEvent me) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

@Override 
public void mouseEntered(MouseEvent me) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

@Override 
public void mouseExited(MouseEvent me) { 
    throw new UnsupportedOperationException("Not supported yet."); 
} 

} 

класс ExplosingSquares

import java.awt.Graphics; 
import java.awt.event.*; 
import java.util.ArrayList; 
import javax.swing.JPanel; 
import javax.swing.Timer; 


public class ExplosingSquares extends JPanel implements ActionListener 
{ 
private int x; 
private int y; 
    private Timer timer = new Timer(20, this); 


private ArrayList<Square> squares=new ArrayList<Square>(); 


ExplosingSquares(int x,int y){ 
    this.x=x; 
    this.y=y; 


    for(int i=0; i<100;i++){ 


Square squ = new Square(x,y); 

    squares.add(squ); 
    } 
    timer.start(); 
} 

public void paintComponent(Graphics g){ 

super.paintComponent(g); 

for(Square square : squares) { 

    square.boom(); 

g.setColor(square.getColor()); 
// update the square's location 
    g.fillRect(square.getXCoord(), square.getYCoord(),(int)square.getSize(), (int)square.getSize()); 

} 
} 

@Override 
public void actionPerformed(ActionEvent ae) { 
    repaint(); 
} 
} 

Квадратная класс

import java.awt.Color; 
    import java.util.Random; 


    class Square { 

    private int width; 
    private int height; 
    //direction of x cordinate 
    private Random random= new Random(); 
    private int randomNumber=(random.nextInt(25)-12); 
    //direction of y cordinate 
    private Random rand= new Random(); 
private  int rando=(rand.nextInt(25)-12); 



private int x; 
    private int y; 
    private double size=50; 
    Color c=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256)); 

    Square(){ 


    } 
    Square(int width, int height){ 
     this.width=width; 
     this.height=height;  

    } 
     Square(int width, int height,Color c){ 
     this.width=width; 
     this.height=height; 
     this.c=c; 


    } 
    public void setWidth(int width){ 
     this.width=width; 
    } 
    public void setHeight(int height){ 
     this.height=height; 
    } 
     public void setSize(double size){ 
     this.size=size; 
    } 
     public void setColor(Color c){ 
     this.c=c; 
    } 
    public int getXCoord() { 
     return x+width; 

    } 
    public int getYCoord(){ 
     return y+height; 
     } 

     public double getSize(){ 
     return size; 
    } 
    public double getHeight(){ 
     return size; 
    } 
    public Color getColor(){ 
     return c; 
    } 


     public void boom(){ 
     this.x+=rando; 
     this.y+=randomNumber; 
     this.size-=1; 
     } 
    } 
+0

Отладка - важный навык, который вы абсолютно должны развивать ... –

ответ

0

Вы должны использовать .getY() если вам нужен y.

@Override 
public void mouseClicked(MouseEvent me) { 
    while(x>0){ 
    this.x=me.getX(); 
    this.y=me.getY(); 
    } 
} 
1

Swing использует одну нить для обработки изображений и событий. Все, что блокирует этот поток от выполнения, предотвратит обработку запросов обработки потоков событий.

Цикл while в mouseClicked событие остановит вашу программу от запуска.

Я предлагаю вместо этого использовать javax.swing.Timer. См. Concurrency in Swing для получения более подробной информации.

Смежные вопросы