I have the following two classes, the main-class (SamG) and the Panll-class. I have implemented the KeyListener in the Panll-class, but it doesn't seem to work.
public class SamG {
public static void main(String[] args) {
JFrame jf = new JFrame("My APP");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(800, 400);
jf.setVisible(true);
jf.pack();
jf.setContentPane(new Panll());
}
}
public class Panll extends JPanel implements KeyListener {
int x=100,y=100;
boolean run=true;
Panll() {
addKeyListener(this);
}
#Override
public void paint (Graphics g) {
super.repaint();
g.clearRect(0, 0, 800, 400);
update();
draw(g);
try {
Thread.sleep(17);
} catch (InterruptedException ex) {
Logger.getLogger(Panll.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void update(){
x++;
y++;
}
public void draw(Graphics g){
g.drawOval(x, y, 100, 100);
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyChar());
}
#Override
public void keyReleased(KeyEvent e) {
}
}
you can't focus a JPanel without explicit allowing it with setFocusable(true);. if you add this line you can take you focus on the JPanel. if you then press any button the KeyListener works just fine
First never send main thread to sleep. Create a new Thread that periodically calls a repaint of your panel.
Second don't use paint() method to draw your stuff. Use the paintComponent() method instead.
Third add your KeyListener to your JFrame. Your JPanel won't receive the KeyEvents because it never gets the Focus.
Related
I'm newbie and I'm trying to make a game but than I don't understand how to use repaint() from different paint with different threads too. One thread with thread.sleep and the other one doesn't have.
Here's my piece of code :
GamePanel :
public class GamePanel extends JPanel implements MouseListener, MouseMotionListener{
EnemyEngine enemyE = new EnemyEngine();
public GamePanel() {
new Thread(new Runnable() {
#Override
public void run() {
while(true){
repaint();
}
}
}).start();
new Thread(new Runnable() {
#Override
public void run() {
while(true){
enemyE.update();
enemyE.repaint();
try {
Thread.sleep(1000/10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void paintComponent(Graphics g) {
// board painting
}
}
EnemyEngine:
public class EnemyEngine extends JPanel{
Vector<Enemy> enemyVect = new Vector<>();
Random rand = new Random();
public void paintComponent(Graphics g){
for (Enemy enemy : enemyVect) {
enemy.render(g);
}
}
public void update() {
for (Enemy enemy : enemyVect) {
enemy.move();
}
}
}
I have already search on the internet but it still didn't work...or maybe I'm the stupid one :/
Please help me senpai
I don't understand clearly your issue about repaint?
If you want to refresh ui immediately, why don't you use paintImmediately(0, 0, getWidth(), getHeight());
I work on a Java development software with Swing and I have a problem with my code, I want to display an image with the LoadingFrame class, its main work but when I call the constructor and the start() method in my main class, the frame opens but the image doesn't display (I have no Exception).
Why it doesn't work with my main class?
public class LoadingFrame
{
private JFrame frame;
public LoadingFrame()
{
frame = new JFrame();
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setUndecorated(true);
frame.setContentPane(new Panneau());
}
public void start()
{
frame.setVisible(true);
}
public void stop()
{
frame.setVisible(false);
}
public static void main(String[] args)
{
LoadingFrame l = new LoadingFrame();
l.start();
try
{
Thread.sleep(3000);
}
catch(Exception e)
{
e.printStackTrace();
}
l.stop();
}
}
public class Panneau extends JPanel
{
public void paintComponent(Graphics g)
{
System.out.println("hello");
try
{
Image img = ImageIO.read(new File("Images/loading.png"));
//g.drawImage(img, 0, 0, this);
//Pour une image de fond
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
The App class is my main class :
public class App {
//Attributes used to display the application
private JFrame frame;
//Attribute which display a waiting frame
private static LoadingFrame loadingFrame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
loadingFrame = new LoadingFrame();
loadingFrame.start();
App window = new App();
loadingFrame.stop();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public App()
{
initialize();
synchronizeScriptReferenceList();
synchronizeTests();
}
[...]
}
I was able to get this to work from App.java. For some reason, using EventQueue isn't cutting it. I tried to use SwingUtilities as well, but that doesn't work either. Finally I tried just get rid of the Thready-stuff in App.main at just straight up running it in the main thread. For some reason, this works when the other approaches do not! Here is my code:
// In the App class:
public static void main(String[] args) {
try {
loadingFrame = new LoadingFrame();
loadingFrame.start();
App window = new App();
loadingFrame.stop();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
When I used this code, I got it to work! (for some reason unknown to me), And here's a bonus rewrite of the Panneau class:
class Panneau extends JPanel
{
Image img;
public Panneau() {
try
{
img = ImageIO.read(new File("Images/loading.png"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
}
}
There are two main difference with this class; problems which I addressed. Here they are:
I call super.paintComponent as the very first method in our own paintComponent
I only load the loading image once, in the constructor, and not every single time I want to draw, which moves everything along much smoother. (you don't want the loading screen to be CPU heavy, do you?)
Hopefully, with these improvements, I hope you can make your program work! It worked with me, so I wish the best of luck to you.
P.S. Don't call frame.pack(), that was a mistake on my part. For some reason, I think it doesn't work well with undecorated windows.
EDIT: Okay, so I've managed to get it working now with the help of a friend. =)
I am currently creating a Java applet. The applet works by taking text input from the user and displaying it to the screen when they press return. This is the point up to which my program is working at the moment.
I was wondering whether there was a way in which I could make it so the text input/String is drawn when the mouse is clicked, at the point of mouse click.
Many thanks in advance to anyone who can help me out with this. :)
I was wondering whether there was a way in which I could make it so
the text input/String is drawn when the mouse is clicked, at the point
of mouse click.
Answer: Yes...
Would it be rude of me to leave the answer as that...?
This is a relatively simple process, depending on what you want to achieve...
This example just uses Graphics#drawString to render text to a custom component. You could, equally, just at a label to the component at the specified point, but that's another can of worms.
public class TestDrawText {
public static void main(String[] args) {
new TestDrawText();
}
public TestDrawText() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Point textPoint;
public TestPane() {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textPoint = e.getPoint();
repaint();
}
});
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (textPoint != null) {
FontMetrics fm = g.getFontMetrics();
g.drawString("You clicked at " + textPoint.x + "x" + textPoint.y, textPoint.x, textPoint.y + fm.getAscent());
}
}
}
}
Check out
How to Write a Mouse Listener
Performing Custom Painting
For more info.
I'm making my first Applet. I have a JPanel which creates a Swing GUI and performs CPU intensive tasks (repainting a Component 60Hz). My Applet displays this JPanel on event dispatching thread. here is an abstraction of the problem. Normally I would launch the applet from an html document instead of having a main method. This program puts about a 40% load on my CPU.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
public class TestApplet extends JApplet {
TestPanel tp;
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
private void createGUI() {
//Create and set up the content pane.
tp = new TestPanel();
tp.setOpaque(true);
setContentPane(tp);
}
public static void main(String[] args) {
JFrame f = new JFrame("Fish Tank");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet ap = new TestApplet();
ap.init();
f.add("Center", ap);
f.pack();
f.setVisible(true);
}
}
class TestPanel extends JPanel{
public TestTank tt = new TestTank();
public TestPanel() {add(tt);}
public void stop() {tt.stop();}
public void start() {tt.start();}
}
class TestTank extends Component implements ActionListener{
private javax.swing.Timer timer;
TestTank(){
timer = new javax.swing.Timer(17, this);
timer.setCoalesce(true);
timer.start();
}
public Dimension getPreferredSize(){
return new Dimension(900, 700);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Dimension size = getSize();
g2.setPaint(new GradientPaint(0,0,Color.RED,900, 0,Color.WHITE));
g2.fill(new Rectangle2D.Float(0,0,size.width,size.height));
}
public void actionPerformed(ActionEvent e) {
repaint();
}
public void stop(){timer.stop();}
public void start(){timer.start();}
}
My question: How do I suspend and resume execution of the JPanel (FishTankPanel) when the user switches tabs or minimizes the browser? I want the Applet to stop using the CPU when the user can't see what it is doing. I need to capture browser events in order to execute tp.stop() in the applet. I have tried to execute them with window event listeners in the JPanel, and by overriding the start() and stop() methods in the Applet. I have been unsuccessful. Any suggestions or solutions would be appreciated.
I would do as Dave said and use the JApplet override start and stop methods to call your GUI methods. For instance, see changes in code:
public class TestApplet extends JApplet {
TestPanel tp;
public void init() {
// ... no change
}
private void createGUI() {
// ... no change
}
#Override
public void stop() {
if (tp != null) {
tp.stop();
}
}
#Override
public void start() {
if (tp != null) {
tp.start();
}
}
}
class TestTank extends Component implements ActionListener {
private javax.swing.Timer timer;
// ... no change
public void stop() {
timer.stop();
System.out.println("stop");
}
public void start() {
timer.start();
System.out.println("start");
}
}
It seems you might need to leverage some JS for this. E.G. use the JS shown in this answer to explicitly call the applet start() & stop() methods on focus & blur detection respectively.
The solution for my problem was to use javascript to implement the Page Visibility API. I then called the appropriate Java methods from within the javascript script.
I'm trying to write a full screen application that takes keyboard inputs. I've tried adding keylisteners to my JFrame and now to a canvas within the JFrame and setting them both as focusable but it doesn't seem to be registering anything at all. Is there something special I have do to in full screen mode to get it to register my keyEvents? Any help greatly appreciated. (display is just an extension of JFrame with no changes yet)
public class chanceCore implements Runnable, KeyListener, MouseListener {
boolean isRunning = true;
display frame;
Canvas canvas;
GraphicsDevice device;
GraphicsEnvironment environment;
public chanceCore() {
setUp();
run();
}
public void setUp() {
frame = new display("CGE");
canvas = new Canvas();
frame.getContentPane().add(canvas);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas.addKeyListener(this);
canvas.addMouseListener(this);
canvas.setFocusable(true);
frame.setVisible(true);
environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
device = environment.getDefaultScreenDevice();
device.setFullScreenWindow(frame);
}
#Override
public void run() {
while (isRunning) {
draw(canvas.getGraphics());
try {
Thread.sleep(200);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public void draw(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
}
#Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
}
Try this code. After calling setFullScreenMode(), just add these two lines.
When we enter full screen something goes wrong,
frame.setVisible(false);