java - How to intersect two objects drawn by two different graphics objects? -


i trying create game. in game, ui shows multiple paths(jpanels) obstacles (some sort of jcomponent) moving down paths. user (player) must move left , right or switch paths avoid obstacles. game similar google pony express.

https://www.google.com/doodles/155th-anniversary-of-the-pony-express

for now, because in developing stages, have 1 path. have player class extends jcomponent , overrides paint component method , draws blue square. have obstacle component class extends jcomponent , overrides paint component method , draws black circle.

in board class, create jpanel called thegame , put 1 path , inside of path, 1 obstacle component , 1 player. jpanel put jframe.

the player free move using key listener. keep obstacle component in 1 place make debugging easier. not main problem. problem want end game when player intersects obstacle. create skeleton rectangles resembles body of component. example rectangle blue square square , circle, rectangle covers area inside of it. call intersects method on 2 rectangles , if true, game over. however, intersect method not work. intersects method returns true when player , obstacle not visibly touching. (no, there no problem misproper refreshing or that). how intersect 2 objects? code provided if answerer needs it.

observation: not sure believe graphics objects used draw player , obstacle different. test hypothesis, drew green square in each of paintcomponent methods this:

public void paintcomponent(graphics g) {     //code create blue square     g2 = (graphics2d) g;     rect = new rectangle(x,y,w,h);     g2.setcolor(color.blue);     g2.fill(rect);     g2.draw(rect);      //code see green rectangle     rectangle bill = new rectangle(100,100,w,h);     g2.setcolor(color.green);     g2.fill(bill);     g2.draw(bill); } 

the reasoning if paint green rectangle seemingly same coordinates using graphics objects of player , obstacle, should end in same place on jpanel. instead, green rectangle objects showed in 2 different places. lead me believe graphic objects use different coordinate systems. if so, how use 1 universal coordinate system multiple graphic objects?

mcve: not make classes smaller because code not working if did that. player class: import javax.swing.; import java.awt.; import java.awt.geom.; import java.util.; import java.awt.event.*; public class player extends jcomponent { private int x; private int y; private int h = 20; private int w = 20; private rectangle rect; private int dx=5; private int dy=5; private graphics2d g2; public player(int x, int y) { this.x = x; this.y = y; rect= new rectangle(x,y,w,h); }

public void assigngraphics(graphics g) {     g2 = (graphics2d)g; }  public player() {     x=0;     y=0; }  public void paintcomponent(graphics g) {      g2 = (graphics2d) g;     rect = new rectangle(x,y,w,h);     g2.setcolor(color.blue);     g2.fill(rect);     g2.draw(rect);       rectangle test = new rectangle(100,100,w,h);     g2.setcolor(color.green);     g2.fill(test);     g2.draw(test); }  public rectangle getrectcomp() {     return rect; }  public void setmove(int a, int b) {     x=x+a;      y=y+b;     repaint(); }  public int getx() {     return x; }  public int gety() {     return y; }  } 

obstacle component: import javax.swing.; import java.awt.; import java.awt.geom.; import java.util.; import java.awt.event.*; import java.awt.image.bufferedimage; import java.net.url; import javax.imageio.imageio; import java.io.ioexception; public class obstaclecomponent extends jcomponent { private int x; private int y; public ellipse2d.double elli; private int dx=5; private int dy=5; private int h; private int w; private graphics2d g2; public obstaclecomponent(int x, int y, int h, int w) { this.x = x; this.y = y; this.h = h; this.w = w; }

public void assigngraphics(graphics g) {     g2 = (graphics2d)g; }  public obstaclecomponent() {     x=0;     y=0; }  public void paintcomponent(graphics g) // method declaration- method overrides paintcomponent method jcomponent class.  // method takes in explicit parameter of graphics object {     g2 = (graphics2d) g;     elli = new ellipse2d.double(x,y,w,h);     g2.setcolor(color.black);     g2.fill(elli);     g2.draw(elli);     rectangle test= new rectangle(0,0,w,h);     g2.setcolor(color.green);     g2.fill(test);     g2.draw(test); }  public void setmove(int a, int b) {     x=x+a; // sets dx variable value of parameter variable     y=y+b;     repaint(); }  public int getx() {     return x; }  public int gety() {     return y; }  public rectangle getrectcomp() {     return new rectangle(x,y,20,20); } } 

path:

import javax.swing.*; import java.awt.*;  import java.awt.geom.*;  import javax.swing.*;  import java.util.*; import javax.swing.plaf.tableui; import javax.swing.table.tablecolumn; import java.awt.event.*; import javax.swing.border.etchedborder; public class path extends jpanel implements actionlistener { public arraylist <obstaclecomponent> badguys; private int interval; private int xdimension = 1000; private int ydimension = 600; private graphics2d pen; public path(int rate) {     badguys = new arraylist<obstaclecomponent>();     setpreferredsize(new dimension(xdimension, ydimension));     setborder(new etchedborder());     setlayout(new gridlayout(1,1)); }  public graphics2d getg() {     return pen; }  public void actionperformed(actionevent av) {     if(interval%10==0)     {           badguys.add(new obstaclecomponent(200,10,20,20));         add(badguys.get(badguys.size()-1));         badguys.get(badguys.size()-1).assigngraphics(pen);     }     for(int b = 0;b<badguys.size();b++)     {          badguys.get(b).setmove(-5,0);      }      removeobstaclesoffthescreen();     repaint();     interval++; }  public void removeobstaclesoffthescreen() {     for(int b = 0;b<badguys.size();b++)     {         if(badguys.get(b).getx()<0)         {             remove(badguys.remove(b));         }     } }  } 

board:

import javax.swing.*; import java.awt.*;  import java.awt.geom.*;  import javax.swing.*;  import java.util.*; import javax.swing.plaf.tableui; import javax.swing.table.tablecolumn; import java.awt.event.*; import javax.swing.border.etchedborder; public class board  implements actionlistener { // instance variables - replace example below own private arraylist<path> paths;  public jpanel thegame; private  int interval; private int currentplayerpath; public player player; private int pathnumber = 1;//4; private int leveltime; private int levelrate; private boolean notgameover; private int removethiscounter=0; private int timetochangerate; private random gen; private int levelrange = 20; public board() {     thegame = new jpanel();     setlayout(pathnumber);     player = new player();     levelrate = 0;     paths =  new arraylist<path>();       gen = new random();     for(int = 0;a<pathnumber;a++)     {         paths.add(new path(getrandomrate()));     }     for(int = 0;a<pathnumber;a++)     {         thegame.add(paths.get(a));     }     currentplayerpath=0;     setcurrentplayerpath(); }  public jpanel returnthegamepanel() {     return thegame; }   public void setlayout(int paths) {     thegame.setlayout(new gridlayout(pathnumber, paths)); }   public void setcurrentplayerpath() {     paths.get(currentplayerpath).add(player);     player.assigngraphics(paths.get(currentplayerpath).getg()); }  public boolean hit() {     path currentpath = paths.get(currentplayerpath);     for(int = 0;a<currentpath.badguys.size();a++)     {         object b = player.getrectcomp();         object c = currentpath.badguys.get(a).getrectcomp();         if (((rectangle)b).intersects((rectangle)c))         {             return true;         }         else         {             system.out.println("player: " + player.getx() + " " + player.gety());             system.out.println("badguy: " + currentpath.badguys.get(a).getx() + " " + currentpath.badguys.get(a).gety());                      }      }     return false; }  public void actionperformed(actionevent av) {        for(int = 0;a<pathnumber;a++)     {         paths.get(a).actionperformed(av);        }        player.repaint();     thegame.revalidate();     thegame.repaint();   } public void gameover() {       notgameover=!notgameover; }  public boolean isgameover() {     return notgameover; } 

} engine:

import java.awt.*; import javax.swing.*; import java.awt.event.*; import javax.sound.sampled.*; import java.io.*; import java.awt.geom.*;  public class engine {  public static void main (string[] args) {             joptionpane.showmessagedialog(null, "press arrow key move.");     final jframe display = new jframe();      display.setsize(1000, 600);      display.settitle("code not work");      display.setdefaultcloseoperation (jframe.exit_on_close);     final board b = new board();     class timedaction implements actionlistener     {         private int removethiscounter=0;         public void actionperformed(actionevent av)         {             b.actionperformed(av);             if(b.hit())             {                 system.out.println("hit!!"+removethiscounter);                 removethiscounter++;             }                             display.revalidate();             display.repaint();         }      }     class playeraction implements keylistener     {         public void keypressed (keyevent e) {              int keycode = e.getkeycode();              if(keycode==keyevent.vk_left)             {                 b.player.setmove(-7, 0);              }             if(keycode==keyevent.vk_right)             {                  b.player.setmove(7, 0);              }             if(keycode==keyevent.vk_up)             {                 b.player.setmove(0,-7);              }             if(keycode==keyevent.vk_down)             {                  b.player.setmove(0,7);              }             else             {                 b.player.setmove(0,0);              }                        }          public void keyreleased(keyevent e) {} //method delcaration empty methods needed override abstract methods of keylistener interface         public void keytyped(keyevent e) {}     }     timer thetime = new timer(1000, new timedaction());     thetime.start();     jpanel masterpanel = new jpanel();     display.addkeylistener(new playeraction());      masterpanel.add(b.returnthegamepanel());     display.add(masterpanel);     display.setvisible(true);  } } 


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -