Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: java working unexpectedly  (Read 3666 times)

0 Members and 1 Guest are viewing this topic.

TheWaffle

    Topic Starter


    Hopeful
  • Thanked: 4
    • Yes
  • Computer: Specs
  • Experience: Beginner
  • OS: Linux variant
java working unexpectedly
« on: March 05, 2013, 04:47:12 PM »
I want the cyan rectangle to move but it don't.

Code: [Select]
package circlegame.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.geom.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class game extends JFrame {

Rectangle r1  = new Rectangle(30,50,30,40);
Rectangle r2  = new Rectangle(300,150,30,40);
move1 m1 = new move1();
private JPanel contentPane;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
game frame = new game();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//thread
public class move1 extends Thread
{
public void run(){
//infinate loop
while(true)
{
try
{
repaint();
Thread.sleep(100);
}
catch(Exception e)
{
break;
}
}
}
}
public void paint(Graphics frame){
super.paint(frame);
frame.setColor(Color.CYAN);

frame.fill3DRect(r1.x, r1.y, r1.width, r1.height,true);
frame.setColor(Color.BLUE);
frame.fill3DRect(r2.x, r2.y, r2.width, r2.height,true);

}
public void move(){
try
{
r1.x +=(int)r1.x;
r1.setLocation(r1.x, r1.y);
}
catch(Exception e)
{
System.out.print("Error");
}
}


/**
* Create the frame.
*/
public game() {
super("Circlegame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 350);
contentPane = new JPanel();
contentPane.setBackground(new Color(255, 255, 255));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
m1.start();
}

}
why doesn't it work?

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: java working unexpectedly
« Reply #1 on: March 05, 2013, 05:04:41 PM »
Quote
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               game frame = new game();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
* Maybe this is correct for Java syntax, but I am use to seeing a function called that is independent of being enclosed within ( ...... ); multilines, and not structured how you have it with everything contained within a spanned multiline (  ...... ); as I highlighted.

I don't program in Java so this may be valid syntax

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: java working unexpectedly
« Reply #2 on: March 05, 2013, 06:39:38 PM »
Well it doesn't show anything as is. I had to change the paint method to get them to show up at all:

Code: [Select]
public void paint(Graphics frame){
System.out.println("paint");
//super.paint(frame);
Rectangle clip = frame.getClipBounds();
frame.clearRect(clip.x,clip.y,clip.width,clip.height);
frame.setColor(Color.CYAN);

frame.fill3DRect(r1.x, r1.y, r1.width, r1.height,true);
frame.setColor(Color.BLUE);
frame.fill3DRect(r2.x, r2.y, r2.width, r2.height,true);
}


The cyan rectangle (r1) doesn't move because the move() method is never called. add move(); before repaint() in the run method.

* Maybe this is correct for Java syntax, but I am use to seeing a function called that is independent of being enclosed within ( ...... ); multilines, and not structured how you have it with everything contained within a spanned multiline (  ...... ); as I highlighted.

I don't program in Java so this may be valid syntax

It's correct. In fact, the original code could be refactored to completely eliminate move1. right now they are subclassing Thread. this is bad. Don't do this. First, what they ought to be doing is creating a class that implements Runnable, and passing an instance of that class to the Thread constructor. Thread should only be subclassed if you want to add new Thread functionality, not to implement the Run method. Right now:

Code: [Select]
Thread move1 = new Move1();

My version:

Code: [Select]
Thread move1 = new Thread(new Runnable() {public void run(){
//infinite loop
while(true)
{
try
{
move();
//invalidate();
repaint();
Thread.sleep(100);
}
catch(Exception e)
{
break;
}
}}});

This basically moved the implementation within the class they had that derived from Thread to here. to explain more simply the part that got you confused: that would be the anonymous interface implementation feature of Java. In this case, it's implementing Runnable. A simple example:

Code: [Select]
private void somemethod(Runnable runner){
runner.run();
}
private void main(String[] args){
somemethod(new Runnable(){
public void run(){System.out.println("howdy!");}
}
);
}

(Not formatted well). basically, you can use new with an interface type and define the interface implementation in-line. The documentation is perhaps a more heady source of information.
I was trying to dereference Null Pointers before it was cool.