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

0 Members and 1 Guest are viewing this topic.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #60 on: December 19, 2010, 05:03:04 pm »
you can also use AWT. Java has two GUI packages. Swing generally being preferred over AWT.


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #61 on: December 19, 2010, 06:40:53 pm »
you can also use AWT. Java has two GUI packages. Swing generally being preferred over AWT.

I've always used JFrame, so never heard of others.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #62 on: December 19, 2010, 07:31:50 pm »
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.
« Last Edit: December 19, 2010, 07:32:12 pm by nemo »


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #63 on: December 19, 2010, 07:32:34 pm »
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?

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #64 on: December 19, 2010, 07:33:00 pm »
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?

yes. but i can attach an executable jar if you want


Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #65 on: December 20, 2010, 03:15:32 pm »
I thought that that random number generator was weak and made a new one, much more attractive. Attached is a .jar file and here is a screenshot and the code:



Code: [Select]
// 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);;
    }
}

Tell me what you think of it :)

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 #66 on: December 27, 2010, 08:09:38 am »
awesome! I'm keep learning... :)
Sig wipe!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #67 on: December 27, 2010, 08:28:36 am »
awesome! I'm keep learning... :)

Do you like Java?

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 #68 on: December 27, 2010, 08:29:45 am »
Yes, I do.
In fact I'm learning it right now(APCS)
Sig wipe!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #69 on: December 27, 2010, 10:35:21 am »
Yes, I do.
In fact I'm learning it right now(APCS)

That's great. Thanks for waking up this post :)

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #70 on: December 30, 2010, 11:12:06 pm »
ColorChooser! it has more range than MS Paint, which really isn't saying much because the actual color-oriented math is done by java. change the final int SIZE in ColorChooser to make the size larger.

here's the source:

Code: [Select]
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){}
}
« Last Edit: December 30, 2010, 11:13:15 pm by nemo »


Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #71 on: December 30, 2010, 11:56:53 pm »
I love everything about Java, except that the source is VERY insecure.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #72 on: December 31, 2010, 12:12:44 am »
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.


Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #73 on: December 31, 2010, 05:07:31 pm »
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.
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? ;-)

But seriously, Java has it's uses and I like it for quite a few things. The fact that I can always get the source of a jar file within two clicks is one of them.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Random simple java programs that actually do something useful.
« Reply #74 on: February 10, 2011, 01:58:56 pm »
Code: [Select]
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

}

Code: [Select]
/*
 * 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);

       
    }

}

The second one is Main.java and the first one is NewJFrame.java.

The thing is I'm using Color Chooser under Swing Windows, it's automatic.