Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Yeong

Pages: 1 ... 248 249 [250] 251 252 ... 276
3736
now how do I display text on JPanel?

3737
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

}

}

3738
oh it doesn't let you move multiple objects
Yeong had tried to make the same smile move

3739
 :w00t:moving circle :w00t:
EDIT: What is the "move left!", "move right!" thingy? is it supposed to display?

3740
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 :)
}

}

3741
@nemo: question: how do i view applet?

3742
Gaming Discussion / Re: Maplestory
« on: December 13, 2010, 07:22:21 pm »
Gosh Maplestory became so easy to lvl up now
*New class comming out at Dec 20th*

3743
oh
Mah Savitch book gives me a example about those but it was always in the applet section.

3744
Computer Projects and Ideas / Re: Trio and Niko: Falling
« on: December 13, 2010, 07:19:31 pm »
Quote
Wow this looks fucking great! Awesome job in those maps and sprites.
We can use these words in this forum? ???

3745
isn't JPanel and JFrame Applet stuff?

3746
Other Calculators / Re: What do you prefer to program in on z80 calcs?
« on: December 13, 2010, 07:16:16 pm »
what is brainf thingy?

3747
I use BlueJ because I AM in APCS
only shortcut I learned in there was ctrl+k, which was compiling

3748
Other Calculators / Re: What do you prefer to program in on z80 calcs?
« on: December 13, 2010, 07:13:09 pm »
BASIC
(Yeong is not familiar with Axe yet and not even tried ASM)

3749
TI Z80 / Re: YEONG RPG(HALTED UNTIL AFTER EOC)
« on: December 13, 2010, 07:11:01 pm »
Imma post some info up here even though I paused the project for few weeks:

-There will be one secret character that you cn get by doing all the quest in entire game before final boss.

-Yeong and his party will save the world against the evil witch Malore.

-The dream that Yeong had at intro screenie is related to some serious event

3750
Computer Projects and Ideas / Re: Trio and Niko: Falling
« on: December 13, 2010, 07:04:26 pm »
if it is, nice idea
New Year post

Pages: 1 ... 248 249 [250] 251 252 ... 276