Tuesday, April 15, 2014

Adding multiple components to 1 single frame/panel
















Title says it all i want to get these two components:
























Java Code:




















package TestVersion;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JPanel;

public class GameWorld extends JPanel {

private int charX = 225;
private int charY = 225;
private int charDiameter = 25;
private int charDeltaX = -1;
private int charDeltaY = 3;
public Color playerColor = new Color(255,0,0);
private int noseX = 236;
private int noseY = 221;
private int noseWidth = 4;
private int noseHeight = 5;

public void changePlayerColor(Color YELLOW){
this.playerColor = YELLOW;
repaint();
}

public GameWorld() {
setPreferredSize(new Dimension(300, 300));

}


public void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.GREEN);

g.setColor(playerColor);
g.fillOval(charX, charY, charDiameter, charDiameter);
g.fillRect(noseX, noseY, noseWidth, noseHeight);
}
public void walkCharLeft() {
charX-=5;
repaint();
noseX-=5;


}
public void walkCharRight() {
charX+=5;
noseX+=5;
repaint();
}
public void walkCharUp() {
charY-=5;
noseY-=5;
repaint();
}
public void walkCharDown() {
charY+=5;
noseY+=5;
repaint();
}
public void runCharLeft() {
charX-=10;
noseX-=10;
repaint();

}
public void runCharRight() {
charX+=10;
noseX+=10;
repaint();

}
public void runCharUp() {
charY-=10;
noseY-=10;
repaint();

}
public void runCharDown() {
charY+=10;
noseY+=10;
repaint();

}



public void setColor(Color color) {
// TODO Auto-generated method stub

}



}


























Java Code:




















package TestVersion;

import java.awt.*;
import java.awt.Graphics;

import javax.swing.JPanel;

public class MatWood extends JPanel{

private int woodX = 250;
private int woodY = 100;
private int woodWidth = 10;
private int woodHeight = 50;

public MatWood(){
setPreferredSize(new Dimension(300, 300)); // Panel needs a size;
}

@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.BLACK);
g.fillRect(woodX, woodY, woodWidth, woodHeight);
}
}


















To the frame/panel what this class creates:















Java Code:




















package TestVersion;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.JFrame;

import TestVersion.CKeyListener;
import TestVersion.GameWorld;
import TestVersion.MatWood;

public class MYCoreWorld {


public static void main(String[] args) {


JFrame frame = new JFrame("My Core 0.0.1a");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GameWorld gameWorld = new GameWorld();
//make sure that the panel can detect keyevents
gameWorld.setFocusable(true);
gameWorld.setBackground(Color.GREEN);

MatWood matWood = new MatWood();

CKeyListener listener = new CKeyListener(gameWorld);
gameWorld.addKeyListener(listener);

frame.add(matWood, BorderLayout.SOUTH);
frame.add(gameWorld, BorderLayout.CENTER);

frame.setBackground(Color.GREEN);
frame.pack();
frame.setVisible(true);

//make sure the JPanel has the focus
gameWorld.requestFocusInWindow();

}
}

































No comments:

Post a Comment