Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Jabor
Jul 16, 2010

#1 Loser at SpaceChem
The way the UI thread works is this:

1. Have any events happened?
2. If yes, run the associated event handler
3. Have any events happened?

One of those events is the window-drawing event - when something on the window changes and the whole thing needs to be redrawn, the redraw is processed much like any other event.

If your event handling code locks itself up (say but doing a Thread.sleep()), then the redrawing code won't ever get to run until your event handler finishes up.

You basically need to be using the timer class - make sure you're using the javax.swing.Timer version, as you can run into threading issues with java.util.Timer.

Adbot
ADBOT LOVES YOU

Fly
Nov 3, 2002

moral compass

Paolomania posted:



I'd like to use this on our log processing options page, but someone might think that's "unprofessional". I think you can't get any more professional than a machine like that.

Freundlich Freund
Jan 29, 2010

Cojawfee posted:

After a few years of doing nothing, I've decided to get back into Java. I want to redo some dungeon crawler game I made for a final project in high school. I'm off to a somewhat decent start as NetBeans easily created a nice GUI for me (the one I made in high school was some sort of Frankenstein's monster I created out of frantic googlings of Swing and AWT). But I've fallen into an old habit where I want to try something that I probably will never use but I can't move forward until I've made it work.

Basically I want to type something in to a JTextField, press enter, then it appends the text to a JTextArea, then it sets the text field to uneditable, sets the textfield to say "wait" and then three seconds later clears that field and sets it back to editable. I've successfully been able to do everything except the wait three seconds part. I've tried using Thread.sleep(), but no matter where I put it, it seems to do the sleep before all the other parts. I tried to use the Timer class but NetBeans says there aren't any constructors for it.

I've tried moving all the different parts into their own methods. It seems that if I call my waiting method, it will always do it before any method before it or something.

Just another case out of many where my brain doesn't work the same as whoever designed whatever language I'm using.

Try and use SwingWorker!

Have some code:

code:
appendText = JTextField.getText();
text = JTextArea.getText() + appendText;
JTextField.setEnabled(false);
new Wait().execute();

private class Wait extends SwingWorker<Boolean, Void> {
    protected Boolean doInBackground() throws Exception {
            Thread.sleep(3000); //wait 3000ms
            return true;
    }

    @Override
    protected void done() {
        try {
                JTextField.setEnabled(true);
        } catch (Exception e) {
        }
    }
}
SwingWorker is used to allow the UI thread to run while you do heavy processes (or waiting in our case) behind the scenes.

cisneros
Apr 18, 2006
I need to do some simple stuff in Java ME, what would be the best SDK/ GUI to go about it?

Max Facetime
Apr 18, 2009

cisneros posted:

I need to do some simple stuff in Java ME, what would be the best SDK/ GUI to go about it?

There's Java ME SDK 3.0 for Windows which is the latest official SDK. You can use the Java ME SDK 3.0 with Eclipse by installing a Mobile Tools for Java plugin. The SDK has a UI for compiling, running and packaging so you may not need an IDE at all. Then there's the older Wireless Toolkit 2.5.x that's available on Linux as well.

Java ME consists of few related technologies, so it's hard to give better suggestions without knowing what devices you're targeting and how simple your stuff is.

Speed
Dec 25, 2010

I wanna sleep...
Alright, in my java programming class I'm doing a change calculator. Underwhelming I know. I know for a fact I am doing many things wrong on the math but I am not sure how to fix it properly, and I can't figure out how to display the final results properly, those being my only two problems.

code posted:

import java.util.Scanner;

public class Changecalcuator {

public static void main(String[] args) {

double toonies = 0;
double loonies = 0;
double quarters = 0;
double nickles = 0;
double dimes = 0;
double pennies = 0;
double money = 0;
int option = 0;

Scanner input = new Scanner(System.in);

System.out.println("Please enter the amount of money you would like to calculate change for");
money = input.nextInt();

System.out.println ("Please enter the coin type you would like to recieve");

System.out.println ("1 for Toonies, 2 for Loonies, 3 for Quarters, 4 for Dimes, 5 for Nickles, 6 for pennies");
option = input.nextInt();

while (option < 1|| 6>6 ){
System.out.println ("Please enter a value from 1 to 6");
option = input.nextInt();

switch (option){
case '1':
toonies = money / 2.00;
money = money - toonies*2;

case '2':
loonies = money / 1.00;
money = money - loonies;

case '3':
quarters = money / 0.25;
money = money - quarters * 25;

case '4':
dimes = money / 0.10;
money = money - dimes * 10;

case '5':
nickles = money / 0.05;
money = money - nickles * 5;

case '6':
pennies = money / 0.01;
money = money - pennies ;
break;

}
}
}
}

e: accidentally posted while trying to indent (tab and space), but I have the feeling it is still readable this way.
e2: after much dickery this is the best I can get it, sorry

Speed fucked around with this message at 03:09 on Mar 2, 2011

Paolomania
Apr 26, 2006

Do you have a question about how to do something specific aside from "fix my program"? Displaying one of your computed values is as simple as System.out.println("Silly Canadian two-dollar coins = " + toonies);

Speed
Dec 25, 2010

I wanna sleep...
Yeah I guess it looked like that.

Something specific is that whenever I get a value displayed it comes out as 0.0, I can't figure out what is causing this.

Solving that would be a better start for me, as I could just go the long way for displaying all of the values.

cisneros
Apr 18, 2006

I am in posted:

There's Java ME SDK 3.0 for Windows which is the latest official SDK. You can use the Java ME SDK 3.0 with Eclipse by installing a Mobile Tools for Java plugin. The SDK has a UI for compiling, running and packaging so you may not need an IDE at all. Then there's the older Wireless Toolkit 2.5.x that's available on Linux as well.

Java ME consists of few related technologies, so it's hard to give better suggestions without knowing what devices you're targeting and how simple your stuff is.

Just some basic matrix stuff(rref, etc), I had that sdk you linked, but for some reason it doesn't run on my machine, but I got a complete version of NetBeans and that did the trick.

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.
while (option < 1|| 6>6 ){

what


edit: also, tip: '1' != 1.

Speed
Dec 25, 2010

I wanna sleep...
E: I completely overthought this entire thing and I am an idiot

case 1:while (money >2){
money = money - 2;
++toonies;
}

All I had to do to overcome this stupidly easy thing.

Speed fucked around with this message at 05:16 on Mar 2, 2011

Malloc Voidstar
May 7, 2007

Fuck the cowboys. Unf. Fuck em hard.

Speed posted:

Well I basically rewrote the program and got rid of the ' and that incredibly weird while loop (no idea when I put that in or why). Its working now for the most part, the only problem being that remainders exist so I'm getting 5.375 when dividing 10.75 by 2, I'm not sure what to do about that though. But this has been help anyway so thank you.
If you want to just drop the remainder completely, cast the result to an int.
int a = (int) (10.75 / 2);

But something important to know is to never ever using floating-point math when ever dealing with money ever. (That means no floats, no doubles, no decimals.)
As an example of why, compile and run this:
code:
public class FPMath {
	public static void main(String[] args) {
		for (double i = 0; i < 1; i += 0.1) {
			System.out.println(i);
		}
	}
}
(you may or may not have gone over this in class)

Fly
Nov 3, 2002

moral compass

Aleksei Vasiliev posted:

But something important to know is to never ever using floating-point math when ever dealing with money ever.
Repeat the above line 100 times.

Specifically if you're doing any sort of arithmetic at all.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

Fly posted:

Repeat the above line 100 times.

Specifically if you're doing any sort of arithmetic at all.

Every time I encounter money as float I first feel a twinge of self satisfaction knowing I'm not that dumb, then I feel an overwhelming sense of dread as I've probably just nicked the tip of the iceberg.

Seafea
Mar 21, 2003

They say it's not what life throws at you, but how you deal with it.


So, I'm dealing with a String that could be in one of two formats. Either ABC or BC, where A/C are a positive integer, and B is the letter S.

I need a way to split the String into it's pieces. I've tried using String.trim().split("S"), but for the Array I get from that, .size() returns 2, even if the format is AX.

I tried using a scanner, using this code:

Scanner scanForNums = new Scanner(a);
if (scanForNums.hasNextInt())
{
test.add(scanForNums.nextInt());
}
System.out.println(test.size());

but then test.size() returns 0, so I can't use that.


My best guess right now, is to use .split, because it at least gives me the correct pieces, and I count them manually with an int to know how many there are. Any ideas better than that?


BTW, the goal of this is to return an ArrayList containing the A instances of the String "BC".

Outlaw Programmer
Jan 1, 2008
Will Code For Food
How about just:
code:

int delimiter = input.indexOf("S");

if(delimiter < 0)
    throw new IllegalArgumentException("Input is invalid: " + input);

int A = delimiter == 0 ? -1 // case where 'A' is absent.
                       : Integer.parseInt( input.substring(0, delimiter) );

int C = Integer.parseInt( input.substring(delimiter + 1) );

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

Seafea posted:

So, I'm dealing with a String that could be in one of two formats. Either ABC or BC, where A/C are a positive integer, and B is the letter S.

I need a way to split the String into it's pieces. I've tried using String.trim().split("S"), but for the Array I get from that, .size() returns 2, even if the format is AX.

My best guess right now, is to use .split, because it at least gives me the correct pieces, and I count them manually with an int to know how many there are. Any ideas better than that?

BTW, the goal of this is to return an ArrayList containing the A instances of the String "BC".

So you want to return the ones that are <number>S<number> in a list, and not the ones that are S<number>? Or you want to return only the first number from the instances that have an S in the middle? (I'm a little confused about your change in lettering, from ABC/BC to AX to 'the A instances of the String "BC"')

Why not just just use .startsWith("S") to determine which case you're in, then you can do .substring(0, .firstInstance("S")) or whatever to get the first number (if that's what you're looking for??)?

Seafea
Mar 21, 2003

They say it's not what life throws at you, but how you deal with it.

Kilson posted:

So you want to return the ones that are <number>S<number> in a list, and not the ones that are S<number>? Or you want to return only the first number from the instances that have an S in the middle? (I'm a little confused about your change in lettering, from ABC/BC to AX to 'the A instances of the String "BC"')

Why not just just use .startsWith("S") to determine which case you're in, then you can do .substring(0, .firstInstance("S")) or whatever to get the first number (if that's what you're looking for??)?

I need it in 2 different blocks of code. The first one just strips that first coeffecient, leaving me with BC.

The second one will add A instances of BC to an ArrayList. (I guess I could create a class for that that would just hold A and BC, to waste less space in ArrayLists, but don't need to for the moment.)

Oops. I had the lettering as AXA at first, and changed it to clarify that the two A's did not have to be the same integer.

I like the .startsWith("S") though. It's a simple solution I overlooked.

Thanks!

fart factory
Sep 28, 2006

SHREK IS COOL
Is there any simple way to perform an action without notifying a listener? I've got a JXTreeTable with a tree expansion listener on it, which I want to get notified when a single node is expanded, but not when the "expand all" function is used (this just iterates over each row and attempts to expand it).

The issue is that on expansion the table creates a hashtable of all the entries, in order to determine which colour the rows will be. However if there are, say, 20,000 entries in the table, when all are expanded the listener will be notified 20,000 times, which will then go over all 20,000 records for each notification.

Paolomania
Apr 26, 2006

Probably easier for your expansion listener to just ignore expansion of the root.

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!
I'm trying to extract a bodypart from a MIME Multipart message. I get the correct bodypart and everything, but this code isn't working as I understand it should.

code:
MimeMultipart mmp = (MimeMultipart) req1.getContent();
for (int i = 0; i < mmp.getCount(); i++) {
  MimeBodyPart bp = (MimeBodyPart) mmp.getBodyPart(i);
  if (bp.isMimeType(PIDF_LO_MIME_TYPE)) {
    bp.setDataHandler(new DataHandler(bp, "text/plain"));
    String content = (String) bp.getContent();
    ...
  }
}
I'm getting:
java.lang.ClassCastException: javax.mail.internet.MimeBodyPart cannot be cast to java.lang.String

I don't understand why bp.getContent() would give me back a MimeBodyPart. Setting the DataHandler as text/plain is supposed to give me back a String. Also, I'm pretty sure this code used to work. Is there a proper way to do this?

Luminous
May 19, 2004

Girls
Games
Gains
Well, I don't know specifically an answer (never used), however, the documentation MimePart.getContent says that it always returns a subclass of MultiPart for objects that are a multipart type, which I assume your MimeBodyPart would be in this case.

mcw
Jul 28, 2005
After a year of developing Java EE apps in Eclipse, I've now been asked to switch to Maven by Monday. Does anyone have any suggestions for quality online resources that can show me the "best practices" of Maven? It seems like there's an archetype for everything-- many of which are old, or just plain fail when I try to use them to build a project. I'm starting to wonder if most folks just use a "generic" archetype of some sort and then put a shitload of dependencies in the POM.

covener
Jan 10, 2004

You know, for kids!

MariusMcG posted:

After a year of developing Java EE apps in Eclipse, I've now been asked to switch to Maven by Monday. Does anyone have any suggestions for quality online resources that can show me the "best practices" of Maven? It seems like there's an archetype for everything-- many of which are old, or just plain fail when I try to use them to build a project. I'm starting to wonder if most folks just use a "generic" archetype of some sort and then put a shitload of dependencies in the POM.

I've always freeloaded on this, but I wouldn't think you'd contrast the two.

You layout your war or ear projects the way maven wants, run mvn eclipse:eclipse (or mvn eclipse:m2eclipse) then continue developing in eclipse like you always have.

m2eclipse has good user doc.

mcw
Jul 28, 2005

covener posted:

I've always freeloaded on this, but I wouldn't think you'd contrast the two.

You layout your war or ear projects the way maven wants, run mvn eclipse:eclipse (or mvn eclipse:m2eclipse) then continue developing in eclipse like you always have.

m2eclipse has good user doc.

m2eclipse? Thanks; I'll read through that. I'm a complete newbie to Maven, and am eventually looking to build these projects on the server using Hudson, so I figure it'd be best to learn how most folks do this while I'm learning Maven in general.

geeves
Sep 16, 2004

What are some decent frameworks that anyone can recommend I take a look at - I know a bit about what's out there, but I've been mainly stuck in Struts and Apache Jackrabbit / Sling for the last 5 years and really haven't really looked. I have the opportunity to possibly pick something fresh (for me anyway) for a small freelance project I'm taking.

The project is a licensed search-oriented application based on pre-defined content - there are other details to be handled but that is the bulk of the application.

mcw
Jul 28, 2005

geeves posted:

What are some decent frameworks that anyone can recommend I take a look at - I know a bit about what's out there, but I've been mainly stuck in Struts and Apache Jackrabbit / Sling for the last 5 years and really haven't really looked. I have the opportunity to possibly pick something fresh (for me anyway) for a small freelance project I'm taking.

I dig straight-up Java EE 6, man. EJB 3.1, CDI, Bean Validation, and JSF... Very powerful stuff.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

geeves posted:

What are some decent frameworks that anyone can recommend I take a look at - I know a bit about what's out there, but I've been mainly stuck in Struts and Apache Jackrabbit / Sling for the last 5 years and really haven't really looked. I have the opportunity to possibly pick something fresh (for me anyway) for a small freelance project I'm taking.

The project is a licensed search-oriented application based on pre-defined content - there are other details to be handled but that is the bulk of the application.

Spring
Grails if you want a full on web thing
Guice is supposedly nice
Solr for search rules

zootm
Aug 8, 2006

We used to be better friends.

geeves posted:

What are some decent frameworks that anyone can recommend I take a look at - I know a bit about what's out there, but I've been mainly stuck in Struts and Apache Jackrabbit / Sling for the last 5 years and really haven't really looked. I have the opportunity to possibly pick something fresh (for me anyway) for a small freelance project I'm taking.

The project is a licensed search-oriented application based on pre-defined content - there are other details to be handled but that is the bulk of the application.
I hear many good things about the Play Framework for websites.

lamentable dustman
Apr 13, 2007

🏆🏆🏆

Been playing with GWT at work, kinda cool but kinda dumb. Would learn some Spring if you've never worked with it though

Stubb Dogg
Feb 16, 2007

loskat naamalle

geeves posted:

What are some decent frameworks that anyone can recommend I take a look at - I know a bit about what's out there, but I've been mainly stuck in Struts and Apache Jackrabbit / Sling for the last 5 years and really haven't really looked. I have the opportunity to possibly pick something fresh (for me anyway) for a small freelance project I'm taking.

The project is a licensed search-oriented application based on pre-defined content - there are other details to be handled but that is the bulk of the application.
I recently worked on a project with Seam 2.2 and it removed lots of usual annoying stuff you have to deal with when doing complex web sites in Java and made it actually enjoyable.

Though at this point it's probably better go straight to Seam 3.0 as it's getting close to release and 3.0 brings portable CDI based on EE 6.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

osama bin diesel posted:

Been playing with GWT at work, kinda cool but kinda dumb. Would learn some Spring if you've never worked with it though

GWT is terrible for anything of any size or complexity.

geeves
Sep 16, 2004

Stubb Dogg posted:

I recently worked on a project with Seam 2.2 and it removed lots of usual annoying stuff you have to deal with when doing complex web sites in Java and made it actually enjoyable.

Though at this point it's probably better go straight to Seam 3.0 as it's getting close to release and 3.0 brings portable CDI based on EE 6.

Thanks to everyone who responded - seems it was all for naught though - for at least the demo. I have to convert their DB from... Filemaker Pro to MySQL. 1 million records which will convert to about 25k after properly designing the DB. Unfortunately it seems I'll be doing the proof of concept in PHP since more of my time is being spent converting this database - as well as hosting it myself.

:ughh:

Diametunim
Oct 26, 2010
I was wondering if anyone could help me out here, I'm currently in a high school computer science 2 class and our current project (and final for the year)is as a class we're writing super smash brothers as a class (teacher included). The only real requirements is that every piece of code besides the engine must be written from scratch. Here lies our problem, none of my classmates (There's 7 of us) except for one guy knows jack poo poo about writing a physics engine. That one guy however also has to handle all of the networking code because he is the only one in the class that knows anything about networking as well. Most of us all feel that it's not exactly fair or going to be efficant for him to be writing the two most important parts of the code.

My question is does anyone know any opensource 2D physics engines that are relatively good and easy to understand? I've stumbled around trying to look for a decent one and came across Phys2D as well as JBullet. The problem is our teacher doesn't like the idea of using JBullet (or couldn't understand the code) so we're back at square one.

So to make this short and simple, does anyone know a decent opensource 2D physics engine that would be acceptable for writing a java version of Super Smash Brothers? keep in mind this doesn't have to be perfect we're a bunch of worthless highschool students.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
If you're just trying to make a sidescrolling beat-em-up, do you really need to do that much in terms of physics?

For a 2D game, you need to understand two concepts- collision and movement. Checking if the hitboxes for two sprites overlap doesn't have to be any more complicated than:
code:
boolean collidesWith(Rectangle a, Rectangle b) {
        return ( a.y + a.height >= b.y            ) &&
               ( a.y            <  b.y + b.height ) &&
               ( a.x + a.width  >= b.x            ) &&
               ( a.x            <  b.x + b.width  );
}
(Or better yet use the stuff in java.awt.geom and it'll be even simpler.)

The other easy option is doing circular collision- compare the distance between the centerpoints of your sprites and see if they're closer than a bounding radius. Apply the pythagorean theorem.

For movement, crack open a calculus textbook. It'll contain about five percent concepts and ninety-five percent algebra and applications- focus on that first five percent. Think about how things in your game move in terms of changes to their position, their velocity and their acceleration. Gravity is a constant acceleration downards. Hitting a horizontal or vertical wall multiplies the vertical or horizontal component of the object's velocity, respectively, by something close to -1. Play with it.

Just try to think like a developer for the NES. If something is hard, is there some way you can fake it or simplify it? Taking a conservative, aggressively DIY approach may not create the most impressive game in the world, but you'll learn way more than you would by using a physics engine you barely understand.

Just my two cents. If you try to do things from scratch and run into specific problems along the way, I'm sure we'd be happy to offer suggestions.

baquerd
Jul 2, 2007

by FactsAreUseless

Internet Janitor posted:

Gravity is a constant acceleration downards. Hitting a horizontal or vertical wall multiplies the vertical or horizontal component of the object's velocity, respectively, by something close to -1. Play with it.

Your post was all good advice, I just want to add that having a terminal velocity is a very good idea.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
baquerd: Oh yeah, totally. You should also have a minimum velocity cutoff so that friction makes things stop eventually. I didn't mean for that to sound like an exhaustive explanation.

Diametunim
Oct 26, 2010
Thanks for the help guys, I really appreciate it. We were looking at using an open source engine just for the purpose of saving time since we only have about 7 weeks to write everything else. However three of us just ended up getting together and writing our own. So far we have friction, gravity and jumping and all of our characters are going to inherit or be based off a basic rectangle (am I saying that right?)

On a completely unrelated note from this project, 7 years ago or so my computer science teacher had the class at the time write their own version of Legend of Zelda. The class never got very far other than creating a basic dungeon you could walk through that had a short midi sound file playing in the background. You could "attack" but you wouldn't do any damage and there was only one little enemy that didn't attack you but he also couldn't be killed.

The weird issue is and it's an issue we believe with how the sound threading was coded? (I'm trying to explain things that are over my head again) however when the midi file for the dungeon sound ends the program closes and no one can figure out why the hell this happens.

anyone have any ideas on what the problem could be? like I said it was coded by the kids who took his class 7 years ago so other than having the code I really don't know anything about how it was written.

again, thanks for the help guys.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
For hitboxes I would favor composition (a character has a rectangle used for collision) over inheritance (a character is a rectangle that can collide with things), but either approach can work. If you start to have a more complex hierarchy of things in your game that are similar to characters you'll probably see where I'm coming from with this.

Without the code that sound issue will probably remain shrouded in mystery. If you can pastebin the source to whatever class was responsible for playing music we might be able to figure it out. Do you know if the game crashes with an exception or just exits cleanly?

Adbot
ADBOT LOVES YOU

Dub Step Dad
Dec 29, 2008
In my programming class I'm working on a project where I have a JPanel class that I am going to put several rectangles on (by using fillRect in its paint method), and am going to be able to move the rectangles around with the mouse. My problem is, the only way I can think of checking if a rectangle is being clicked is by checking if the mouse event's xy-coordinates are within the rectangle's boundaries, which causes issues when I click over an area where two rectangles are overlapping where both rectangles snap to one location, so one is covering the other. Is there another way to check if a rectangle is being selected? Or at least some way for my program to know which rectangle is above another?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply