Author Topic: Random simple java programs that actually do something useful.  (Read 34814 times)

0 Members and 5 Guests are viewing this topic.

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #30 on: December 13, 2010, 07:18:38 pm »
isn't JPanel and JFrame Applet stuff?
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #31 on: December 13, 2010, 07:19:29 pm »
isn't JPanel and JFrame Applet stuff?

nope, it's called Swing. it allows you to have little windows on your computer. i think i should give multiple examples....


Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #32 on: December 13, 2010, 07:20:52 pm »
oh
Mah Savitch book gives me a example about those but it was always in the applet section.
Sig wipe!

Offline FinaleTI

  • Believe in the pony that believes in you!
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1830
  • Rating: +121/-2
  • Believe in the pony that believes in you!
    • View Profile
    • dmuckerman.tumblr.com
Re: Random simple java programs that actually do something useful.
« Reply #33 on: December 13, 2010, 07:27:31 pm »
Ooh, I didn't know that one.

I like Ctrl+Shift+I, because it auto-indents your code, which can be really helpful sometimes.


Spoiler For Projects:

My projects haven't been worked on in a while, so they're all on hiatus for the time being. I do hope to eventually return to them in some form or another...

Spoiler For Pokemon TI:
Axe port of Pokemon Red/Blue to the 83+/84+ family. On hold.

Spoiler For Nostalgia:
My big personal project, an original RPG about dimensional travel and a few heroes tasked with saving the world.
Coding-wise, on hold, but I am re-working the story.

Spoiler For Finale's Super Insane Tunnel Pack of Doom:
I will be combining Blur and Collision Course into a single gamepack. On hold.

Spoiler For Nostalgia Origins: Sky's Story:
Prequel to Nostalgia. On hold, especially while the story is re-worked.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #34 on: December 13, 2010, 07:34:16 pm »
Code: [Select]
//program: makes an empty window you can move around, minimize. whatever.
import javax.swing.*;

public class Example1{
public static void main(String[] args){
JFrame frame = new JFrame("Omnimaga."); //makes a new window with the title "Omnimaga." in memory.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes it so when you press the red X, the program exits.
frame.setSize(300,400); //300 pixels wide by 400 tall.
frame.setResizable(true); //it's resizable by default, but if you ever want to make it not-resizable, you know how.
frame.setVisible(true); //yay! it's visible!
}
}

example numero one.

Code: [Select]
//program: makes an empty window with a panel, which has a black background.
import javax.swing.*;  //for JPanel, JFrame
import java.awt.*; // for Color, and Container pane

public class Example2{
public static void main(String[] args){
JFrame frame = new JFrame("Omnimaga."); //makes a new window with the title "Omnimaga." in memory.
Container pane = frame.getContentPane(); //this is what organizes buttons, text boxes, panels, etc.
JPanel panel = new JPanel(); //you can draw on this! yay!
panel.setBackground(Color.BLACK); //make the background color black
pane.add(panel); //add it to the panel!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes it so when you press the red X, the program exits.
frame.setSize(300,400); //300 pixels wide by 400 tall.
frame.setResizable(true); //it's resizable by default, but if you ever want to make it not-resizable, you know how.
frame.setVisible(true); //yay! it's visible!
}
}

example nummer zwei.


3 is on its way. they're starting to get more complex.

Code: [Select]
//program: makes an empty window with a panel. oh, and the panel has a red circle in the center. yay.
import javax.swing.*;  //for JPanel, JFrame
import java.awt.*; // for Color, and Container pane
import java.awt.Graphics; //idk why, but my IDE needs this whenever i use graphics. BlueJ may not.

public class Example3{
public static void main(String[] args){
JFrame frame = new JFrame("Omnimaga."); //makes a new window with the title "Omnimaga." in memory.
Container pane = frame.getContentPane(); //this is what organizes buttons, text boxes, panels, etc.
MagicPanel panel = new MagicPanel(); //you can draw on this! yay!
pane.add(panel); //add it to the panel!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes it so when you press the red X, the program exits.
frame.setSize(300,400); //300 pixels wide by 400 tall.
frame.setResizable(true); //it's resizable by default, but if you ever want to make it not-resizable, you know how.
frame.setVisible(true); //yay! it's visible!
}
}

class MagicPanel extends JPanel{ //it's like a jpanel... but magic...

public void paintComponent(Graphics g){
g.setColor(Color.BLACK); //everything painted is black
g.fillRect(getX() , getY() , getWidth(), getHeight() );
g.setColor(Color.RED); //everything you paint will be red now.
g.fillOval(getWidth() / 2 ,getHeight() / 2, 50,50);  //oval upper-left X position is the panel's width / 2,
// y position is the panel's height / 2
//width is 50 pixels, and then height is 50 pixels.
}

}
numeri tres! you'll notice that the red circle isn't *completely* in the center of the panel. that's because of the edges of the window. if you're in XP, the extra height is +34 and the extra width is +4.
« Last Edit: December 13, 2010, 07:46:53 pm by nemo »


Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #35 on: December 13, 2010, 07:57:35 pm »
@nemo: question: how do i view applet?
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #36 on: December 13, 2010, 07:58:02 pm »
i don't know. i don't work with applets.


Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #37 on: December 13, 2010, 08:11:46 pm »
modified your example 3 a bit  :)
Code: [Select]
//program: makes an empty window with a panel. oh, and the panel has a red circle in the center. yay.
import javax.swing.*;  //for JPanel, JFrame
import java.awt.*; // for Color, and Container pane
import java.awt.Graphics; //idk why, but my IDE needs this whenever i use graphics. BlueJ may not.

public class Example3{
public static void main(String[] args){
JFrame frame = new JFrame("Omnimaga."); //makes a new window with the title "Omnimaga." in memory.
Container pane = frame.getContentPane(); //this is what organizes buttons, text boxes, panels, etc.
MagicPanel panel = new MagicPanel(); //you can draw on this! yay!
pane.add(panel); //add it to the panel!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes it so when you press the red X, the program exits.
frame.setSize(500,500); //300 pixels wide by 400 tall.
frame.setResizable(true); //it's resizable by default, but if you ever want to make it not-resizable, you know how.
frame.setVisible(true); //yay! it's visible!
}
}

class MagicPanel extends JPanel{ //it's like a jpanel... but magic...

public void paintComponent(Graphics g){
g.setColor(Color.BLACK); //everything painted is black
g.fillRect(getX() , getY() , getWidth(), getHeight() );
g.setColor(Color.RED); //everything you paint will be red now.
g.drawOval(100,50,200,200);
                         g.fillOval(155,100,10,20);
                         g.fillOval(230,100,10,20);
                          g.drawArc(150,160,100,50,180,180); // Draw smile :)
}

}

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #38 on: December 13, 2010, 08:15:26 pm »
lol a smilie. nice. here's example four. it's pretty.. badly coded. everything is declared static  <_< i wish main didn't have to be static. it's a very inelegant solution to modifier problem, but the concepts are shown.

Code: [Select]
//program: makes an empty window with a panel. oh, and the panel has a red circle in the center. yay.
import javax.swing.*;  //for JPanel, JFrame
import java.awt.*; // for Color, and Container pane
import java.awt.event.*; //needed for the key movement
import java.awt.Graphics; //idk why, but my IDE needs this whenever i use graphics. BlueJ may not.

public class Example4{
static boolean isMovingLeft = false;
static boolean isMovingRight = false;
static boolean isMovingUp = false;
static boolean isMovingDown = false;
static int x = 0, y = 0;
static Timer t; //for timed events. like every 100 milliseconds do this action
static MagicPanel panel;
public static void main(String[] args){
JFrame frame = new JFrame("Omnimaga."); //makes a new window with the title "Omnimaga." in memory.
Container pane = frame.getContentPane(); //this is what organizes buttons, text boxes, panels, etc.
panel = new MagicPanel(); //you can draw on this! yay!
InputMap i = panel.getInputMap(); //this allows you to define keystrokes the panel should recognize.
ActionMap a = panel.getActionMap(); //this allows you to transfer those keystrokes into actions.
i.put(KeyStroke.getKeyStroke("LEFT"),"move left!"); //maps the pressed key "LEFT" (left arrow key) to the string "move left!"
i.put(KeyStroke.getKeyStroke("RIGHT"),"move right!"); //like above..
i.put(KeyStroke.getKeyStroke("UP"),"move up!");
i.put(KeyStroke.getKeyStroke("DOWN"),"move down!");
i.put(KeyStroke.getKeyStroke("released LEFT"),"stop moving left!"); //maps the released key "LEFT" (left arrow key) to the string "stop moving left!"
i.put(KeyStroke.getKeyStroke("released RIGHT"),"stop moving right!");
i.put(KeyStroke.getKeyStroke("released UP"),"stop moving up!");
i.put(KeyStroke.getKeyStroke("released DOWN"),"stop moving down!");
a.put("move left!",moveLeft); //maps the string in the InputMap "move left!" to the action moveLeft.
a.put("move right!",moveRight);//like above..
a.put("move up!",moveUp);
a.put("move down!",moveDown);
a.put("stop moving left!",stopMovingLeft);
a.put("stop moving right!",stopMovingRight);
a.put("stop moving up!",stopMovingUp);
a.put("stop moving down!",stopMovingDown);
panel.setFocusable(true); //allows the panel to intercept key presses.
pane.add(panel); //add it to the panel!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes it so when you press the red X, the program exits.
frame.setSize(300,400); //300 pixels wide by 400 tall.
frame.setResizable(true); //it's resizable by default, but if you ever want to make it not-resizable, you know how.
frame.setVisible(true); //yay! it's visible!
t = new Timer(1,update); //do action update every 1 millisecond
t.start(); //start the timer
while(true){
panel.repaint();
}
}

static Action update = new AbstractAction(){ //timer's action
public void actionPerformed(ActionEvent e){
panel.updateCoordinates(x,y);
if(isMovingLeft)
x--;
if(isMovingRight)
x++;
if(isMovingUp)
y--;
if(isMovingDown)
y++;
}
};

static Action moveLeft = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingLeft = true;
}
};
static Action moveRight = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingRight = true;
}
};
static Action moveUp = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingUp = true;
}
};
static Action moveDown = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingDown = true;
}
};
static Action stopMovingLeft = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingLeft = false;
}
};
static Action stopMovingRight = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingRight = false;
}
};
static Action stopMovingUp = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingUp = false;
}
};
static Action stopMovingDown = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingDown = false;
}
};

}

class MagicPanel extends JPanel{ //it's like a jpanel... but magic...
private int x,y; //where the circle would be placed

public void updateCoordinates(int xPos, int yPos){
x = xPos;
y = yPos;
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK); //everything painted is black
g.fillRect(getX() , getY() , getWidth(), getHeight() );
g.setColor(Color.RED); //everything you paint will be red now.
g.fillOval(x ,y, 50,50);
}

}

Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #39 on: December 13, 2010, 08:16:41 pm »
 :w00t:moving circle :w00t:
EDIT: What is the "move left!", "move right!" thingy? is it supposed to display?
« Last Edit: December 13, 2010, 08:20:46 pm by yeongJIN_COOL »
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #40 on: December 13, 2010, 08:21:25 pm »
yeah it was a headache to write  :banghead:

i haven't built the framework for keypresses in so long. i kept having to refer back to my game Juggernaut.


Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #41 on: December 13, 2010, 08:22:32 pm »
oh it doesn't let you move multiple objects
Yeong had tried to make the same smile move
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #42 on: December 13, 2010, 08:23:25 pm »
no, they aren't supposed to display. the panel has two instance objects called the InputMap and the ActionMap. the inputmap is basically a bunch of strings that each correspond to a specificy keyStroke. the actionmap reads the inputmap's strings and maps the strings onto a bunch of Action objects, which run that code whenever you press the keyStroke in the inputmap.

and yes it should, if you make the smile's x and y positions be based upon x and y variables.


Offline Yeong

  • Not a bridge
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3739
  • Rating: +278/-12
  • Survivor of Apocalypse
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #43 on: December 13, 2010, 08:25:54 pm »
like this?
Code: [Select]
//program: makes an empty window with a panel. oh, and the panel has a red circle in the center. yay.
import javax.swing.*;  //for JPanel, JFrame
import java.awt.*; // for Color, and Container pane
import java.awt.event.*; //needed for the key movement
import java.awt.Graphics; //idk why, but my IDE needs this whenever i use graphics. BlueJ may not.

public class Example4{
static boolean isMovingLeft = false;
static boolean isMovingRight = false;
static boolean isMovingUp = false;
static boolean isMovingDown = false;
static int x = 0, y = 0;
static Timer t; //for timed events. like every 100 milliseconds do this action
static MagicPanel panel;
public static void main(String[] args){
JFrame frame = new JFrame("Omnimaga."); //makes a new window with the title "Omnimaga." in memory.
Container pane = frame.getContentPane(); //this is what organizes buttons, text boxes, panels, etc.
panel = new MagicPanel(); //you can draw on this! yay!
InputMap i = panel.getInputMap(); //this allows you to define keystrokes the panel should recognize.
ActionMap a = panel.getActionMap(); //this allows you to transfer those keystrokes into actions.
i.put(KeyStroke.getKeyStroke("LEFT"),"move left!"); //maps the pressed key "LEFT" (left arrow key) to the string "move left!"
i.put(KeyStroke.getKeyStroke("RIGHT"),"move right!"); //like above..
i.put(KeyStroke.getKeyStroke("UP"),"move up!");
i.put(KeyStroke.getKeyStroke("DOWN"),"move down!");
i.put(KeyStroke.getKeyStroke("released LEFT"),"stop moving left!"); //maps the released key "LEFT" (left arrow key) to the string "stop moving left!"
i.put(KeyStroke.getKeyStroke("released RIGHT"),"stop moving right!");
i.put(KeyStroke.getKeyStroke("released UP"),"stop moving up!");
i.put(KeyStroke.getKeyStroke("released DOWN"),"stop moving down!");
a.put("move left!",moveLeft); //maps the string in the InputMap "move left!" to the action moveLeft.
a.put("move right!",moveRight);//like above..
a.put("move up!",moveUp);
a.put("move down!",moveDown);
a.put("stop moving left!",stopMovingLeft);
a.put("stop moving right!",stopMovingRight);
a.put("stop moving up!",stopMovingUp);
a.put("stop moving down!",stopMovingDown);
panel.setFocusable(true); //allows the panel to intercept key presses.
pane.add(panel); //add it to the panel!
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //makes it so when you press the red X, the program exits.
frame.setSize(300,400); //300 pixels wide by 400 tall.
frame.setResizable(true); //it's resizable by default, but if you ever want to make it not-resizable, you know how.
frame.setVisible(true); //yay! it's visible!
t = new Timer(1,update); //do action update every 1 millisecond
t.start(); //start the timer
while(true){
panel.repaint();
}
}

static Action update = new AbstractAction(){ //timer's action
public void actionPerformed(ActionEvent e){
panel.updateCoordinates(x,y);
if(isMovingLeft)
x--;
if(isMovingRight)
x++;
if(isMovingUp)
y--;
if(isMovingDown)
y++;
}
};

static Action moveLeft = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingLeft = true;
}
};
static Action moveRight = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingRight = true;
}
};
static Action moveUp = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingUp = true;
}
};
static Action moveDown = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingDown = true;
}
};
static Action stopMovingLeft = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingLeft = false;
}
};
static Action stopMovingRight = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingRight = false;
}
};
static Action stopMovingUp = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingUp = false;
}
};
static Action stopMovingDown = new AbstractAction(){
public void actionPerformed(ActionEvent e){
isMovingDown = false;
}
};

}

class MagicPanel extends JPanel{ //it's like a jpanel... but magic...
private int x,y; //where the circle would be placed

public void updateCoordinates(int xPos, int yPos){
x = xPos;
y = yPos;
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK); //everything painted is black
g.fillRect(getX() , getY() , getWidth(), getHeight() );
g.setColor(Color.RED); //everything you paint will be red now.
g.drawOval(x,y,200,200);
g.fillOval(x+55,y+50,10,20);
g.fillOval(x+130,y+50,10,20);
g.drawArc(x+50,y+110,100,50,180,180); // smile :D

}

}
Sig wipe!

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #44 on: December 13, 2010, 08:27:09 pm »
yep, that should do the trick.