Solved

Why doesn't this JFrame draw a line?

Posted on 2011-09-24
10
207 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
String array comparison 4 45
Java: The Public Class Main 4 45
java imports not found 4 47
restrict decimal places for double datatype 10 29
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

740 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