Solved

Why doesn't this JFrame draw a line?

Posted on 2011-09-24
10
199 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
  • 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 500 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashMap Vs TreeMap 12 55
Running Jira on Raspberry PI 2? 3 247
eclipse package explorer vs project explorer view 2 73
Basic Java Case or If-Else statement... 3 43
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now