Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - nemo

Pages: 1 2 [3] 4 5 ... 82
31
Computer Programming / Re: Java - Load Images on Screen Display
« on: June 19, 2011, 01:41:23 pm »
could you show us which tutorial you're using? i see thenewboston has about 80 tutorials on java and i don't know which one your code references.



try this:
Code: [Select]
catch(Exception ex) {
System.out.println("An error occurred: " + ex);
}

instead of
Code: [Select]
catch(Exception ex) {

}

in the setFullScreen method of the Screen class.

also, put
Code: [Select]
System.out.println(vc.isDisplayChangeSupported());
before the line
Code: [Select]
if (dm != null && vc.isDisplayChangeSupported()) {

in the same method and class. if your program prints "false," your graphics card won't allow java to interact in a low-level way with it, and i don't know how to fix it. but here's a link describing what's wrong.

if you get "An error occurred: [something]," then copy/paste what the [something] says. although i haven't really dealt with the classes DisplayMode and GraphicsDevice, the rest of the code looks fine.

32
Axe / Re: Functional Axe
« on: June 16, 2011, 09:27:14 pm »
Mapping and folding in Functional programming basically are functions that apply to all members of an Array, List, etc.  Basically, they loop and fix all numbers one by one until the end of array or the specified length has been reached.  An example:

implementing mapping, folding and filtering in Axe isn't very efficient though. The power behind those three functions is that they are higher order, which isn't possible in Axe (i.e. you can't pass a function as a parameter). for example, say you want to make 3 separate functions. one that add 5 to every element of a list, one that takes the square root of every element and one that divides each element by 3. in a functional programming language, you would have one function called map, and then three functions, add5(x), sqrt(x), and div3(x). then you'd call map with the parameters of each function and a list. technically you can "map" in Axe, but without the advantages of mapping in functional programming, which IMHO defeats the purpose.

it would be pretty cool to see higher order functions in Axe though.

33
Other / Re: How to show that Chrome is secure?
« on: June 04, 2011, 08:49:56 pm »
Browser statistics

here's some statistics showing how many people use ie, chrome, firefox, opera and safari. you could use the bandwagon approach ("more people use chrome than IE, how can they be wrong?")

also, it depends upon what version of IE you have. if you have IE9, that's not a bad thing.

34
Computer Programming / Re: java giant button glitch
« on: May 26, 2011, 11:49:50 pm »
look into using layouts.

35
Computer Programming / Re: [Java] Key Events and Multiple Classes
« on: May 26, 2011, 05:51:57 pm »
the KeyEvents class shouldn't extend JPanel. That's why the GamePanel class exists. in the Squared class you had a new DrawingPanel, which we'll refer to as "dp1" for simplicity. dp1 is the panel added to the frame, which is seen on your screen. Then you make a new KeyEvents object, which contains a GamePanel. we'll call that dp2. then, whenever you press the right arrow key, dp2's x coordinate is incremented by 5. if the problem in your code isn't apparent by now, refer to the bolded portion of this post.

also, a pro tip. if your class contains instance variables or methods that don't refer to the class name, something's wrong. in this case, your class KeyEvents has a GamePanel object. second, your Squared class doesn't take advantage of constructors. your main method should be as simple as "new Squared();" or "new Squared().show();" -- where show is just frame.setVisible(true); lastly, it's good practice to use constants, e.g. KeyEvent.VK_RIGHT represents the right arrow key, instead of hard-coding it as 39. this makes your code more understandable (eliminating the need for the comment "Right key") and makes it easy to change which number is the right arrow key if needed. for example, you wouldn't write this:
Code: [Select]
frame.setDefaultCloseOperation(-3);

because -3 has no significant value. if you looked back on that line of code in 3 years you would have no idea what that line does, which is why JFrame.EXIT_ON_CLOSE is used.



36
Computer Programming / Re: [Java] Key Events and Multiple Classes
« on: May 24, 2011, 10:42:55 pm »
what's wrong with your code is that everytime you press the right key, you make a new Squared object, set it's value to 10, and then you draw the frame. if you did mySquared.Draw(), you would see a new JFrame pop up everytime you press the right arrow key. i'm not certain how to fix this existing code. i would scrap it.

This is how i would do what you're trying:
you'll want to have a class which extends JPanel, implements KeyListener, and overrides the method public void paintComponent(Graphics g); paintComponent is where you'll have the code to draw the Squared. then you'll have a class called Square which holds the data for the square. the class which extends JPanel should have a JFrame and Square as instance variables. after this, whenever you press the right arrow key, update the Squared object, and call repaint();

37
TI Z80 / Re: Emerald Programming Language / VixenVM
« on: May 14, 2011, 04:19:52 pm »
i'd suggest getting rid of further and /further and replacing them with something else (curly braces, parens). this might just be me but i don't find them aesthetically pleasing. also, i'm not sure how much room there is on the PRIZM's screen.
would all of this fit on one line?
Code: [Select]
define action call rien (boolean, value) further

it may be annoying to read if it's split up.

38
Computer Programming / Re: [Java] FlowLayout issues
« on: May 07, 2011, 12:12:43 pm »
Me = super-Java-optimizer
(I could get rid of almost all the if statements if I wanted to by using an array of buttons, but I don't feel like it)

Code: [Select]
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class rockpaperscissors extends JFrame implements ActionListener {

String[] messages = {"You win!", "You lose!", "Try again!"};
String[] TypeNames = {"Rock", "Paper", "Scissors"}; //didn't have a better name

//rock = 0, paper = 1, scissors = 2
//the first index corresponds to the one you chose, so if you chose rock, and the computer chose
//paper, it would be referenced as comparisons[0][1]
//it's a byte array to save a tiny bit of ram :)
//the numbers in the brackets correspond to the index in the messages array
byte[][] comparisons ={
{2, 1, 0}, //rock
{0, 2, 1}, //paper
{1, 0, 2}, //scissors
};

Random rpc = new Random();

JButton rock = new JButton("Rock");
JButton paper = new JButton("Paper");
JButton scissors = new JButton("Scissors");
JLabel youchoose = new JLabel("You choose:");
JLabel compchoose = new JLabel("The computer chooses:");
JLabel message = new JLabel("You win!");
JTextArea yourchoice = new JTextArea(1,10);
JTextArea compchoice = new JTextArea(1,10);
JLabel rockpiclbl, paperpiclbl, scissorspiclbl;
Container contentArea;

public rockpaperscissors() {

contentArea = getContentPane();
contentArea.setBackground(Color.cyan);
FlowLayout manager = new FlowLayout(FlowLayout.CENTER,50,50);
contentArea.setLayout(manager);
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
rock.setEnabled(true);
paper.setEnabled(true);
scissors.setEnabled(true);
contentArea.add(rock);
contentArea.add(paper);
contentArea.add(scissors);
contentArea.add(youchoose);
contentArea.add(yourchoice);
contentArea.add(compchoose);
contentArea.add(compchoice);
contentArea.add(message);
setContentPane(contentArea);
}
public void actionPerformed(ActionEvent event) {
int randint = rpc.nextInt(3); //0 = rock, 1 = paper, 2 = scissors

compchoice.setText( TypeNames[randint] );

//rock beats scissors, scissors beats paper, paper beats rock
try {

Toolkit toolkit = Toolkit.getDefaultToolkit();
        MediaTracker tracker = new MediaTracker(this);
        Image rockpic = toolkit.createImage("rock.jpg");
        Image paperpic = toolkit.createImage("paper.jpg");
        Image scissorspic = toolkit.createImage("scissors.jpg");



if(event.getSource()==rock) {  //if you push rock...
yourchoice.setText("Rock!");
tracker.addImage(rockpic, 1);
tracker.waitForID(1);
message.setText( messages[ comparisons[0][randint] ] );
}
if(event.getSource()==paper) {  //if you push paper...
tracker.addImage(paperpic, 2);
tracker.waitForID(2);
yourchoice.setText("Paper!");
message.setText( messages[ comparisons[1][randint] ] );
}
if(event.getSource()==scissors) { //if you push scissors...
tracker.addImage(scissorspic, 3);
tracker.waitForID(3);
yourchoice.setText("Scissors!");
message.setText( messages[ comparisons[2][randint] ] );
}
}
catch(InterruptedException ie)
        {
            System.out.println("Error: " + ie.getMessage());
        }
}
public static void main(String[]args){
rockpaperscissors GUI = new rockpaperscissors();
GUI.setTitle("Rock Paper Scissors!");
GUI.setSize(1000,1000);
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.pack();
GUI.setVisible(true); //Its not invisible!
}
}

using byte arrays is rarely worth the trouble because you have to cast bytes to ints and ints to bytes, which takes up cycles. the only place i've encountered where using a byte array is necessary would be IO with files.

also, a different way to get an image:

Code: [Select]
import java.awt.image.*;
import javax.imageio.ImageIO;

Image img = (Image) ImageIO.read(new File("tornado.jpg"));
//looks for a jpeg image titled "tornado" in the same folder as the .class file of the class.
//you can also use paths, such as "C:\\Documents and Settings\\My Documents"..etc


and as to your code, i'm not certain what's wrong with it, but i'm willing to bet it's a GUI problem. i'd suggest having 1 JLabel object instead of 3 for when you lose/win/tie. then just use setText() to say the correct message.

39
Computer Programming / Re: [Java] FlowLayout issues
« on: May 06, 2011, 06:28:29 pm »
ah. right. i forgot about that. make a constructor for the rockpaperscissors class and that won't happen

40
Computer Programming / Re: [Java] FlowLayout issues
« on: May 06, 2011, 06:12:32 pm »
Code: [Select]
public static void main(String[]args){
rockpaperscissors GUI = new rockpaperscissors();
init(); //this is all i added
GUI.setTitle("Rock Paper Scissors!");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.pack();
GUI.setVisible(true);
}

although this will work, i'd suggest making a constructor for the rockpaperscissors class and just copy/pasting the code in init() into the constructor

41
Computer Programming / Re: [Java] FlowLayout issues
« on: May 06, 2011, 06:07:25 pm »
you need to call init();

by the way, have you taken the AP Computer Science exam? or are you in a different course?

42
Computer Programming / Re: [java help] concatenation?
« on: April 13, 2011, 08:54:01 pm »
here's how i would do it:
Code: [Select]
line | stars | spaces
0      1       4
1      3       3
2      5       2
3      7       1
4      9       0
make two linear equations from this:
stars = line * 2 + 1;
spaces = 4 - line;
and then use them in the inner for-loop headers.
Code: [Select]
for(int line = 0; line < 5; line++){
    for(int spaces = 0; spaces < 4 - line; spaces++)
        System.out.print(" ");
    for(int stars = 0; stars < line * 2 + 1; stars++)
        System.out.print("*");
    System.out.println();
}

to use concatenation like you're trying to do, just start a String star as "*", and every new line concatenate "**".
Code: [Select]
String stars = "*";
for(int line = 0; line < 5; line++){
    for(int spaces = 0; spaces < 4 - line; spaces++)
        System.out.print(" ");
    System.out.print(stars);
    stars += "**";
    System.out.println();
}

yes, i realize the for loop for spaces can be optimized. i was just showing how i used my linear equations in the code.

43
Computer Programming / Re: [java] help!
« on: April 03, 2011, 08:29:50 pm »
well deep thought's code breaks after it reaches a negative number, so if you did 10,90,6,-5,12,5 it would calculate 10, 90, and 6 to get 10+90+6=106/3=35.333....


if you did 10,90,6,-5 shouldn't 6 be dropped, and you end up with 100/2 = 50?

44
Computer Programming / Re: [java] help!
« on: March 30, 2011, 09:10:55 pm »
Code: [Select]
int sum = 0;
int numScores = 0;
int lowest = 101; //assuming 100 is the highest score possible
while(true){
    String temp = JOptionPane.showInputDialog("Enter test score (negative to exit): ");
    int score = Integer.parseInt(temp);
    if(score < 0)
        break;
    numScores++;
    if(score < lowest)
        lowest = score;
    sum += score;
}
System.out.println("Average: " + ((sum - lowest) / (numScores - 1)));

should work, unless you don't enter any scores, in which case you'll get -101 (if i went through that right).
You can get all your information from those three things. You shouldn't need any loops for this.

you do need one loop... unless you want to write it recursively? just for the challenge:

Code: [Select]
public static void main(String[] args){
    int[] answers = recursion(0, 101, 0);
    System.out.println("Average: " + ((answers[2] - answers[1]) / (answers[0] - 1)));
}
public static int[] recursion(int numScores, int lowest, int sum){
    String temp = JOptionPane.showInputDialog("Enter test score (negative to exit): ");
    int score = Integer.parseInt(temp);
    if(score < 0)
        return new int[] {numScores, lowest, sum};
    return recursion(numScores + 1, score < lowest ? score : lowest, sum + score);
}

pretty sure the first one works, not sure about the second.

       

45
TI-BASIC / Re: Sprite to Hex Routines
« on: March 27, 2011, 01:35:13 pm »
I reversed the bit order, so 0001 <=> 1000, 0010 <=> 0100, etc.

This was done to get rid of the (3-C).

(I do believe that you can perform a similar optimization to drop 4 bytes off the display-and-forget version, as well.)

yeah i was too lazy to switch that around when i wrote the original 92 byte one (it's on cemetech, probably posted about 6 months ago)

Pages: 1 2 [3] 4 5 ... 82