import java.util.*;
import java.lang.*;
public class Prog508a
{
public static Scanner k=new Scanner(System.in);
public static int yesorno=1;
public static String YN="";
public static int yncheck(String YesOrNo)
{
if(YN.equals("Yes"))
yesorno=1;
else if(YN.equals("No"))
yesorno=0;
else if( !(YN.equals("Yes") || YN.equals("No")) && (YN.equalsIgnoreCase("yes") || YN.equalsIgnoreCase("no")))
{
System.out.print("Capitalization is important Yes or No? ");
YN=k.nextLine();
yncheck(YN);
}
else
{System.out.print("You must answer either Yes or No? ");
YN=k.nextLine();
yncheck(YN);
}
return yesorno;
}
public static void main(String args[])
{
String str="",str2="",str3="";
while(yesorno==1){
System.out.println();
System.out.print("Enter a sentence: ");
str=k.nextLine();
String[] sarray = str.split(" ");
int len = sarray.length;
for(int i=0;i<len;i++)
{
char[] word = sarray[i].toCharArray();
char firstletter = word[0];
int len3=word.length;
int len4=len3-1;
if(firstletter!='a' && firstletter!='e' && firstletter!='i' && firstletter!='o' && firstletter!='u' && firstletter!='A' && firstletter!='E' && firstletter!='I' && firstletter!='O' && firstletter!='U')
{
for(int j=0;j<=len4-1;j++)
{
word[j]=word[j+1];
}
word[len3-1]=firstletter;
}
for(int l=0;l<=len3-1;l++)
{
str2=str2+word[l];
}
str3=str3+str2+" ";
str2="";
}
str3=str3.replace(" ","ay ");
System.out.println(str3);
str3="";
System.out.print("Do you wish to convert another sentence? (Yes/No) ");
YN=k.nextLine();
yncheck(YN);
}
}
}
*/
Optimizing comments thnx
import java.util.*;
public class Censor{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter a string to censor: ");
String evilness = reader.nextLine();
System.out.println("Your string in a more holy format: \n" + censor(evilness));
System.out.print("\nAgain? (y/n) ");
if(reader.nextLine().equalsIgnoreCase("y"))
main(null); //recursive calls to main? i think yes.
}
public static String censor(String evilness){
String[] badWords = {"fuck","shit","damn","ass","FUCK","SHIT","DAMN","ASS"}; //yes, i know there are more swear words. this covers the basics.
String[] replacementWords = {"apple","magical","unicorn","fairies","endear","renowned","butterflies","sugar"};
Random r = new Random();
for(int i = 0; i < badWords.length; i++){
int indexOfBadWord = evilness.indexOf(badWords[i]);
if(indexOfBadWord != -1){
String begin = evilness.substring(0,indexOfBadWord);
String replacement = replacementWords[r.nextInt(replacementWords.length)];
String end = evilness.substring(indexOfBadWord + badWords[i].length());
evilness = begin + replacement + end;
i = -1;
}
}
return evilness; //now holy.
}
}
Lol, "apple this magical unicorn". Interesting program, nemo. I love the comment on that return statement.
EDIT: What would be really funny, is to analyze the sentence, and get a new version that was "prettier" so that everyone talked pretty.
So "Hey, I think your program is a fail" becomes "I think your program is missing some butterflies, but other than that, I think the unicorns were happy."
I'm sure you are referring to replacing Game with Lost, right?Lol, "apple this magical unicorn". Interesting program, nemo. I love the comment on that return statement.
EDIT: What would be really funny, is to analyze the sentence, and get a new version that was "prettier" so that everyone talked pretty.
So "Hey, I think your program is a fail" becomes "I think your program is missing some butterflies, but other than that, I think the unicorns were happy."
i think that's a little beyond my word processing abilities. it'd be cool if i made an advanced library to process strings, though.
also, if you want, you can just copy/paste my source code and add words. if you want to add the swear words list to include "snow" for some reason, you can do that. you can also add replacement words, if you wanted to replace snow with honey.
maybe i could make a function that, when a certain word is spotted, it will always replace it with a certain other word...
interesting. you know what i just thought of doing? censoring a string to take out all the swear wordsHaha this is awesome. ;D
edit: just finished it. it will change something like this:intoSpoiler For language:
You endearing unicorn.Code: [Select]import java.util.*;
public class Censor{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("Enter a string to censor: ");
String evilness = reader.nextLine();
System.out.println("Your string in a more holy format: \n" + censor(evilness));
System.out.print("\nAgain? (y/n) ");
if(reader.nextLine().equalsIgnoreCase("y"))
main(null); //recursive calls to main? i think yes.
}
public static String censor(String evilness){
String[] badWords = {"fuck","shit","damn","ass","FUCK","SHIT","DAMN","ASS"}; //yes, i know there are more swear words. this covers the basics.
String[] replacementWords = {"apple","magical","unicorn","fairies","endear","renowned","butterflies","sugar"};
Random r = new Random();
for(int i = 0; i < badWords.length; i++){
int indexOfBadWord = evilness.indexOf(badWords[i]);
if(indexOfBadWord != -1){
String begin = evilness.substring(0,indexOfBadWord);
String replacement = replacementWords[r.nextInt(replacementWords.length)];
String end = evilness.substring(indexOfBadWord + badWords[i].length());
evilness = begin + replacement + end;
i = -1;
}
}
return evilness; //now holy.
}
}
interesting. you know what i just thought of doing? censoring a string to take out all the swear words
edit: just finished it. it will change something like this:intoSpoiler For language:
You endearing unicorn.
Not public commands, though. Earlier I had to mute the IRC channel (disable it completely) to stop a spam fest because no one would listen. Imagine with bot commands, now. X.x
import java.util.*;
public class randomcalc
{
public static void main(String args[])
{
Random generator = new Random();
int r = generator.nextInt(20);
System.out.println("yeongJIN_COOL's random calc gen");
System.out.println("You get...");//WHAT YOU'RE GONNA GET?
switch(r)
{
case 0: System.out.println("TI-81");break;
case 1: System.out.println("TI-83+");break;
case 2: System.out.println("TI-84+");break;
case 3: System.out.println("TI-83+ SE");break;
case 4: System.out.println("TI-84+ SE");break;
case 5: System.out.println("TI-86");break;
case 6: System.out.println("TI-89");break;
case 7: System.out.println("TI-89 Titanium");break;
case 8: System.out.println("TI-92");break;
case 9: System.out.println("Voyage 200");break;
case 10: System.out.println("TI-32 stuff");break;
case 11: System.out.println("f(x)-9750g");break;
case 12: System.out.println("cf(x)-9750g");break;//AT LEAST, IT HAS COLOR
case 13: System.out.println("TI-MICROWAVE");break;//ROFL
case 14: System.out.println("CASIO PRIZM");break;
case 15: System.out.println("TI-nSPIRE with Clickpad");break;
case 16: System.out.println("TI-nSPIRE with Touchpad");break;
case 17: System.out.println("TI-nSPIRE CAS");break;
case 18: System.out.println("Wabbitemu");break;
case 19: System.out.println("Nothing");break;//TOO BAD FOR YOU
}
}
}
import java.util.Scanner;
public class Solve{
static double[][] matrix;
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
System.out.print("# of variables: ");
int vars = reader.nextInt();
inputValues(vars,vars+1);
for(int col = 0; col < vars; col++){
for(int row = 0; row < vars; row++){
if(row==0)
matrix[col] = rowDiv(matrix[col],matrix[col][col]);
if(row!=col)
matrix[row] = rowAdd(rowDiv(matrix[col],-1/matrix[row][col]),matrix[row]);
}
}
for(int i = 0; i < vars; i++)
System.out.println("xyzuvw".substring(i,i+1) + "= " + Math.round(matrix[i][vars]*1000)/1000.0);
}
public static void inputValues(int rows, int columns){
String equ = "Ax+By+Cz+Du+Ev+Fw".substring(0,rows*3-1) + "=" + "ABCDEFG".substring(rows,rows+1);
matrix = new double[rows][columns];
Scanner reader = new Scanner(System.in);
for(int r = 0; r < rows; r++){
System.out.println("Enter equation " + (r+1) + ": \n" + equ);
for(int c = 0; c < columns; c++){//C++
System.out.print("ABCDEFG".substring(c,c+1)+"= ");
matrix[r][c] = reader.nextInt();
}
System.out.println();
}
}
public static double[] rowDiv(double[] r, double c ){
double[] temp = new double[r.length];
for(int p = 0; p < r.length; p++)
temp[p] = r[p] / c;
return temp;
}
public static double[] rowAdd(double[] i, double[] j){
double[] temp = new double[i.length];
for(int p = 0; p < i.length; p++)
temp[p] = i[p] + j[p];
return temp;
}
}
lol that's not simple :PAww, ok then D:
I don't even know how to put graphic on java...
:(
I don't even know how to put graphic on java...Me too.
:(
isn't JPanel and JFrame Applet stuff?
//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!
}
}
//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!
}
}
//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.
//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 :)
}
}
//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);
}
}
//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
}
}
{0171,0157,0165,040,0150,0141,0166,0145,040,0154,0157,0163,0164,040,0164,0150,0145,040,0147,0141,0155,0145,056}
you have lost the game.
import java.util.*;
public class Array{
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
String str = reader.nextLine();
String obfuscation = "{";
for(int i = 0; i < str.length(); i++){
obfuscation += "0" + Integer.toOctalString((int)str.charAt(i));
if(i + 1 != str.length())
obfuscation += ",";
}
obfuscation += "}";
System.out.println("encoded: " + obfuscation);
}
}
public class Array2{
public static void main(String[] args){
int[] ints = <output from first program>;
for(int i: ints)
System.out.print((char)i);
}
}
import java.util.*;
public class Insult{
static Random r = new Random();
public static void main(String[] args){
System.out.println(randomStart() + randomAdj() + randomNoun() + "you " + randomPersonAdj() + randomInsult() + ". " + randomEnd());
Scanner reader = new Scanner(System.in);
System.out.println("Want another opinion? (y/n)");
if(reader.nextLine().equalsIgnoreCase("y"))
main(null);
}
public static String randomStart(){
String[] starts = {"What is this ","Why would you leave this ",};
return starts[r.nextInt(2)];
}
public static String randomAdj(){
String[] adj = {"stupid ","blatant ","moronic ","abominable "};
return adj[r.nextInt(4)];
}
public static String randomNoun(){
String[] noun = {"piece of garbage ","filth ","waste "};
return noun[r.nextInt(3)];
}
public static String randomPersonAdj(){
String[] adj = {"deficient ","senseless ","rash ","puerile ","nonsensical ","thick headed ","fat ","pointless ","obtuse "};
return adj[r.nextInt(9)];
}
public static String randomInsult(){
String[] insults = {"crud of the gene pool","failure","waste of breath","imbecile","incompetent monkey"};
return insults[r.nextInt(5)];
}
public static String randomEnd(){
String[] ends = {"Get lost.","Go climb a mountain.","Why don't you leave?","Just stop. Stop it now.","Seriously? Vamoose.","The Game."};
return ends[r.nextInt(6)];
}
}
import javax.swing.*;
public class HelloWorldSwing
{
public static void main(String args[])
{
JFrame frame = new JFrame("Hello");
JLabel label = new JLabel("Hello, Swing World");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
What's JFrame? Is it a derivative of java or is it a library, like JQuery for JS?
you can also use AWT. Java has two GUI packages. Swing generally being preferred over AWT.
simple program that allows you to move a square around a 10 x 10 map. it's about 200 lines of code so i didn't post it here. don't be scared though, 60 lines of the 200 are just keyboard action events. you'll need to download the images and put them in the same directory as the source.
simple program that allows you to move a square around a 10 x 10 map. it's about 200 lines of code so i didn't post it here. don't be scared though, 60 lines of the 200 are just keyboard action events. you'll need to download the images and put them in the same directory as the source.
Needs compiling, right?
// Imports
import javax.swing.*;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) {
JFrame frame= new JFrame("Random Number generator");
JPanel panel=new JPanel();
final Random generator = new Random();
final int randomNumber = generator.nextInt();
final JTextArea textArea = new JTextArea(""+randomNumber,5,20);
JLabel title = new JLabel("Random Number generator");
JLabel empty = new JLabel(" "
+ " "
+ " ");
JLabel programBy = new JLabel(" by David Gomes");
JButton generate = new JButton("Generate");
generate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
final Random generator = new Random();
final int randomNumber = generator.nextInt();
textArea.setText(""+randomNumber);
}
});
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
System.exit(1);
}
});
frame.add(panel);
panel.add(title);
panel.add(textArea);
panel.add(generate);
panel.add(quitButton);
panel.add(empty);
panel.add(programBy);
frame.setSize(300,220);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);;
}
}
awesome! I'm keep learning... :)
Yes, I do.
In fact I'm learning it right now(APCS)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;;
public class ColorChooserTest extends JFrame{
public ColorChooserTest(){
setSize(400,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("hi");
ColorChooser panel = new ColorChooser();
getContentPane().add(panel);
setVisible(true);
while(true)
panel.repaint();
}
public static void main(String[] args){
new ColorChooserTest();
}
}
class ColorChooser extends JPanel implements MouseMotionListener,MouseListener{
private final int SIZE = 200;
private BufferedImage gradient = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_4BYTE_ABGR);
private BufferedImage hueStrip = new BufferedImage(15, SIZE, BufferedImage.TYPE_4BYTE_ABGR);
private Rectangle hueRect = new Rectangle(SIZE + 20,10,15,SIZE);
private Rectangle gradientRect = new Rectangle(10,10,SIZE,SIZE);
private float hue = 0;
private Color selectedColor = Color.RED;
public ColorChooser(){
addMouseListener(this);
addMouseMotionListener(this);
setFocusable(true);
generateHueStrip();
}
public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
generateGradient();
g2d.drawImage(gradient,null,10,10);
g2d.drawImage(hueStrip,null,SIZE + 20,10);
g2d.setColor(selectedColor);
g2d.fillRect(10,SIZE+20,30,30);
}
private void generateHueStrip(){
for(int i = 0; i < SIZE; i++){
Color hsb = Color.getHSBColor((float)(i/(double)SIZE),(float) 1.0,(float) 1);
for(int k = 0; k < 15; k++)
hueStrip.setRGB(k,i,hsb.getRGB());
}
}
private void generateGradient(){
for(int saturation = SIZE-1; saturation >= 0; saturation--){
for(int value = SIZE-1; value >= 0; value--){
Color hsb = Color.getHSBColor(hue, (float) saturation / SIZE, (float) value / SIZE);
gradient.setRGB(value, SIZE - 1 -saturation, hsb.getRGB());
}
}
}
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if(hueRect.contains(x,y)){
x -= SIZE + 20;
y -= 10;
Color rgb = new Color(hueStrip.getRGB(x,y));
int red = rgb.getRed();
int blue = rgb.getBlue();
int green = rgb.getGreen();
hue = Color.RGBtoHSB(red, green, blue, null)[0];
}
else
if(gradientRect.contains(x,y))
selectedColor = new Color(gradient.getRGB(x-10,y-10));
}
public void mouseDragged(MouseEvent e) {
mouseClicked(e);
}
public void mouseMoved(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e){}
}
I love everything about Java, except that the source is VERY insecure.
True, or you could write classes that are like the programs that read their own source code, but instead, they decrypt themselves into a program that compiles the real program that then runs. It only takes 10 days to run a hello world program, but hey, it's secure, right? ;-)I love everything about Java, except that the source is VERY insecure.
you can always just bundle everything up in a couple meaningless wrapper classes, have one-letter var names, method names and classes. and make all your classes implement random interfaces completely off-base to their real purpose. define a final constant True as 0, and False as 1. write comments that lie. of course this really just bloats your code and makes it unmaintainable.. BUT, no one's going to pick through it.
package javaapplication1;
import java.awt.Color;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* NewJFrame.java
*
* Created on 10/Fev/2011, 0:01:33
*/
/**
*
* @author david
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jColorChooser1 = new javax.swing.JColorChooser();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Text Editor");
jTextField2.setForeground(jColorChooser1.getColor());
jButton1.setText("UPDATE");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jMenu1.setText("File");
jMenuItem1.setText("New");
jMenu1.add(jMenuItem1);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jColorChooser1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(jButton1))
.addComponent(jColorChooser1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
this.jTextField2.setText("");
this.jTextField2.setForeground(this.jColorChooser1.getColor());
)
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JColorChooser jColorChooser1;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1;
public class Main {
public static void main(String[] args) {
NewJFrame form = new NewJFrame();
form.setVisible(true);
}
}
package randomnumbergame;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter difficulty (from 0 to 1000): ");
Scanner difficulty = new Scanner(System.in);
int difficultyLevel = difficulty.nextInt();
if (difficultyLevel<0 || difficultyLevel>1000)
{
System.out.println("Invalid Difficulty");
}
System.out.println(difficulty.nextLine());
Random generator = new Random();
int randomNumber = generator.nextInt(difficultyLevel);
boolean gotIt = false;
while (gotIt == false)
{
Scanner attempt = new Scanner(System.in);
int attemptNumber = attempt.nextInt();
if (attemptNumber == randomNumber)
{
gotIt = true;
}
else
{
if (attemptNumber > randomNumber)
{
System.out.println("The secret number is smaller... ");
}
else
{
System.out.println("The secret number is highter... ");
}
}
}
System.out.println("You won!");
}
}
I'd avoid creating a scanner each and every time you need to read something - once is enough. :)
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :PYou shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.
anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
package randomnumbergame;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter difficulty (from 0 to 1000): ");
Scanner inputScanner = new Scanner(System.in);
int difficultyLevel = inputScanner.nextInt();
if (difficultyLevel<0 || difficultyLevel>1000)
{
System.out.println("Invalid Difficulty");
}
System.out.println(inputScanner.nextLine());
Random generator = new Random();
int randomNumber = generator.nextInt(difficultyLevel);
boolean gotIt = false;
while (gotIt == false)
{
int attemptNumber = inputScanner.nextInt();
if (attemptNumber == randomNumber)
{
gotIt = true;
}
else
{
if (attemptNumber > randomNumber)
{
System.out.println("The secret number is smaller... ");
}
else
{
System.out.println("The secret number is highter... ");
}
}
}
System.out.println("You won!");
}
}
package randomnumbergame;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Enter difficulty (from 0 to 1000): ");
Scanner inputScanner = new Scanner(System.in);
int difficultyLevel = inputScanner.nextInt();
if (difficultyLevel<0 || difficultyLevel>1000)
{
System.out.println("Invalid Difficulty");
sys.exit(0);
}
System.out.println(inputScanner.nextLine());
Random generator = new Random();
int randomNumber = generator.nextInt(difficultyLevel);
boolean gotIt = false;
while (gotIt == false)
{
int attemptNumber = inputScanner.nextInt();
if (attemptNumber == randomNumber)
{
gotIt = true;
}
else
{
if (attemptNumber > randomNumber)
{
System.out.println("The secret number is smaller... ");
}
else
{
System.out.println("The secret number is highter... ");
}
}
}
System.out.println("You won!");
}
}
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :PYou shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.
anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
int difficultyLevel = inputScanner.nextInt();
while (difficultyLevel<0 || difficultyLevel>1000)
{
System.out.println("Invalid Difficulty");
difficultyLevel = inputScanner.nextInt();
}
:)
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :PYou shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.
anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
well, I'm talking simple 3D, like minecraft 3D.
Well, still - you'd better be off learning C++ IMO. ;)Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :PYou shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.
anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
well, I'm talking simple 3D, like minecraft 3D.
I'd do:Code: [Select]int difficultyLevel = inputScanner.nextInt();
:)
while (difficultyLevel<0 || difficultyLevel>1000)
{
System.out.println("Invalid Difficulty");
difficultyLevel = inputScanner.nextInt();
}
Hello, I want to use java at home, and I'm trying to download eclipse right now, unless if you guys think netbeans is better :PYou shouldn't be using Java for anything 3D - it's just too slow/memory-intensive for that. That's my opinion though.
anyways, I suck at knowing all of these things how to install libraries and such, anyone know of a simple graphics library that can do 3D, and is easy to install?
well, I'm talking simple 3D, like minecraft 3D.
Even though... It's just so complicated, you shouldn't start with it, never.
import java.util.*;
public class Convert
{
public static String convertbase(int n, int base) {
// special case
if (n == 0) return "0";
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s = "";
while (n > 0) {
int d = n % base;
s = digits.charAt(d) + s;
n = n / base;
}
return s;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number base 10 to convert: ");
int original = keyboard.nextInt();
System.out.print("Enter the new base: ");
int base = keyboard.nextInt();
String answer = convertbase(original,base);
System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]");
}
}
import java.util.*;
public class Convert
{
public static String convertbase(int n, int base) {
// special case
if (n == 0) return "0";
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s = "";
while (n > 0) {
int d = n % base;
s = digits.charAt(d) + s;
n = n / base;
}
return s;
}
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number base 10 to convert: ");
int original = keyboard.nextInt();
System.out.print("Enter the new base: ");
int base = keyboard.nextInt();
String answer = convertbase(original,base);
System.out.println("The number "+original+"[base 10] = "+answer+"[base "+base+"]");
}
}
david@DavidPC:~/Documentos/Java$ javac Convert.java
david@DavidPC:~/Documentos/Java$ java Convert
Enter the number base 10 to convert: 65535
Enter the new base: 16
The number 65535[base 10] = FFFF[base 16]
david@DavidPC:~/Documentos/Java$ java Convert
Enter the number base 10 to convert: 255
Enter the new base: 2
The number 255[base 10] = 11111111[base 2]
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner intRead = new Scanner(System.in);
Scanner strRead = new Scanner(System.in);
System.out.print("Enter a number to convert: ");
String src = strRead.nextLine();
System.out.print("Enter the number's base: ");
int oldBase = intRead.nextInt();
System.out.print("Enter the desired base: ");
int newBase = intRead.nextInt();
String ans = Integer.toString(Integer.valueOf(src, oldBase), newBase);
System.out.println(src + " in base " + oldBase + " is " + ans + " in base " + newBase);
}
}