This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-05-11 12:33:52

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Java

I'm kinda new at java, so how do I make this jframe display the graphics void?

Code:

import java.awt.Cursor;
import java.awt.Graphics;

import javax.swing.JFrame;

public class main {
    
    public static String gameTitle = "Squares Mouse";
    public static JFrame f = new JFrame(gameTitle);
    
    public void paint(Graphics g){
        g.drawString("Squares Mouse", 10, 20);
    }
    
    public static void createFrame(){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setCursor(Cursor.CROSSHAIR_CURSOR);
        f.setSize(460, 340);
        f.setVisible(true);
    }
    
    public static void main(String[] args) {
        createFrame();
        
    }
}

thanks.


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#2 2012-05-11 12:46:52

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Java

i need answer quick...


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#3 2012-05-11 12:48:46

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Java

You need to extend from JFrame.

Code:

import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;

public class main extends JFrame{
    
                 public static String gameTitle = "Squares Mouse";
                 public main(){
                                   setTitle(gameTitle);
                             setDefaultCloseOperation(3);
                      setCursor(Cursor.CROSSHAIR_CURSOR);
        setSize(460, 340);
        setVisible(true);
    }
    public void paint(Graphics g){
                                   Graphics2D g2d = (Graphics2D) g;
        g2d.drawString("Squares Mouse", 10, 20);
    }
    
    public static void main(String[] args) {
        new main();
        
    }
}

Last edited by poopo (2012-05-11 12:53:23)


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#4 2012-05-11 12:50:59

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Java

poopo wrote:

You need to extend from JFrame.

well, still doesn't display it, but lets me add

Code:

main main = new main();
f.add(main);

http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#5 2012-05-11 12:53:30

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Java

try it now  hmm  I have no idea why it's not working.
Ok I figured it out. Try to display the text at 50, 50. You have to account for the window frame size.

Last edited by poopo (2012-05-11 12:57:54)


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#6 2012-05-11 12:57:13

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Java

poopo wrote:

try it now  hmm  I have no idea why it's not working.

still not working. I got this to work before, but i forget the code...


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#7 2012-05-11 12:58:11

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Java

WindowsExplorer wrote:

poopo wrote:

try it now  hmm  I have no idea why it's not working.

still not working. I got this to work before, but i forget the code...

Check my last post.  wink


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#8 2012-05-11 13:02:04

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Java

poopo wrote:

WindowsExplorer wrote:

poopo wrote:

try it now  hmm  I have no idea why it's not working.

still not working. I got this to work before, but i forget the code...

Check my last post.  wink

can u just edit my code, and not completely change it to ur style of code, and add a new post, not edited. thanks


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

#9 2012-05-11 13:18:05

poopo
Scratcher
Registered: 2009-09-20
Posts: 1000+

Re: Java

Code:

import java.awt.Cursor;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class main extends JPanel{

    public static String gameTitle = "Squares Mouse";
    public static JFrame f = new JFrame(gameTitle);

    public void paint(Graphics g){
        g.drawString("Squares Mouse", 0, 10);
    }

    public void createFrame(){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setCursor(Cursor.CROSSHAIR_CURSOR);
        f.setSize(460, 340);
        f.setVisible(true);
        f.add(this);
    }

    public static void main(String[] args) {
        new main().createFrame();
    }
}

That is the most like yours I can make it.


http://i45.tinypic.com/28rnqki.jpg

Offline

 

#10 2012-05-11 14:59:39

WindowsExplorer
Scratcher
Registered: 2011-02-25
Posts: 1000+

Re: Java

poopo wrote:

Code:

import java.awt.Cursor;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class main extends JPanel{

    public static String gameTitle = "Squares Mouse";
    public static JFrame f = new JFrame(gameTitle);

    public void paint(Graphics g){
        g.drawString("Squares Mouse", 0, 10);
    }

    public void createFrame(){
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setCursor(Cursor.CROSSHAIR_CURSOR);
        f.setSize(460, 340);
        f.setVisible(true);
        f.add(this);
    }

    public static void main(String[] args) {
        new main().createFrame();
    }
}

That is the most like yours I can make it.

Thanks  smile  It works  big_smile !!!


http://i.imgur.com/H6LLdnK.pnghttp://i.imgur.com/VYuD7BY.png

Offline

 

Board footer