How to change starting position of an object in jframe

197
April 25, 2018, at 03:28 AM

I'm currently trying to change the position of a object that I created in Jframe and cannot seem to get the starting position of the object to change. I have other code in the program, but this is the only part that I think you will need. I tried posting the x and y locations of the object that I made but it would not format as code for me. Note, it is not a button it is a object made up of x and y coordinates. Thanks in advance.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DiveGame extends JPanel 
{
private static final long serialVersionUID = 1L;
int currentx=0;    
int currenty=0;    
int depth=20;
int Fish1move = 150;
int Fish1x = 0;
int Fish1y = 0;
int Fish2move = 200;
int Fish2x = -100;
int Fish2y = -200;

/*The constructer method sets some things up for the start of the 
program*/
public DiveGame()
{      
PolygonPanel listener = new PolygonPanel();  //create a polygon 
listener
addMouseListener(listener);  //listen for mouse changes 
setBackground(new Color(126,197,252));
addMouseMotionListener(listener);  //listen for mouse movement
setPreferredSize(new Dimension(1500,525));
System.out.println("We are currently at " +depth+ "ft");
}//set default size of JFrame window 

/*This method will draw in the graphics panel of the Jframe.  It
 * will be called every time we invoke repaint and it will redraw
 * the entire graphics panel*/
 public void paintComponent(Graphics page)
{
super.paintComponent(page);
 //create and draw sub main body
   int[] xloc_mainbody = {};
   int[] yloc_mainbody = {};
   if(depth >= 659)
     page.setColor(new Color(127,127,63));
   else if(depth <= 659)
     page.setColor(new Color(188,188,117));
   page.fillPolygon(xloc_mainbody,yloc_mainbody,xloc_mainbody.length); 
 //change the width of the brushstroke to make lines wider
   Graphics2D page2d = (Graphics2D) page;
   page2d.setStroke(new BasicStroke(3));
 //Drawing the Subs Portholes
 //Porthole1
 if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+130,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+130,currenty+200,23,23);
 //Porthole2
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+185,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+185,currenty+200,23,23);
 //Porthole3
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+240,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+240,currenty+200,23,23);
 //Porthole4
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+295,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+295,currenty+200,23,23);
 //Porthole5
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+350,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+350,currenty+200,23,23);
 //Porthole6
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+405,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+405,currenty+200,23,23);
 //Porthole7
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+460,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+460,currenty+200,23,23);
 //Porthole8
  if(depth <= 659)
   page.setColor(new Color(204,204,0));
 else if(depth >= 659)
   page.setColor(new Color(255,255,102));
 page.fillOval(currentx+515,currenty+200,22,22);
 page.setColor(Color.black);
 page.drawOval(currentx+515,currenty+200,23,23);
 if(depth >= 659)
   setBackground(new Color(22,113,182));
 else if(depth <= 659)
    setBackground(new Color(126,197,252));

  //draw Fish #1
  if(depth <= 659)
  page.setColor(new Color(204,255,255));
  else if(depth >=659)
  page.setColor(new Color(0,204,204));
  int[] xloc_Fish1x = {};
  int[] yloc_Fish1y = {};
  page.fillPolygon(xloc_Fish1x,yloc_Fish1y,xloc_Fish1x.length);
  //draw Fish #2
  if(depth <= 659)
   page.setColor(new Color(255,153,153));
  else if(depth >=659)
   page.setColor(new Color(153,0,0));
  int[] xloc_Fish2x = {};
  int[] yloc_Fish2y = {}; 
  page.fillPolygon(xloc_Fish2x,yloc_Fish2y,xloc_Fish2x.length);
  repaint(); //repaint calls this method immediately- allowing for 
  animation
  } //end paintComponent
  /*The following method is the listener for mouse changes*/
  private class PolygonPanel implements MouseListener, 
  MouseMotionListener
  {
  public void mousePressed(MouseEvent event){}   //unused event
  public void mouseDragged(MouseEvent event){}   //unused event
  //The next method will determine what to do when a mouse button is 
  clicked
  //In this case, a left click will descend and a rightclick will 
  ascend
  public void mouseClicked(MouseEvent event){
  if (event.getButton()==MouseEvent.BUTTON1)
  {
    depth += 10; 
    if(depth>20)
    { 
     System.out.println("DECENDING");
      System.out.println("You are at " +depth+ "ft");
      System.out.println("\n ==================== \n");
    }
   repaint();
    } //end if Button 1
    if (event.getButton()==MouseEvent.BUTTON3)
    {
       depth -= 10; 
       System.out.println("ASCENDING");
       System.out.println("You are at " +depth+ "ft");
       System.out.println("\n ==================== \n");
       repaint();
    }//end if button 3 
    } //end mouseClicked
   public void mouseReleased(MouseEvent event) {}  //unused event
   public void mouseEntered(MouseEvent event) {}   //unused event
   public void mouseExited(MouseEvent event) {}    //unused event

   public void mouseMoved(MouseEvent event) 
   {
   currentx=event.getPoint().x-200;  
   currenty=event.getPoint().y-190;  
   repaint();  //redraw the image  
   {
   if (Fish1move+Fish1x>3)
    Fish1move-=5;  
   else
    Fish1move=-200;  
   if (Fish2move-Fish2y>2)
    Fish2move-=4;
   else
    Fish2move=200;

    }

    } 
    }  
    }
Rent Charter Buses Company
READ ALSO
Logging Errors from Multiple Threads in Java

Logging Errors from Multiple Threads in Java

I am using the built in Java logger to log errors (caught in a try-catch statement) to a text file

158
Using batch to run multiple java programs

Using batch to run multiple java programs

I wrote a batch file to run multiple batch files which run a java program separatelyI am wondering if I need to pass an argument to the java program when I run it, how do I pass the argument when I start the station batch files? The main batch file looks like this:

231
Best practices for web service extension

Best practices for web service extension

I have a maven-based multi-territory RESTful Java web service using JAX-RS (Jersey) deployed on Grizzly and using CDI (WELD) for Dependency InjectionIt runs as a stand-alone application (inside a Docker container) to wire-up the Server and CDI in App

159
Delay/ Wait-time in getting access to fork-join thread pool from main applications thread

Delay/ Wait-time in getting access to fork-join thread pool from main applications thread

I have a fork-join thread pool with parallelism of #of cpu's *12 (4 * 12) in my caseThey do db operation in parallel

151