crwood please come(every one can come too)
hi crwood ,
thanks for your suggestion of the structure.
now i have this structure, but i don't know how to use Event class to handle a event.Do you have any exsample?
now i try this code but the event handle part is not work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
public class Downstair{
void doSomethng() {
}
public static void main(String[] args) {
JFrame mainframe;
mainframe = new JFrame();
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphicsClass contentPane = new GraphicsClass();
mainframe.getContentPane().add(contentPane);
mainframe.setSize(600, 600);
mainframe.setVisible(true);
}
}
class GraphicsClass extends JPanel{
ImageIcon bg;
JList menulist;
public GraphicsClass(){
bg = new ImageIcon("image/bgimg.jpg");
setBackground(Color.white);
setOpaque(true);
// Create components.
String[] gameMenulist = {"New Game","Abouts","how to play","Options","Exit"};
menulist= new JList(gameMenulist);
ListSelectionModel menulistModel = menulist.getSelectionModel();
//menulist setting
menulist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
menulist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
menulist.setVisibleRowCount(-1);
menulist.setBackground(Color.BLACK);
menulist.setForeground(Color.WHITE);
menulist.addMouseListener(this);
setLayout(new GridBagLayout());
GridBagConstraints a = new GridBagConstraints();
a.anchor = GridBagConstraints.PAGE_END;
add(menulist, a);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg.getImage(), 0, 0, this);
}
}
class EventClass implements MouseListener{
public void mouseClicked(MouseEvent e) {
switch(menulist.getSelectedIndex()) {
case 0:
playPage.startGame();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("unexpected type: " +
menulist.getSelectedIndex());
}
}
class GraphicsComponent extends JPanel {
protected void paintComponent(){
}
}
Message was edited by:
nickgoldgodl
# 1
don't know how to use Event class to handle a event.Do you have any exsample?
Okay. Here are some ideas demonstrated in the app below. And some pointers to resources.
To learn more about events and handling events see [url=http://java.sun.com/docs/books/tutorial/uiswing/events/index.html]Lesson: Writing Event Listeners[/url].
The [url=http://java.sun.com/docs/books/tutorial/index.html]Java Tutorial[/url] has a lot of information and code examples on many different areas.
The section on making guis is here: [url=http://java.sun.com/docs/books/tutorial/uiswing/index.html]Trail: Creating a GUI with JFC/Swing[/url].
Other subjects you might enjoy:
1 ?[url=http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html]Lesson: Performing Custom Painting[/url]. Includes more links.
2 ?[url=http://java.sun.com/docs/books/tutorial/uiswing/components/index.html]Lesson: Using Swing Components[/url]
3 ?[url=http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html]Lesson: Laying Out Components Within a Container[/url]
4 ?Animation: [url=http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html]Lesson: Concurrency in Swing[/url]. More advanced.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.event.*;
public class DS2 implements ActionListener {
DSGraphics graphicComponent;
DSController controller;
JPanel centerPanel;
public void actionPerformed(ActionEvent e) {
String ac = e.getActionCommand();
CardLayout cards = (CardLayout)centerPanel.getLayout();
cards.show(centerPanel, ac);
}
private JPanel getCenterPanel() {
graphicComponent = new DSGraphics();
controller = new DSController(graphicComponent);
CardLayout cards = new CardLayout();
centerPanel = new JPanel(cards);
centerPanel.add("Menu", getMenuPanel());
centerPanel.add("Game", graphicComponent);
return centerPanel;
}
private JPanel getMenuPanel() {
// Create components.
String[] gameMenulist = {"New Game","Abouts","how to play","Options","Exit"};
JList menulist= new JList(gameMenulist);
ListSelectionModel menulistModel = menulist.getSelectionModel();
//menulist setting
//menulist.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
menulist.setLayoutOrientation(JList.HORIZONTAL_WRAP);
menulist.setVisibleRowCount(-1);
menulist.setBackground(Color.BLACK);
menulist.setForeground(Color.WHITE);
menulist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
menulist.addListSelectionListener(new ListListener());
JPanel panel = new JPanel() {
Image bg = new ImageIcon(//"image/bgimg.jpg");
"images/Bird.gif").getImage();
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg, 0, 0, this);
}
};
panel.setBackground(Color.white);
panel.setOpaque(true); // default value
panel.setLayout(new GridBagLayout());
GridBagConstraints a = new GridBagConstraints();
// The anchor constraint requires a non-zero weight
// constraint in the direction it is going, ie,
// weightx for horizontal values such as EAST,
// WEST, LINE_START, LINE_END.
a.weighty = 1.0;
a.anchor = GridBagConstraints.PAGE_END;
panel.add(menulist, a);
return panel;
}
private Box getControls() {
String[] ids = { "Menu", "Game" };
Box box = Box.createHorizontalBox();
box.add(Box.createHorizontalGlue());
for(int j = 0; j < ids.length; j++) {
JButton button = new JButton(ids[j]);
button.setActionCommand(ids[j]);
button.addActionListener(this);
box.add(button);
box.add(Box.createHorizontalGlue());
}
return box;
}
public static void main(String[] args) {
DS2 app = new DS2();
JFrame mainframe = new JFrame();
mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainframe.getContentPane().add(app.getCenterPanel());
mainframe.getContentPane().add(app.getControls(), "Last");
mainframe.setSize(600, 600);
mainframe.setVisible(true);
}
private class ListListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()) {
JList list = (JList)e.getSource();
int index = list.getSelectedIndex();
switch(index) {
case 0:
controller.newGame();
CardLayout cards = (CardLayout)centerPanel.getLayout();
cards.show(centerPanel, "Game");
break;
case 4:
System.exit(0);
break;
default:
System.out.println("unexpected type: " + index);
}
}
}
}
}
class DSGraphics extends JPanel {
Ellipse2D.Double ball = new Ellipse2D.Double(60,100,40,40);
public void initializeBall() {
double x = 60;
double y = 100;
ball.setFrameFromDiagonal(x, y, x+ball.width, y+ball.height);
repaint();
}
public void moveBall(int dx, int dy) {
double x = ball.x + dx;
double y = ball.y + dy;
ball.setFrameFromDiagonal(x, y, x+ball.width, y+ball.height);
repaint();
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(Color.blue);
String s = "Click to start and stop";
float x = (getWidth() - g2.getFontMetrics().stringWidth(s))/2;
g2.drawString(s, x, 25);
g2.setPaint(Color.red);
g2.fill(ball);
}
}
class DSController implements Runnable {
DSGraphics component;
Thread thread;
boolean keepRunning = false;
int dx = 2;
int dy = 3;
final int DELAY = 50;
public DSController(DSGraphics dsg) {
component = dsg;
component.addMouseListener(ml);
}
public void newGame() {
stop();
component.initializeBall();
}
public void run() {
while(keepRunning) {
try {
Thread.sleep(DELAY);
} catch(InterruptedException e) {
System.out.println("interrupted");
stop();
}
advance();
}
}
private void advance() {
checkBoundries();
component.moveBall(dx, dy);
}
private void checkBoundries() {
int w = component.getWidth();
int h = component.getHeight();
Rectangle r = component.ball.getBounds();
if(r.x + dx < 0 || r.x + r.width + dx > w)
dx *= -1;
if(r.y + dy < 0 || r.y + r.height + dy > h)
dy *= -1;
}
public void start() {
if(!keepRunning) {
keepRunning = true;
thread = new Thread(this);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
}
}
public void stop() {
keepRunning = false;
if(thread != null)
thread.interrupt(); // wake up
thread = null;
}
private MouseListener ml = new MouseAdapter() {
public void mousePressed(MouseEvent e) {
if(keepRunning)
stop();
else
start();
}
};
}