Anonymní profil Vlado – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu
Reklama
Reklama

Anonymní profil Vlado – Programujte.comAnonymní profil Vlado – Programujte.com

 

Příspěvky odeslané z IP adresy 91.127.70.–

Vlado
Java › Java hra - draggovanie obráz…
24. 11. 2012   #167513

Jop, díks. Už som vytváranie pričítadla hodil do inicializácie a je to ok. Ešte raz díky  

Vlado
Java › Java hra - draggovanie obráz…
23. 11. 2012   #167463

Zdravím... som v podstate začiatočník a v škole máme projekt, takú menšiu hru (zadanie vyzerá takto http://edi.fmph.uniba.sk/~tomcsanyiova/Java/11hra3x3/index.htm) a mám problém s pohybom objektu v applete... kód vyzerá takto:

(ide o môj prvý príspevok tu vo fóre, takže ak som niečo urobil zle, berte na to pls ohľad a upozornite ma, aby som vedel do budúcna)

import java.awt.*;
import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class Pocitanie extends Applet implements MouseListener, MouseMotionListener {

	int c,c1,x,y,sx,sy;
	Obrazok Cisla [] [] = new Obrazok [3] [3];
	Pricitadlo Pric;
	private BufferedImage bufImage;	
	boolean tahanie = false;
	
	class Obrazok {
		Image obr;
		int x,y,z;
		
		Obrazok (int xx, int yy, int zz) {
			this.x = xx;
			this.y = yy;
			this.z = zz;
			obr = getImage(getCodeBase(),"images/" + zz + ".gif");
		}
	}
	
	class Pricitadlo {
		Image obr;
		int x,y,nx,ny,z;
		
		Pricitadlo (int xx, int yy, int zz) {
			this.x = xx;
			this.y = yy;
			this.z = zz;
			this.nx = xx;
			this.ny = yy;
			obr = getImage(getCodeBase(),"images/" + zz + ".gif");
		}
	}
	
	public void init () {
		resize(600,450);
		bufImage = new BufferedImage(600, 450, BufferedImage.TYPE_INT_ARGB);
		addMouseMotionListener((MouseMotionListener) this);
		addMouseListener((MouseListener) this);
        
        for (int i = 0; i < 3; i++) {
        	for (int j = 0; j < 3; j++) {
        		y = 40;
        		x = 30;
        		switch (i) {
        		  case 1: y+= 110; break;
        		  case 2: y+= 220; break;
        		}
        		switch (j) {
        		  case 1: x+= 85; break;
        		  case 2: x+= 170; break;
        		}
        	    c = (int) (Math.random()*5+1);
        		Cisla [i] [j] = new Obrazok(x,y,c);	
        	}
        } 
    }
	
	public void update(Graphics g)
	  {
		  paint(g);
	  }
	
	public void paint(Graphics g) {
		c1 = (int) (Math.random()*9+1);
		sx = 325;
		sy = 150;
		Pric = new Pricitadlo (sx, sy, c1);
		
		Graphics bg = bufImage.getGraphics();
		bg.setColor(new Color(255,255,255));
		bg.fillRect(0, 0, 650, 450);
		
		bg.drawImage(Pric.obr,Pric.x,Pric.y,this);
				
		System.out.println(Pric.x);
				
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++) {
				bg.drawImage(Cisla[i][j].obr,Cisla[i][j].x,Cisla[i][j].y,this);	
				System.out.print(Cisla[i][j].x + " " + Cisla[i][j].y + " " + Cisla[i][j].z + " ");
			}
		
		g.drawImage(bufImage, 0, 0,this);
		g.fillRect(300, 25, 5, 350);
		g.drawString("Pričítadlo", 325, 125);
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		int x = e.getX();
        int y = e.getY();	
        
        if ((x >= Pric.x) && (x <= (Pric.x) + 75) && (y >= Pric.y) && (y <= (Pric.y+100))) {
        	tahanie = true;
        }
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		Pric.x=Pric.nx;	
		Pric.y=Pric.ny;
		int cis = (int) Math.random()*9+1;
		Pric.obr= getImage(getCodeBase(),"images/" + cis + ".gif");
		Pric.z = cis;
		
		repaint();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		if (tahanie == true) {
			Pric.x = e.getX()-20;
			Pric.y = e.getY()-20;
		}
		this.repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}

Díky moc

 

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032018 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý