Im trying to understand how this stuff works i have looked at some code… this is as far as a come.
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import java.awt.image.BufferStrategy;
public class RawSugar extends JFrame implements ActionListener {
BufferStrategy bufferStrategy;
public RawSugar() {
JFrame window = new JFrame("Project: RawSugar Beta");
window.setBounds(
50,
window.getToolkit().getScreenSize().height/20,
window.getToolkit().getScreenSize().width-100,
window.getToolkit().getScreenSize().height-100
);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout border = new BorderLayout();
Container c = window.getContentPane();
c.setLayout(border);
JMenuItem subMeny1;
JMenuItem subMeny2;
JMenuItem subMeny3;
window.setIgnoreRepaint( true );
JMenuBar menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
JMenu meny = new JMenu("Arkiv");
meny.setMnemonic(KeyEvent.VK_A);
menuBar.add(meny);
subMeny1 = new JMenuItem("Nytt",KeyEvent.VK_T);
subMeny1.setAccelerator(KeyStroke.getKeyStroke('N', ActionEvent.CTRL_MASK));
meny.add(subMeny1);
subMeny2 = new JMenuItem("Stäng",KeyEvent.VK_T);
subMeny2.setAccelerator(KeyStroke.getKeyStroke('Q', ActionEvent.CTRL_MASK));
meny.add(subMeny2);
meny.addSeparator();
subMeny3 = new JMenuItem("Stäng", new ImageIcon("macke.jpg"));
subMeny3.setMnemonic(KeyEvent.VK_B);
subMeny3.setAccelerator(KeyStroke.getKeyStroke('Q', ActionEvent.CTRL_MASK));
meny.add(subMeny3);
subMeny3.addActionListener(this);
JPanel plan = new JPanel();
plan.setBackground(Color.white);
c.add(plan, BorderLayout.CENTER);
ImageIcon icon = new ImageIcon("pics/test.jpg");
c.add(new JLabel(icon), BorderLayout.WEST);
JPanel right = new JPanel();
right.setBackground(Color.black);
c.add(right, BorderLayout.EAST);
right.setLayout(new GridLayout(24,1));
JPanel botten = new JPanel();
botten.setBackground(Color.black);
c.add(botten, BorderLayout.SOUTH);
TextField goalP1 = new TextField();
TextField goalP2 = new TextField();
TextField timeLeft = new TextField();
goalP1.setText("kauken");
goalP1.setBackground(Color.red);
botten.add(goalP1);
goalP2.setText("0 Goals");
goalP2.setBackground(Color.red);
botten.add(goalP2);
timeLeft.setText("TID KVAR");
timeLeft.setBackground(Color.white);
right.add(timeLeft);
//------------------KNAPPAR--------------------
JButton nytt = new JButton("New Game");
right.add(nytt);
nytt.setBackground(Color.gray);
nytt.addActionListener(this);
JButton end = new JButton("End Game");
right.add(end);
end.setBackground(Color.blue);
end.addActionListener(this);
JButton toplist = new JButton("TopList");
right.add(toplist);
toplist.setBackground(Color.gray);
toplist.addActionListener(this);
JButton p1 = new JButton("Player 1");
right.add(p1);
p1.setBackground(Color.blue);
p1.addActionListener(this);
JButton p2 = new JButton("Player 2");
right.add(p2);
p2.setBackground(Color.gray);
p2.addActionListener(this);
JButton start = new JButton("Start match");
right.add(start);
start.setBackground(Color.blue);
start.addActionListener(this);
window.setVisible(true);
//--------------------------------------------------
Image test2= Toolkit.getDefaultToolkit().createImage("test.png");
int ScreenWidth;
int ScreenHeight;
int test = 1;
//boolean Running = true;
bufferStrategy = plan.getBufferStrategy();
plan.createBufferStrategy( 3 );
while( test == 1 )
{
// This is where you would call a method that updates your game state (ie: moves
// objects, checks for collisions, etc.
// updategame();
// Call our render function to draw everything.
render();
// Sleep
try
{
Thread.sleep( 30 ); // This should be replaced with proper frame rate limiting code
} catch( InterruptedException e ) {}
}
}
// A variable used with render()
int i = 0;
// This method is called once every frame to display the current state of our game.
private void render()
{
// If we've lost our video memory, don't bother drawing anything
if( !bufferStrategy.contentsLost())
{
Graphics g = bufferStrategy.getDrawGraphics();
// Clear the drawing buffer to white
g.setColor( Color.white );
g.fillRect( 0, 0, 100, 111 );
// Say hello
g.setColor( Color.black );
g.drawString( "Hello World!", i, i );
// We'll stop after 400 frames.
i++;
// if( i > 400 )
// test=2;
bufferStrategy.show();
g.dispose();
}
//--------------------------------------------------------------
}
public static void main (String[] args) {
new RawSugar();
}
public void actionPerformed(ActionEvent e)
{
/*if(e.getSource() == subMeny3)
System.exit(0);
else if(e.getSource() == cirkel)
{
Cirkel c = new Cirkel(ruta);
t = new Thread(c);
BouncingObject.tradarIgang.add(c);
t.start();
}
else if(e.getSource() == fyrkant)
{
Rektangel c = new Rektangel(ruta);
t = new Thread(c);
BouncingObject.tradarIgang.add(c);
t.start();
}*/
}
}
the thing is that i want to be draw in the JPanel Plan.
I may be way wrong in my code… anyone have any tips… i tryed to set the drawing to the JFrame but thats not what i want and then the JFrame just shows the stuff on the screen before i started the program… plx help