For Programmers: Free Programming Magazines  


Home > Archive > Java Help > November 2007 > Simple Help required....









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Simple Help required....
TheBigPJ

2007-11-15, 8:13 am

I am just trying to entertain myself in java to keep myself interested
in learning it. I want to create a square, and every time I press 'Up'
on the keyboard the square size increases. I can draw everything, but
I am unsure where I would go from there...any hints?

so far I have...

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Line{

private int x;
private int y;

public static void main(String args[])
{
x = 50;
y = 100;

JPanel face = new JPanel()
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

g.drawRect (50,50,100,100);

}
};
face.setBackground(Color.WHITE);
face.setPreferredSize( new Dimension(100, 100) );
JScrollPane scrollPane = new JScrollPane( face );
scrollPane.setPreferredSize( new Dimension(200, 200) );

JFrame frame = new JFrame();
frame.getContentPane().add( scrollPane );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}

public void keyPressed(KeyEvent e) {
//what do I do here??? If(e = 'up key'){x= x+25; y = y+25 }
}

}

Thanks,
Peter
Andrew Thompson

2007-11-15, 7:58 pm

TheBigPJ wrote:
>I am just trying to entertain myself in java to keep myself interested
>in learning it. I want to create a square, and every time I press 'Up'
>on the keyboard the square size increases. I can draw everything, but
>I am unsure where I would go from there...any hints?


That code does not compile.

D:\Line.java:12: non-static variable x cannot be referenced from a static
context
x = 50;
^
D:\Line.java:13: non-static variable y cannot be referenced from a static
context
y = 100;
^
2 errors

This SSCCE* will. When posting code in future, please
consider posting SSCCEs, and it helps a lot if you add a
little indenting as per normal conventions. But indent
'less', around 2-3 chars per level, and definitely avoid
tab chars.

* <http://www.physci.org/codes/sscce.html>

<sscce>
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class Line{

static private int x;
static private int y;

public static void main(String args[])
{
x = 50;
y = 100;

JPanel face = new JPanel()
{
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

g.drawRect (50,50,100,100);
}
};
face.setBackground(Color.WHITE);
face.setPreferredSize( new Dimension(100, 100) );
JScrollPane scrollPane = new JScrollPane( face );
scrollPane.setPreferredSize( new Dimension(200, 200) );

JFrame frame = new JFrame();
frame.addKeyListener( new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println(e);
//what do I do here???
// read the output..
// consult the JavaDocs..
// formulate theories..
// test the theories in code..
// if you get stuck, get back to us
// with a specific question.
}
} );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.getContentPane().add( scrollPane );
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
}
</sscce>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.as...-setup/200711/1

TheBigPJ

2007-11-15, 7:58 pm

public void keyPressed(KeyEvent e) {

switch (e.getKeyCode())
{
case KeyEvent.VK_UP:
System.out.println("Up Arrow Test...");
break;
}
}

Ive managed to change the value of x, but how do i redraw or rather
where does repaint() go? Ive tried a few places, but am missing
something.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com