0 Members and 2 Guests are viewing this topic.
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, JFrameimport java.awt.*; // for Color, and Container panepublic 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, JFrameimport java.awt.*; // for Color, and Container paneimport 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. }}
//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, JFrameimport java.awt.*; // for Color, and Container paneimport 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, JFrameimport java.awt.*; // for Color, and Container paneimport java.awt.event.*; //needed for the key movementimport 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, JFrameimport java.awt.*; // for Color, and Container paneimport java.awt.event.*; //needed for the key movementimport 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 }}