Wednesday, 15 April 2015

swing - Moving image with mouse Java -



swing - Moving image with mouse Java -

okay new java , having problems on project assigned me. having problem moving ctetrimino object across jframe here ctetrimino class.

import java.awt.color; import java.awt.graphics; import java.awt.image; public class ctetrimino { public ctetrimino(int type, int x, int y, int w, int h, color c) { cmino m = new cmino(); d = m.getdiameter(); type=type; x=x; y=y; width=w; height=h; fillcolor=c; } public ctetrimino(ctetrimino src) { // re-create constructor type=src.type; x=src.x; y=src.y; width=src.width; height=src.height; fillcolor=src.fillcolor; } public int getx() { homecoming x; } public void setx(int x) { x=x; } public int gety() { homecoming y; } public void sety(int y) { y=y; } public void draw(graphics g) { g.setcolor(fillcolor); switch (type) { case 0: //g.filloval(x, y, width, height); //break; g.filloval(x, y, d, d); g.filloval(x+d,y, d, d); g.filloval(x+d,y-d, d, d); g.filloval(x+d+d,y-d, d, d); break; case 1: g.fillrect(x, y, width, height); //g.filloval(x, y, d, d); //g.filloval(x+d,y, d, d); //g.filloval(x+d,y+d, d, d); //g.filloval(x+d+d,y+d, d, d); break; case 2: g.filloval(x, y, d, d); g.filloval(x+d,y, d, d); g.filloval(x,y-d, d, d); g.filloval(x+d,y-d, d, d); break; case 3: g.filloval(x, y, d, d); g.filloval(x,y+d, d, d); g.filloval(x,y+d+d, d, d); g.filloval(x,y-d, d, d); break; case 4: g.filloval(x, y, d, d); g.filloval(x,y+d, d, d); g.filloval(x,y-d, d, d); g.filloval(x+d,y-d, d, d); break; case 5: g.filloval(x, y, d, d); g.filloval(x,y+d, d, d); g.filloval(x,y-d, d, d); g.filloval(x-d,y-d, d, d); break; case 6: g.filloval(x, y, d, d); g.filloval(x+d,y, d, d); g.filloval(x,y-d, d, d); g.filloval(x-d,y, d, d); break; } } public boolean containpoint(int x, int y) { switch (type) { case 0: { double a=width/2.0; double b=height/2.0; double xc=x+a; double yc=y+b; system.out.println(a); system.out.println(b); system.out.println(xc); system.out.println(yc); system.out.println(((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)<=1.0)); homecoming ((x-xc)*(x-xc)/(a*a)+(y-yc)*(y-yc)/(b*b)<=1.0); } case 1: homecoming (x>=x && y>=y && x<x+width && y<y+height); } homecoming false; } private int type; private int x; private int y; private int width; private int height; private color fillcolor; private int d; }

here panel class add together jframe in testnewtetris class.

import java.awt.color; import java.awt.image; import java.awt.graphics; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import javax.swing.*; import java.util.*; public class panel extends jpanel implements mouselistener, mousemotionlistener { private arraylist<ctetrimino> originals; private arraylist<ctetrimino> duplicates; private ctetrimino blocktobemoved; private int m_noffsetx; // difference between cursor , top-left corner private int m_noffsety; // double buffering private image backbuffer; private graphics gbackbuffer; boolean isinitialized; // init , register mouse event handler public panel() { isinitialized=false; // handle mouse , mouse motion events this.addmouselistener(this); this.addmousemotionlistener(this); } // set initial state after panel created void init() { // initial state duplicates = new arraylist<ctetrimino>(); originals = new arraylist<ctetrimino>(); color[] colors = {color.red, color.green, color.blue, color.magenta, color.cyan, color.yellow, color.orange}; int count=colors.length; int dx=10; int dy=30; int gap=20; int length=(getsize().height-2*dy-(count-1)*gap)/count; (int i=0; i<count; i++) { //originals.add(new ctetrimino((i<count/2)?0:1, dx, dy+i*(length+gap), length, length, colors[i])); system.out.println(length); originals.add(new ctetrimino(i, dx, dy, length, length, colors[i])); dx = dx+110; } blocktobemoved=null; // no shape selected // create buffer backbuffer = createimage(getsize().width, getsize().height); gbackbuffer = backbuffer.getgraphics(); } // state presentation public void paintcomponent( graphics g ) { // super.paintcomponent( g ); // clears drawing area if (!isinitialized) { isinitialized=true; init(); } // state presentation, using double buffers // first, clear buffer gbackbuffer.setcolor(color.white); gbackbuffer.clearrect(0, 0, getsize().width, getsize().height); // draw originals buffer (int i=0; i<originals.size(); i++) { originals.get(i).draw(gbackbuffer); } // draw duplicates buffer (int i=0; i<duplicates.size(); i++) { duplicates.get(i).draw(gbackbuffer); } // re-create buffer front end g.drawimage(backbuffer, 0, 0, null); g.fillrect(0, 100, 800, 4); g.fillrect(0, 400, 800, 4); } // end method paintcomponent // mouselistener event handlers public void mouseclicked( mouseevent e ) { if (e.ismetadown()) { // right button (int i=duplicates.size()-1; i>=0; i--) { if (duplicates.get(i).containpoint(e.getx(), e.gety())) { duplicates.remove(i); repaint(); break; } } } } public void mousepressed( mouseevent e ) { if (e.ismetadown()) return; // ignore right button // first, check originals, top downwards (i.e. front) (int i=duplicates.size()-1; i>=0; i--) { ctetrimino p=duplicates.get(i); if (p.containpoint(e.getx(), e.gety())) { duplicates.remove(i); duplicates.add(p); // move end, i.e. top blocktobemoved=p; m_noffsetx=e.getx()-blocktobemoved.getx(); m_noffsety=e.gety()-blocktobemoved.gety(); repaint(); return; } } // second, check orginals (int i=originals.size()-1; i>=0; i--) { ctetrimino p=originals.get(i); system.out.println(p.containpoint(e.getx(), e.gety())); if (p.containpoint(e.getx(), e.gety())) { ctetrimino p2=new ctetrimino(p); // create re-create of p duplicates.add(p2); // add together end blocktobemoved=p2; // p2 selected, moved m_noffsetx=e.getx()-blocktobemoved.getx(); m_noffsety=e.gety()-blocktobemoved.gety(); repaint(); return; } } } public void mousereleased( mouseevent e ) { blocktobemoved=null; // no shape selected } public void mouseentered( mouseevent e ) { } public void mouseexited( mouseevent e ) { } public void mousemoved( mouseevent e ) { } public void mousedragged( mouseevent e ) { if (e.ismetadown()) return; // ignore right button system.out.println(blocktobemoved); if (blocktobemoved!=null) { blocktobemoved.setx(e.getx()-m_noffsetx); blocktobemoved.sety(e.gety()-m_noffsety); repaint(); } } // end method mousedragged }

i show programme when run not have high plenty reputation yet.

the bottom jframe compiled if alter code:

case 0: g.filloval(x, y, d, d); g.filloval(x+d,y, d, d); g.filloval(x+d,y-d, d, d); g.filloval(x+d+d,y-d, d, d); break;

to this:

case 0: g.filloval(x, y, width, height); break;

in both variants able drag greenish square. in sec variant able drag reddish circle. can't move reddish tetrimino. wondering why cannot move reddish tetrimino. want able move of tetriminos , drag them under sec black line delete them. want able rotate tetriminos pressing righr click. code or advice appreciated. if need more info provide asap. thanks.

if understood correctly solution should below. please create image bundle below.you need 2 events label mouse dragged , label mouse pressed that. add together icon label should image image.

critical imports

import java.awt.point;

events

private void jlabel1mousepressed(java.awt.event.mouseevent evt) { initialclick = evt.getpoint(); } private void jlabel1mousedragged(java.awt.event.mouseevent evt) { int thisx = jlabel1.getlocation().x; int thisy = jlabel1.getlocation().y; // determine how much mouse moved since initial click int xmoved = (thisx + evt.getx()) - (thisx + initialclick.x); int ymoved = (thisy + evt.gety()) - (thisy + initialclick.y); // move image position int x = thisx + xmoved; int y = thisy + ymoved; jlabel1.setlocation(x, y); jlabel1.repaint(); }

java swing user-interface graphics jframe

No comments:

Post a Comment