Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Java программирование > Общие вопросы по Java, Java SE, Kotlin
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 10.06.2014, 11:46   #1
MYuV
 
Регистрация: 03.05.2014
Сообщений: 5
Восклицание Поступательное и вращательное движение фигуры

Как организовать поступательное и вращательное движение фигуры при нажатии клавиши Enter.

Код:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;


public class L7 extends Applet implements Runnable, KeyListener, ActionListener{
	public double xT[]={ 4.0, -3.0, -5.0, 3.0, 2.0, -3.0, 3.5, 5.0};
	public double yT[]={-2.0, 2.5, 2.0, -1.0, -3.0, -5.0, 3.5, -4.0};
	 public double alpha=0, step=1;
  public int w=1100;
  public int h=800;
 
  public int R=8;
  public double  r=(R*Math.sqrt(3))/(Math.sqrt(3)+2);
  public double xC[]={-r, 3.7-r, r};
  public double yC[]={1.52-r, 0.5+r, 1.53-r};
  public double x0=0;
  public double y0=0;
  public Thread th=null;
  public double xB[]={6.5, -6.5, -6.5, 6.5};
  public double yB[]={6.5, 6.5,  -6.5, -6.5};
  
  public double xN1[]=new double[xC.length];
  public double yN1[]=new double[yC.length];
  
  public int xN2[]=new int[xC.length];
  public int yN2[]=new int[yC.length];
  
  public Image buff=null;
  public boolean b=false;
  Font f1=new Font("Arial", Font.PLAIN,20);
  Font f2=new Font("Arial", Font.PLAIN,17);
  
 
  
  public void init() {
		Panel p=new Panel();
	  	setLayout (new FlowLayout (FlowLayout.LEFT, 900, 500)); 		
	 	add(p);
	 	addKeyListener(this);	 
 }
  
 
  public void paint(Graphics g){
	  
	  requestFocus();
      buff = createImage(w,h);
      Graphics gr = buff.getGraphics();
	  g.setColor(Color.black);
	  g.setXORMode(Color.white);
	  g.drawLine(20, 400, 980, 400);
	  g.drawLine(500, 35, 500, 780);//y
	  g.fillOval(500-R*30+(int)Math.round(30*x0), 400-R*30-(int)Math.round(30*y0), 2*R*30, 2*R*30);  
	  
	  
	  int X[]=new int[yC.length];
      int Y[]=new int[yC.length];
      
	  for (int i=0; i<3;i++)
      {
		  X[i]=500+(int)((xC[i]-r)*30);
		  Y[i]=400-(int)((xC[i]+r)*30);
		  X[i]=500+(int)Math.round((xC[i]*Math.cos(alpha/180.0*Math.PI)+yC[i]*Math.sin(alpha/180.0*Math.PI)-r)*30);
    	 
		  Y[i]=400-(int)Math.round((-xC[i]*Math.sin(alpha/180.0*Math.PI)+yC[i]*Math.cos(alpha/180.0*Math.PI)+r)*30);
    	
		  g.fillOval(X[i], Y[i], (int)Math.round(30*r*2), (int)Math.round(30*r*2));
		  
		  }
		 
	  for (int i=0; i<8; i++) {
		  g.fillOval(500+(int)Math.round(30*xT[i]), 400-(int)Math.round(30*yT[i]),6,6);
		  g.drawString(Integer.toString(i+1), 500+(int)Math.round(30*xT[i]), 400-(int)Math.round(30*yT[i])); 
		  }
	  
	  for (int i=-15; i<=15; i++)
	    if (i!=0){ 
		  g.drawLine(500+30*i, 395, 500+30*i, 405);
  	      g.drawString(Integer.toString(i),495+30*i,425);
      }
	  
	  for (int j=-12; j<=12; j++)
	   if (j!=0){ 
  	      g.drawLine(495,400+30*j,505, 400+30*j);
  	      g.drawString(Integer.toString(j),515,405-30*j);
	  } 
	
	  g.drawString("0", 505, 415);
	  g.drawString("x", 980, 415);
	  g.drawString("y", 480, 40);
	  
	  gr.drawImage(buff,0,0,null);
	  
	  for (int i=0; i<=9; i++) {
		  if (i!=0)
		  g.drawRect(20, 20+28*i, 50, 30);
		  }
		  g.drawString("i", 45, 70);
	for (int i=0; i<=8; i++) {
		  if (i!=0)
		  g.drawString(Integer.toString(i), 45, 72+28*i);
		  }
		  		

	  for (int i=0; i<=9; i++) {
		  if (i!=0)
		  g.drawRect(72, 20+28*i, 50, 30);
		  }
		  g.drawString("x", 94, 70);
	  for (int i=0; i<=9; i++) {
		  if (i!=0)
		  g.drawRect(124, 20+28*i, 50, 30);
		  }
		  g.drawString("y", 144, 70);
			  
	  for (int i=0; i<=9; i++) {
		  if (i!=0)
		  g.drawRect(176, 20+28*i, 50, 30);
		  }
		  gr.drawImage(buff,0,0,null);
		  
		  g.drawString("-2.0",87,100);
		  g.drawString("3.5",90,128);
		  g.drawString("-1.0",86,156);
		  g.drawString("8.0",90,184);
		  g.drawString("-4.0",87,211);
		  g.drawString("2.0",90,239);
		  g.drawString("5.0",90,266);
		  g.drawString("6.5",90,295);
  }

  
  
  public void update (Graphics gr)
  {
	  paint(gr);
  }
  
	public void actionPerformed(ActionEvent e) {

		}

public void run() {
	while(b){
		try{
			th.sleep(1);
			alpha+=step;
			for(int i=0;i<xB.length;i++){
				xN1[i]=xC[i]*Math.cos(alpha*Math.PI/180)+yC[i]*Math.sin(alpha*Math.PI/180);
				yN1[i]=-xC[i]*Math.sin(alpha*Math.PI/180)+yC[i]*Math.cos(alpha*Math.PI/180);
				xN2[i]=240+(int)Math.round(15*xN1[i]);
				yN2[i]=240-(int)Math.round(15*yN1[i]);
			}					
			repaint();
		} catch (InterruptedException e){}	
	}
}

public void keyPressed(KeyEvent e) {
	int m=e.getKeyCode();
	if (m==e.VK_ESCAPE){
		step=0;	
		if (!b){
			th=new Thread(this); b=false;
			th.stop();
		}	
	}
	else if (m==e.VK_ENTER){
		alpha=0; 
		if (!b){
			th=new Thread(this); b=true;
			th.start();
		}			
	}
	else if (m==e.VK_UP) step-=1.0;
	else if (m==e.VK_DOWN) step+=1.0;	
	
}

public void keyReleased(KeyEvent arg0) {
}

public void keyTyped(KeyEvent arg0) {
}
}
Данный код только рисует фигуру, а функция keyPressed не работает.
MYuV вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Движение фигуры spinogryz_ua Общие вопросы по Java, Java SE, Kotlin 6 23.02.2013 18:19
Движение фигуры Di-em Паскаль, Turbo Pascal, PascalABC.NET 1 10.01.2012 20:55
Движение одной фигуры,вслед изменения высоты другой фигуры. 7vlad37 Microsoft Office Excel 3 21.07.2011 18:01
Движение фигуры Виталюкас Общие вопросы Delphi 2 28.04.2010 10:44
Движение фигуры в PaitBox Виталюкас Общие вопросы Delphi 7 27.04.2010 10:39