Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-05-2008, 02:38 AM
Member
 
Join Date: Feb 2008
Posts: 6
diRisig is on a distinguished road
New: Want to understand Drawing...
Hello,

I'm trying to get into java coming from C# and C++. And I want to understand paint() events of awt components.

I have a jFrame which has a Canvas and a jButton. I've put this code in the button's clicked event method but i don't get anything drawn on the canvas. How do I draw to a canvas? please

Code:
Graphics g = canvas1.getGraphics(); g.setColor(Color.BLACK); g.fillRect(0, 0, canvas1.WIDTH, canvas1.HEIGHT);
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-05-2008, 09:13 AM
Senior Member
 
Join Date: Jul 2007
Posts: 910
hardwired is on a distinguished road
In Swing use a (non-opaque) JComponent or a(n opaque) JPanel and override the paintComponent method for custom graphics. Although you can use an AWT/heavyweight Canvas you may have problems later — see Mixing heavy and light components for more.
The general idea for custom drawing is to set up the paintComponent method to be ready to draw its enclosing classes state at any time and to adjust/change the state from within event code.
See the JComponent paint method for perspective about the various painting methods and how they relate. You can override paint and the other two but it is generally for more advanced work.
Lesson: Performing Custom Painting has basic concepts.
Code:
import java.awt.*; import java.awt.event.*; import java.util.Random; import javax.swing.*; public class Drawing extends JPanel implements ActionListener { Random seed = new Random(); Color color = Color.red; public void actionPerformed(ActionEvent e) { color = getColor(); repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int cx = getWidth()/2; int cy = getHeight()/2; int d = 75; g2.setPaint(color); g2.fillOval(cx-d/2, cy-d/2, 75, 75); } private Color getColor() { return new Color(seed.nextInt(0xffffff)); } private JPanel getUIPanel() { JButton button = new JButton("change"); button.addActionListener(this); JPanel panel = new JPanel(); panel.add(button); return panel; } public static void main(String[] args) { Drawing test = new Drawing(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.add(test.getUIPanel(), "Last"); f.setSize(400,400); f.setLocationRelativeTo(null); f.setVisible(true); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help me need to understand queries hossainsadd Database 0 04-15-2008 02:30 AM
Errors I don't understand MattyB New To Java 4 04-02-2008 12:55 AM
Cannot understand whats wrong Lehane_9 New To Java 1 03-06-2008 08:57 PM
i don understand this error Deon New To Java 4 01-12-2008 11:03 AM
i can't understand using interface as a type sireesha New To Java 3 11-20-2007 11:07 PM


All times are GMT +3. The time now is 09:40 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org