?
Solved

Why doesn't this JFrame draw a line?

Posted on 2011-09-24
10
Medium Priority
?
217 Views
Last Modified: 2012-05-12
I don't understand why the JFrame below doesn't draw a line..

Thanks

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


public class happyFrame extends JFrame{

	public void repaint(Graphics G) {

		G.setColor(Color.blue);
		
		G.drawLine(20, 20, 200, 200);
	}
	public happyFrame () {
		
		this.setSize(200,200);
		this.setLocation(15, 150);
		this.setVisible(true);
		 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		repaint();
	}
}

Open in new window

0
Comment
Question by:beavoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36594500
It should be paint(Graphics G)
not repaint(Graphics G)
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36594502
This draws the line, but in general you  want to add panel
to the frame

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


public class happyFrame extends JFrame{

	public void paint(Graphics G) {

		G.setColor(Color.blue);
		
		G.drawLine(20, 20, 200, 200);
	}
	public happyFrame () {
		
		this.setSize(200,200);
		this.setLocation(15, 150);
		this.setVisible(true);
		 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		repaint();
	}

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

Open in new window

0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36594509

This would be normal way
(otherwise - without adding Panel -  it looks like it grabs as background
part of your current screen)

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


public class happyFrame extends JFrame{


	public happyFrame () {
        Container c = this.getContentPane();
		         c.add(new MyPanel());
		this.setSize(200,200);
		this.setLocation(300, 200);
		this.setVisible(true);
		 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		repaint();
	}

    public static void main(String[] args) {
        new happyFrame();
    }
}
class MyPanel extends JPanel {
       public void paint(Graphics g) {

		g.setColor(Color.blue);

		g.drawLine(20, 20, 200, 200);
	}

}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 47

Expert Comment

by:for_yan
ID: 36594514
And you don't need repaint() after setVisible(true);

It will paint all components when 
you execute setVisible(true).

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36594517
This is witthout repaint()
Still works the same way

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


public class happyFrame extends JFrame{


	public happyFrame () {
        Container c = this.getContentPane();
		         c.add(new MyPanel());
		this.setSize(200,200);
		this.setLocation(15, 100);
         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);

	//	repaint();
	}

    public static void main(String[] args) {
        new happyFrame();
    }
}
class MyPanel extends JPanel {
       public void paint(Graphics g) {

		g.setColor(Color.blue);

		g.drawLine(20, 20, 200, 200);
	}

}

Open in new window

0
 

Author Comment

by:beavoid
ID: 36595310
Nice.

Thanks

Last time I did this, (AWT), I had my Frame drawn in its own class, not a satellite class.
Does it matter?
Is there a disadvantage in doing the getContentPane() thing?

If I put all my listeners on the Mypanel, does it act exactly like a Jframe?
or do I still add listeners to the JFrame?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595368
AWT works differently - you can add directly to the Frame or other top-level elemnt, you don't need to do this stuff
with getContentPane() as with Swing
I don't know about any disadvantage with it, you just need to rememeber to do it.

It does not matter where you put the listeners - in any class you want - I usuallt add listeners to the top-level
frame, they can be even added to any class - it is just a matter to have convenient access to some
GUI elements in the action methods
0
 

Author Comment

by:beavoid
ID: 36595851
Thx,
Before I do my backbuffer game rendering,

I'm going to have the game on an all-white background.
 What is the best way to clear it?
G.fillRect ?
or can I simply say G=null;  or = new Graphics()

?
Which is fastest, cleanest?
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595874
when you initially create your panel
it will be all clean - if you need to clean it after you drew
something

you say
g.glearRect(0,0,this.getWidth(), this.getHeight())

But it should be in the paint method

so one of the ways to do it form outside  the method -
you have instance variable toClean in your class
and inside the piant method
you have

if(toClean)g.clearRect(0,0,...)

and ouside the paint method
when you want to clean you
say

toClean = true;
repaint();

and it will clean it






0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595953
In general when you design such things it is rarely you want to clean

First it appaers blank
then ywhen doing paint metghod it should rely on
ssome data whuch sould be instrance vraibles of the class

Say, assume you draw cards; you keep them in arraylist
This arraylist  should be instance variable and ittakes each card and draws
it in your paint

when in the course of your play you have situation that you have no cards
then your arraylist at thet moment has no elements
so your paint method should be written so, that
when your arrylist is empty - you just say
repaint()
and it should draw blank panel

So you rarely need to tdo something with special boolean
value to clean it as I wrote above
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question