0 Members and 2 Guests are viewing this topic.
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.
Quote from: nemo on December 19, 2010, 07:31:50 pmsimple 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?
// Importsimport 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.
Quote from: Binder News on December 30, 2010, 11:56:53 pmI 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); }}