P
P
pwnsauce2017-03-20 22:09:26
Java
pwnsauce, 2017-03-20 22:09:26

How to fix an object in a window??

Hello. I'm making a tank game and I have a problem. Here the "target" (cross) appears, but when the dialog box changes, it does not change its position relative to the map. How can you fix it on this white card? I will be grateful to you if you help me.
hPI0aEFA3nA.jpg6RkQmkoJmX4.jpg

public static void makeWindow(){
    panel = new Game(); 
      //TerrainFileHandler tfh = panel.new TerrainFileHandler();
    frame.add(panel);
    frame.setTitle("Prototyp 1 / A16B0087P");
    frame.setSize(640, 480);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true);
  }
  
  @Override
  public void paint(Graphics g){
    paint2D((Graphics2D)g);
    kresleniStrelce((Graphics2D)g);
    kresleniCile((Graphics2D)g);
  }
  
  /**
   * Kresleni do panelu
   * @param g2  graficky kontext
   */
  
  //тут вырисовывается эта белая карта и черная обводка
  public void paint2D(Graphics2D g2) {	
    int columns = this.getWidth();
    int rows = columns/2;
    if (rows > this.getHeight()){
      rows = this.getHeight();
      columns = 2*rows;
      
    }
    
    //karta (bely pryamoug.)
    g2.setColor(Color.WHITE);
    g2.fill(new Rectangle2D.Float(10, 10, columns-20, rows-12));
    //obvodka
    g2.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(1));
    g2.draw(new Rectangle2D.Float(10, 10, columns-20, rows-12));
  }
  
  //в этих двух методах находятся стрелок и цель (крестики)
  public static void kresleniStrelce(Graphics2D g2) {	
    g2.translate(shooter.x+50, 50+shooter.y);
    g2.setColor(shooter.color);
    g2.draw(new Line2D.Double(0, -5, 0, 5));
    g2.draw(new Line2D.Double(-5, 0, 5, 0));
  }
  public static void kresleniCile(Graphics2D g2) {	
    g2.translate(target.x+50, 100+target.y);
    g2.setColor(target.color);
    g2.draw(new Line2D.Double(0, -5, 0, 5));
    g2.draw(new Line2D.Double(-5, 0, 5, 0));
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2017-03-20
@pwnsauce

I'm not really good at drawing in java canvas, but I'll try to guess.
The overloaded paint method is overloaded for the entire window, and not for the white panel - here are the crosses and they are attached to the window. Usually, if you need to draw in some component, then you make your own component-heir from which Plane, Panel, Button thread to the extreme, and paint is already overloaded for it. Then all rendering will take place within this object.
This is about going beyond the white area.
If you want your labels to always remain in the same RELATIVE position when changing the size of the white area, then you need to consider relative coordinates. Those. instead of g2.translate(target.x+50, 100+target.y); should be something like g2.translate(0.5*width, 0.5*height);. where 0.5 - relative coordinates, width, height - canvas dimensions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question