My sketch won't show anything when running in processing - java

I am completely new to processing, and I wanted to test it by creating a simple sketch that draws a rectangle, however, when i run the sketch, a window pops up with nothing on it. I tried filling it, putting an outline on it, and various other things, nothing happened. I don't think it's a problem with the code but a problem with the app itself, and i'm not sure how to fix it. I am using processing 3.5.4 on windows.
Code:
class Sketch {
void setup() {
size(100, 100);
}
void draw() {
rect(50, 50, 25, 25);
}
}
Expected Output: A square shows up on screen.
Output: Nothing shows up
<iframe src="https://drive.google.com/file/d/1gAQsX0hpAyH_iSbUXkyO87a8NpXscLEL/preview" width="640" height="480"></iframe>

Chiming in with the obvious (don't hurt me if I didn't get your question right):
draw() and setup() are already of Processing. In fact, those you wrote into a class won't be automatically executed, as Processing will be looking for it's own methods. That's why you get this:
Which is, incidentally, exactly the same result as if you ran an empty sketch.
To fix this, just take draw() and setup() out of the class, and Processing will then find and run them:
void setup() {
size(100, 100);
}
void draw() {
rect(50, 50, 25, 25);
}
Have fun!

Related

Java robot class mouseMove not going to specified location

To be honest I am not entirely sure what is wrong. This is the short version of a ton of other basic robot command movements under the if and if else.
Whenever I run the program the mouse should move to the designated position and click. However when I run the program it does not move to the position I indicated, instead it moves to a different position each time I run it(I do not have any listeners designated to change the position so the position shouldn't be changing). I do not know if it is something with the code I have written itself or possibly my imports? The program was running correctly until recently in which I added the else at the end to end the program, I have run it without the else and still come up with the same issue. Any help would be much appreciated.
package creator;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import javax.swing.*;
public class RobotDemo extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
public static void main(String[] args) throws AWTException, IOException
{
double value = (-0.66721);
{
if (value < -0.3)
{
Robot robotdelta = new Robot();
//Enters Chrome from java
robotdelta.delay(5000);
robotdelta.mouseMove(587, 1045);
robotdelta.delay(1000);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
//Enters online platfrom
robotdelta.mouseMove(770, 21);
robotdelta.delay(1000);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
}
//secondary situation
else if (value > 0.3)
{
Robot robotdelta = new Robot();
//Enters Chrome from java
robotdelta.delay(1000);
robotdelta.mouseMove(587, 1045);
robotdelta.delay(100);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
//Enters online platfrom
robotdelta.mouseMove(770, 21);
robotdelta.delay(100);
robotdelta.mousePress(InputEvent.BUTTON1_MASK);
robotdelta.delay(100);
robotdelta.mouseRelease(InputEvent.BUTTON1_MASK);
robotdelta.delay(1000);
}
else
{
system.exit(0);
}
}
}
public void actionPerformed(ActionEvent e) {
}
}
As a disclaimer, I was playing around with this class for a while, and the most important thing I learned was this was a tool meant for very rudimentary testing, and really no large scale crucial operation should ever hinge on this class working exactly as expected.
To answer your question, there is really no way to get exactly where you tell mouseMove() to go (at least not when I was working with it). However, what seemed to get it pretty close was to call mouseMove() multiple times to the same place (Yes, this is very hacky and not desirable). For example, is I wanted to move the mouse to (300,600) on screen, I found that if you do:
mouseMove(300,600);
mouseMove(300,600);
mouseMove(300,600);
// ... can put more if you want
for some strange reason it gets much closer to where you want to go than if you just call mouseMove() one time. I have no idea why this might be the case, but hopefully this helps. Not to mention, it is also a good idea to put ample delays in between calling the robot to do different actions, and to ensure that waitForIdle() is invoked.

executing a forloop in a particular time

This is my onDraw method, which I use to create my sprites and draw them every fps =10:
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.rgb(21, 181, 195));
canvas.drawBitmap(bmp, 0, 0, null);
for (Sprite wave1 : sprites) {
wave1.onDraw(canvas);
}
}
i want to run this for loop in this code again and again ... i tried to run it with timer task but when i write this for loop inside my timer task method it say error it says i can be used only in a canvas class and also showing error on receiving the image from resource files even after trying to get resource file there it says error again saying it can be used only inside a sprite class :(
so i want a way to run this for loop here in timed interval in this same Ondraw method
: Any ideas how to do it ? try ed handler class too :
Have you tried using a Thread?
Maybe this could help you: http://obviam.net/index.php/sprite-animation-with-android/

Java repaint not displaying component correctly

im developing an GUI Application, using JSWing, i load XML file, then deserialize it, then i add all created object to the JPanel.
However, until i move the window, or click on the panel, this is how they looks like
After i move the window, they look correctly, so how to fix this issue <
I looked at this link
http://docs.oracle.com/javase/6/docs/api/javax/swing/JComponent.html#paintComponent(java.awt.Graphics)
and it might be the answer, since in the constructor of the JComponent i use
setOpaque(true);
but im still not sure how to fix the issue since that part of documentation is very hard to understand (it somehow just does not make any sense to me :-D )
by the way, the painting itselfs goes something like this
for (NetObject o : objects) {
addNewObject(o);
}
and addNewObject (not whole code)
public void addNewObject(NetObject o) {
DraggableComponent object = new DraggableComponent(o, editorIndex); //Creates a draggableComponent
this.add(object);//Adds this component to main container
object.setOverbearing(true); //On click ,this panel gains lowest z-buffer
object.setSize(48, 48);
object.setLocation(o.x - 23, o.y - 23);
object.setBackground(Color.WHITE);
this.repaint(); //this = JPanel
}
and the overriden paintComponent code
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isOpaque()) {
if (object.type == 2) { //tarnsition
g.drawImage(transition, 0, 0, this);
} else if (object.type == 1) {
boolean test = g.drawImage(place, 0, 0, this); //place
g.drawString(object.loadTokens(), 3, 27); // 1,2,3,4...
}
}
}
i tried to call this.revalidate - after FOR EACH LOOP - didnt help, the only way that works is to move somehow with the window, strangely, this problem exists only # Windows, my collegue is developing this exact same application under Linux, and he does not experience ani graphical issues.
I know that there been an awfully lot of topics like this, but i honestly was not able to figure out the solution.
Thanks for the answer,
OSiRiS
The setBackground() API mentions that "It is up to the look and feel to honor this property, some may choose to ignore it." Set the graphics context's color explicitly in paintComponent() and invoke fillRect().

I forget to stop a gui program and run it again. Now I can't close the first program without killing eclipse

I am working on a simple GUI app that just draws some graphics on a canvas. The environment is Vista 64. When I run the program, the Windows resize and minimize buttons work, but the close button doesn't. So I have to press the stop button in Eclipse to kill the program.
But sometimes I forget to press stop, and run the program again. The first instance gets stuck and I can't get rid of it without closing Eclipse. If I get careless I can end up with several java windows I can't close. Is there a way to get control of and close the windows? Also, why does the close button not work?
I doubt the code matters in this case but here it is:
import java.awt.*;
public class RobotFace extends Canvas{
/**
* #param args
*/
public static void main(String[] args) {
RobotFace c = new RobotFace();
c.setBackground(Color.white);
c.setSize(350, 350);
Frame f = new Frame();
f.add(c);
f.setLayout(new FlowLayout());
f.setSize(350,350);
f.setVisible(true);
}
public void paint(Graphics g){
g.setColor(Color.black);
int width = 150;
int height = 200;
g.drawRect((getWidth()-width)/2, (getHeight()-height)/2, width, height);
g.setColor(Color.gray);
g.fillRect((getWidth()-width)/2, (getHeight()-height)/2, width, height);
g.setColor(Color.white);
g.fillRect((getWidth()-80)/2, (getHeight()+50)/2, 80, 20);
g.setColor(Color.yellow);
g.fillOval((getWidth()-105)/2, (getHeight()-100)/2, 30, 30);
g.fillOval((getWidth()+45)/2, (getHeight()-100)/2, 30, 30);
}
}
You can also kill it using the console of eclips , there is a red button(right cornor) when you open console view in eclips.
To open console view
Window --> Show View --> Console
using this way you can close the Frame/window
//add window event adapter
f.addWindowListener(new MyWindowAdapter());
class MyWindowAdapter extends WindowAdapter{
MyWindowAdapter(){
}
//implement windowClosing method
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
Your Eclipse console for the first application instance should still be available, just not visible. On the Console tab, you can click the toolbar button that looks like a little blue monitor to switch between application instances. To stop the first application instance, you would select that instance using the little blue monitor (it's called "Display Selected Console"), then click the "Stop" button.
One option is to change the behavior of your program so that it actually exits with the window is close (which is not the default behavior or Frames in Java.
The Frame javadoc specifies that it can generate WindowClosing events, which you can use to trigger your app to close (using a class like #Pratik does in their answer).
In my opinion, a better solution would be to replace your frame with a JFrame and use it's method f.setDefaultCloseOperation(EXIT_ON_CLOSE) so that you application with exit when the window is closed. JFrame is written such that it should be a drop in replacement for awt.Frame, but is part of the swing toolkit.

Problem with Java Applet

I'm new to Java Applets. I have a problem while reloading the applet. When I resize the applet window or open some other application and then come back to the applet, the contents on the screen is redrawn. Basically my paint method is getting called. I want the contents of the paint method to be called only once. How can I accomplish this? Can anyone please help me with this?
Thanks in advance.
You are misunderstanding how paint works - you have no real control over how many times it is called. What are you doing in the paint method that makes you think you only want to do it once and why is it a problem that it gets called again?
If you're worrying about flickering, then you might like to look at painting into a buffer. Code not directly related to painting should not be in the paint method. You can put other initialization logic in the applet's start method
If you're putting initialization code into the paint method, you might think about putting it into the init or start methods instead.
The phrase you'd be looking for is applet lifecycle.
I am new to java applet design and when running it i get one problem that says "no main"
here is my code:
import java.applet.*;
import java.awt.*;
public class abhidev extends Applet {
/** Initializes the applet abhidev */
public void init() {
try {
setBackground(Color.cyan);
}
catch(Exception e){
e.printStackTrace();
}
}
public void paint(Graphics g){
try{
g.drawString("this ais an applet window",10,30);
showStatus("this is astatus window");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
The applet class name is: abhidev.java.

Categories

Resources