Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Question about graphics in Java?
I am trying to learn how to do graphics in java and I've written a simple program which does not work as I wish. I have one class called MyCanvas which extends JPanel and is the "surface" which I am trying to draw on. I have another class which creates a frame and an instance of MyCanvas and adds that object to the frame. I see the red circle which is drawn when paint is called which happens when the object is added to the frame. I do not see the blue circle when i call the drawOval method from MyCanvas though. The graphics object is defined in MyCanvas though. I have tried using both the update method and the repaint method. Neither work. Any help?
import java.awt.*;
import javax.swing.*;
public class Test {
JFrame frame;
MyCanvas canvas;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Test();
}
public Test(){
frame = new JFrame("Test");
canvas = new MyCanvas();
//Sets conditions for frame
frame.add(canvas);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//Should draw blue circle on screen
canvas.drawOval(10,10,100,100);
}
}
------------------------------------------------------------------------------------------------------
import java.awt.*;
import javax.swing.*;
public class MyCanvas extends JPanel {
public MyCanvas(){
super();
}
public void drawOval(int x, int y, int width, int height){
Graphics g = getGraphics();
g.setColor(Color.blue);
g.fillOval(x,y,width,height);
update(g);
}
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(100, 100, 200, 200);
}
}
@Teku I know the canvas size is not 0x0. First off I can see the red circle which is being painted by the paint method when I add the object to the frame. Second I previously thought this may be a problem and defined the size to fit the frame and still not blue circle. I merely took that out to be more succinct but I believe it automatically makes the panel the size of the frame unless otherwise specified.
@anuk Even removing update/repaint doesn't help. I know I could just do this all in paint but I don't want to do that. I'm trying to cause the component to update without using paint. Other programs must do this. There is no way some advanced programs in Java could handle every bit of graphics in the paint method. So I'm trying to find a way to do that.
2 Answers
- TekuConceptLv 410 years agoFavorite Answer
I'm not big in java any more but I have done alot of graphics coding lately: http://fc03.deviantart.net/fs70/i/2011/306/d/d/vis...
Here would be my c# version:
(don't be discouraged by the fact that this is in c# because it is practically the same syntax as java just named differently)
public class MyPanel : System.Windows.Forms.Panel
{
// generated code
private System.ComponentModel.Container components = null;
public Panel()
{
InitializeComponent();
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// set defaults
this.Size = new Size(600, 400);
this.BackColor = Color.White;
this.Location = Point.Empty; // returns a point set to (0,0)
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components != null)
components.Dispose();
}
base.Dispose(disposing);
}
// The actual painting to the panel
protected override void OnPaint(PaintEventArgs e)
{
// the control's custom paint handeling
e.Graphics.DrawEllipse(new Pen(Color.Black), new Rectangle(100,100,200,200));
// the control's base paint handeling
base.OnPaint(e);
}
}
From what it sounds like, you aren't calling the base's Paint() method; but then again, after reviewing through your code again, I realized you didn't define the size nor location of your canvas. I bet that canvas' size is 0x0. Try including some of these properties into your canvas before adding your canvas to your frame.
Source(s): Programmer in C/C++, C#, Java, VB, Python, Ruby, Perl, AS3 - 10 years ago
im also not a pro on graphics in java, but if i remember correct, any drawing u wanna do should be done inside the paint() method... i just checked with the java api... and what the update() method does is that it also calls paint() just like repaint() does.. so im guessing that ur blue circle gets erased by this second drawing of the red circle... (just my guess though.. i could be wrong)...