Article View: pl.comp.lang.java
Article #153034java 2D i znikajacy rysunek
From: zoska
Date: Sat, 22 Oct 2011 09:19
Date: Sat, 22 Oct 2011 09:19
75 lines
3290 bytes
3290 bytes
Witam
Napisalem taki maly programik w ktorym po kliknieciu buttonu OK
pojawiaja sie dodatkowy zolty kwadrat. Po przyslonieciu i odslonieciu
tego okna jakims innym oknem np mozilla zostaje tlo ale kwadrat znika.
Pytanie jest takie: jak to zrobic zeby ten kwadrat nie zniknal ?
Dzieki za wszelkie sugestie
Ponizej kod programu:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class PaintBackground extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
setBackground(Color.BLACK);
g2d.setColor(Color.RED);
g2d.drawRect(60,60,150,150);
}
}
class CreateGUI implements MouseListener {
JFrame frame = null;
JPanel panel = new JPanel();
JButton button = null;
final static int sizeX = 400;
final static int sizeY = 400;
final static int locX = 10;
final static int locY = 10;
public CreateGUI() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(sizeX,sizeY);
frame.setLocation(locX,locY);
Container cont = frame.getContentPane();
cont.setLayout(new BorderLayout());
button = new JButton("OK");
button.addMouseListener(this);
cont.add(button, BorderLayout.NORTH);
panel.setLayout(new BorderLayout());
panel.add(new PaintBackground(), BorderLayout.CENTER);
cont.add(panel, BorderLayout.CENTER);
frame.setVisible(true);
}
public void mouseClicked(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
Graphics g = panel.getGraphics();
g.setColor(Color.YELLOW);
g.drawRect(40,40,90,90);
panel.revalidate();
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public class RunApp {
public static void main(String[] args) {
Runnable runnable = new Runnable() {
public void run() {
new CreateGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
Technical Details
Message-ID:
<3314ac17-cdaa-4834-8d4c-8fac287f6083@h5g2000vbf.googlegroups.com>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!postnews.google.com!h5g2000vbf.googlegroups.com!not-for-mailShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!fu-berlin.de!postnews.google.com!h5g2000vbf.googlegroups.com!not-for-mail From: zoska <klociaciocia@gmail.com> Newsgroups: pl.comp.lang.java Subject: java 2D i znikajacy rysunek Date: Sat, 22 Oct 2011 02:19:40 -0700 (PDT) Organization: http://groups.google.com Lines: 75 Message-ID: <3314ac17-cdaa-4834-8d4c-8fac287f6083@h5g2000vbf.googlegroups.com> NNTP-Posting-Host: 80.11.130.129 Mime-Version: 1.0 X-Trace: posting.google.com 1319275625 1446 127.0.0.1 (22 Oct 2011 09:27:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 22 Oct 2011 09:27:05 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: h5g2000vbf.googlegroups.com; posting-host=80.11.130.129; posting-account=U2aqgAoAAABLAjDHgvN_bz1PZSnI3gOz User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-Header-Order: HUALESNKRC X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2,gzip(gfe) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit