Please help with a drawing on JPanel in a background.
Hello everybody,
I have already post this question in the main java forum and was adviced to place the question here. Since then, I have refactored the code. I need your opinion and advice.
My task is to draw some objects on the JPanel or some other components and to place the names to the objects, for example squares, but later, from the thread. Because it can take some time, untill the names will be avaliable.
So, I refactored the code I post before http://forum.java.sun.com/thread.jspa?threadID=5192586&tstart=15
And now, it draws on layers and from thread on resize, with some imitating interval.
Is it a good approach I use, is there a better way, to draw on JPanel, after some objects were already painted.
Here is my sample, try to resize window:
package test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
public class Painter extends JFrame implements ComponentListener{
private static final long serialVersionUID = 1L;
private ThreadPainter threadPainter;
JPanel panel = new JPanel() {
private static final long serialVersionUID = 1L;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println("repaint");
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.gray);
g2d.fillRect(0, 0, 800, 600);
g2d.setColor(Color.white);
for (int i = 0; i < 6; i++) {
Rectangle2D rec2d = new Rectangle2D.Double(10 + i * 100,
10 + i * 100, 10, 10);
g2d.fill(rec2d);
g2d.drawString("Square " + i, 10 + i * 100 + 20, 10 + i * 100);
// start thread to paint some changes later
if (threadPainter == null) {
threadPainter = new ThreadPainter();
threadPainter.start();
JPanelTest panel1 = new JPanelTest();
public class JPanelTest extends JPanel {
private static final long serialVersionUID = 1L;
private int times = 0;
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.red);
System.out.println("repaint panel test");
System.out.println("repaitn times");
g2d.drawString("Square " + times, 10 + times * 100 + 20, 10 + times * 100);
public void repaintMethod(int times) {
this.times = times;
this.repaint(times * 100 + 20, times * 100, 300,300);
public class ThreadPainter extends Thread {
private boolean stop = false;
@Override
public void run() {
while (!stop) {
int cnt = 0;
for (int i = 0; i < 6; i++) {
System.out.println("do task");
panel1.repaintMethod(i);
// load data, do calculations
try {
// emulate calcilation
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
if (stop) {
break;
cnt++;
if (cnt == 6) {
stopThread();
public void stopThread() {
this.stop = true;
public Painter() {
this.setLayout(new BorderLayout());
JLayeredPane pane = new JLayeredPane();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(new Point(100, 100));
this.setPreferredSize(new Dimension(800, 600));
this.panel.setOpaque(false);
this.panel1.setOpaque(false);
pane.setOpaque(false);
pane.add(panel, JLayeredPane.DEFAULT_LAYER);
pane.add(panel1, new Integer(
JLayeredPane.DEFAULT_LAYER.intValue() + 1));
this.add(pane, BorderLayout.CENTER);
panel.setBounds(new Rectangle(0, 0, 800, 600));
panel1.setBounds(new Rectangle(0, 0, 800, 600));
this.addComponentListener(this);
public static void main(String[] args) {
Painter painter = new Painter();
painter.pack();
painter.setVisible(true);
@Override
public void componentHidden(ComponentEvent e) {
// TODO Auto-generated method stub
@Override
public void componentMoved(ComponentEvent e) {
// TODO Auto-generated method stub
@Override
public void componentResized(ComponentEvent e) {
if (threadPainter != null) {
threadPainter.stopThread();
threadPainter = new ThreadPainter();
threadPainter.start();
@Override
public void componentShown(ComponentEvent e) {
// TODO Auto-generated method stub
}
Hello camickr,
thanks for your answers.
It sounds like you are trying to add a component and
descriptive text of this component to a panel. So the
question is why are you overriding paintComponent()
method on your main panel and why are you using a
Thread.JLabel is not a good way I think, because of the performance. Think about 1000 labels on the panel. And the text can have different style, so drawString method is better here.
Create a component that draws your "shape" and then
add that component to the main panel that uses a null
layout. That means you need to specify the bounds of
the component. Then you can add a JLabel containing
the releated text of the component. If you don't know
the text then it can always be updated with the
setText() method in the future. Also, the foreground
can be updated as well.If it would be lable, I could update Text as us say and JLabels would be perfect. In this case, JLabels are not what I need.
You said the names will not be available right away.
Well then you don't start a thread to schedule the
repainting of the names since you don't know exactly
when the name will be available. You wait until you
have the names and then simple use the setText()
method.I have decided this way.
Draw all objects on the panel in different layers, for different type of objects.
Get names and make some calculations, for example, optimal position of the text on the panel (in Thread) and after it repaint();
Repaint draw all objects, but now it have the names, which will be added too.
The requirement is: thousand of objects on the panel
Different type of ojbects should be drawn in different layers
Names should be added to the objects and maybe some other drawings (showing state). Name and drawing can be done later, but should be added dynamically, after all calculation are done.
Similar Messages
-
Dear Friends, I am trying to draw a structure in Adobe Illustrator with 3D effect. But I am unable to give it proper shape. Can someone please help me for drawing the similar structure as showed below. I have facing problems in giving 3D effect.
Thanks a lot for your great help...i made a quick example:
and what it's made of:
just shapes and gradients. used a combination of pathfinder and shape builder to get the shapes exact. -
I switched over from an iPhone to a Samsung Galaxy S3 & I haven't been able to receive any text messages from iPhones. I have no problem sending the text messages but I'm not receivng any from iPhones at all. It has been about a week now that I'm having this problem. I've already tried fixing it myself and I also went into the sprint store, they tried everything as well. My last option was to contact Apple directly. Please help with turning my iMessage completely off so that I can receive my texts.
If you registered your iPhone with Apple using a support profile, try going to https://supportprofile.apple.com/MySupportProfile.do and unregistering it. Also, try changing the password associated with the Apple ID that you were using for iMessage.
-
How can I sync my iPhone on a different computer without erasing my applications? My iPhone was earlier synced with a PC which I don't use anymore.
On the new computer, I am getting a message that my all purchases would be deleted if I sync it with new iTunes library.
Please help with proper steps, if any.Also see... these 2 Links...
Recovering your iTunes library from your iPod or iOS device
https://discussions.apple.com/docs/DOC-3991
Syncing to a New Computer...
https://discussions.apple.com/docs/DOC-3141 -
Please help with the message I am receving on startup ""You can't open the application NovamediaDiskSupressor because PowerPC applications are no longer supported."
I have read some of the replies in the Apple Support Communities, but as I am no techie, I would have no idea how I would implement that solution.
Please help with what I need to type, how, where, etc.
Many thanks
AppleSueIn HunterCreekI am afraid there is no solution.
PowerPC refers to the processing chip used by Apple before they transferred to Intel chips. They are very different, and applications written only for PPC Macs cannot work on a Mac running Lion.
You could contact the developers to see if they have an updated version in the pipeline. -
Hi, please help with the installation of Lightroom 4, I bought a new Mac (Apple) and I want to install a software that I have on the album cd. My new computer does not have the drives. Can I download software from Adobe? Is my license number just to be able to download the srtony adobe.
Adobe - Lightroom : For Macintosh
Hal -
[ETL]Could you please help with a problem accessing UML stereotype attributes ?
Hi all,
Could you please help with a problem accessing UML stereotype attributes and their values ?
Here is the description :
-I created a UML model with Papyrus tool and I applied MARTE profile to this UML model.
-Then, I applied <<PaStep>> stereotype to an AcceptEventAction ( which is one of the element that I created in this model ), and set the extOpDemand property of the stereotype to 2.7 with Papyrus.
-Now In the ETL file, I can find the stereotype property of extOpDemand as follows :
s.attribute.selectOne(a|a.name="extOpDemand") , where s is a variable of type Stereotype.
-However I can't access the value 2.7 of the extOpDemand attribute of the <<PaStep>> Stereotype. How do I do that ?
Please help
Thank youHi Dimitris,
Thank you , a minimal example is provided now.
Version of the Epsilon that I am using is : ( Epsilon Core 1.2.0.201408251031 org.eclipse.epsilon.core.feature.feature.group Eclipse.org)
Instructions for reproducing the problem :
1-Run the uml2etl.etl transformation with the supplied launch configuration.
2-Open lqn.model.
There are two folders inside MinimalExample folder, the one which is called MinimalExample has 4 files, model.uml , lqn.model, uml2lqn.etl and MinimalExampleTransformation.launch.
The other folder which is LQN has four files. (.project),LQN.emf,LQN.ecore and untitled.model which is an example model conforming to the LQN metamodel to see how the model looks like.
Thank you
Mana -
Want a complete migration guide to upgrade 11.1.0.7 to 11.2.0.3 database using DBUA..We are implementing R12.1.3 version and then have to migrate the default 11gR1 database to 11.2.0.3 version. Please help with some step by step docs
Upgrade to 11.2.0.3 -- Interoperability Notes Oracle EBS R12 with Oracle Database 11gR2 (11.2.0.3) (Doc ID 1585578.1)
Upgrade to 11.2.0.4 (latest 11gR2 patchset certified with R12) -- Interoperability Notes EBS 12.0 and 12.1 with Database 11gR2 (Doc ID 1058763.1)
Thanks,
Hussein -
Welcome.
At the outset, I'm sorry for my English
Please help with configuration Photoshop CS6 appearance.
How to disable the background of the program so you can see the desktop. (same menus and tools)
i wantto be the same effect as CS5.Please try turning off
Window > Application Frame -
cont contact apple server error please help with ipad touch 4. im just fed up with apple please help me because why is it only apple with these kind of problems?
If you mean updae server
Update Server
Try:
- Powering off and then back on your router.
- iTunes for Windows: iTunes cannot contact the iPhone, iPad, or iPod software update server
- Change the DNS to either Google's or Open DNS servers
Public DNS — Google Developers
OpenDNS IP Addresses
- For one user uninstalling/reinstalling iTunes resolved the problem
- Try on another computer/network
- Wait if it is an Apple problem
Otherwise what server are you talking about -
Purchased DVD in US for Cdn viewing. Digital download will not work in Cda or US? please help with new Digital code that will work
You will need to contact the movie studio that produced the DVD and ask if they can issue you a new code valid for Canada. Apple cannot help you, and everyone here in these forums is just a fellow user.
Regards. -
Please help with slideshow problems!
Am using Photoshop Elements 8 and trying to make a slideshow. Have tried 4 times now and keep ending up with same problem, cannot reopen project to continue edititing. Won't show up in orginizer and when I find on harddrive and try to open get message " wmv file cannot be opened". How can I save a
slideshow inprogress and be able to reopen and continue to edit and make slideshow? I want to thank anyone who can help me with this in advance as I
have gotten so frustrated that I want to just scream.
ThanksThanks for the help, thought I had done so but maybe not. Anyway will have another go at it, now may I ask another
question? I am trying to add audio to slideshow. I have some music I purchased thru amazon as mp3 files but I get
message no codec and when I try to add wmv I get same message. What type of file do I need and how can I add
multiple songs to one slideshow. I have one little wmv file that will go in, but it just replicates itself multiple times until
it fills slide show.
Thanks again, sorry to be a bother, but this thing is driving this old man crazy.
Date: Sun, 26 Dec 2010 20:34:32 -0700
From: [email protected]
To: [email protected]
Subject: Please help with slideshow problems!
You need to save the slideshow project in order to be able to go back later and make changes or additions to an existing slideshow . The wmv file is a final output format.
Now you are most probably using only the Output command: that is what makes the wmv file.
You should also do the Save Project command. (and I make it a practice to do the Save Project command before I do the Output command).
If you look at the Elements Organizer 8 Help, there is a topic on "Create a slide show".
-- Very close to the beginning of that topic is a screen shot of the Sldie Show Editor screen,
-- The bar below the usual menu bar is labeled with a "B" and called the Shortcuts bar.
-- The 1st entry on that Shortcuts bar is "Save Project"
It is the Save Project command that saves the information about which photos, audio, etc you placed in that specific slide show so that you can come back again to do subsequent editing. Save each Project with a unique name.
After completing the Save Project command, you shoud see an "icon" in the Organizer for that slide show.
Note: you must also keep the photo files and audio files which you have used in this slide show: you can't delete them because the project file does NOT contain a copy of the photos, it only has the identification and folder location of the photo and audio files.
> -
Hi. I have an iPhone 4s. The music doesn't play when I connect it to my car stereo. It used to play previously but stopped playing all of a sudden. My phone is getting charged when I cut it to the USB port. Please help with this. The iOS is 6.1.3.
Hello Priyanks,
I found an article with steps you can take to troubleshoot issues with an iPhone not connecting to your car stereo:
iOS: Troubleshooting car stereo connections
http://support.apple.com/kb/TS3581
Thank you for using Apple Support Communities.
Best,
Sheila M. -
My "fn" button is stuck on only revealing mission control in Lion. With this problem none of my "f" keys will work nor can I hold "fn" and delete to delete text in opposite direction. Can someone please help with this?
Following the fix here worked for me:
https://discussions.apple.com/message/15680566#15680566
except my plist file was in ~/Library instead of /Library.
-Scott -
hi, when i am listening music and the screen goes to sleep my music stop playing.Please help with settings
No problem. And no worries, several people have made this same mistake!
Either way enjoy the iPod!
Brock
Maybe you are looking for
-
My @.mac screen name works in messages but not AIM
I have been using a @.mac screen name for my AIM for years now and had no issues. It still works in messages but since iCloud I can seem to use it with AIM on a PC or iPad or iPhone, or any other messages app.
-
Hello~ Does anyone know if there are additional templates to use for iWeb? If so, where can I find them and add them to my current library of templates that came with iWeb? Thank you. Corina
-
Want to purchase LR for mobile. already have CS6
Having mucho difficulty purchasing LR mobile. Already own CS6 & LR 5.5. Wont let me access 30 day free as I mistakenly clicked drop down 5 weeks ago - before I had mobile device...?! IT challenged; pretty much need to be walked through this. Virtuall
-
The IPOD was purchased from Amazon, My kid put a security code on it and forgot what it was and kept trying different combos. Now it is Locked! Msg comes up "IPOD is DISABLED" conect to Itunes Ready to throw this thing out the window! Thanks for any
-
Any implication of using fn-bea:async
Hello, I read what I could find about fn-bea:asynch, but I am left with some questions about its specifics. We have a function that performs a relatively slow database stored procedure. The logical service is called as a web service, and it invokes t