Solved

Why doesn't this JFrame draw a line?

Posted on 2011-09-24
10
196 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
Comment Utility
It should be paint(Graphics G)
not repaint(Graphics G)
0
 
LVL 47

Expert Comment

by:for_yan
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:beavoid
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Performance issue while iterating with streams 5 39
sumDigits  challenge 7 60
word0 challenge 3 56
create a gui in perl 3 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

772 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