-
Java Applet Issue
Ok, so I have this rudimentary code, basically just to test if Netbeans 6.0 is working correctly. Well when I run the program (draws a 800 x 500 rectangle) the rectangle gets cut off
Code:
import java.applet.*;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
public class MyApplet extends Applet {
// Values
private Rectangle box;
public MyApplet() {
box = new Rectangle(300,10, 10, 10);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
setSize(new java.awt.Dimension(1000, 1000));
g2.drawRect(0,0, 800, 500);
}
}
I have the picture, I can send it VIA PM if you would like
-
I think that you set the size of an Applet in the HTML that holds it (I may be wrong as I'm not an expert in this). Also, I'd advise you against calling setSize from within the paint method as it smells funny in that location. The paint method should be used for painting and nothing but painting.
-
Where should I put the setSize then?? If I put it anywhere else it doesn't work
-
How about (as I stated above) in your HTML code? Do not put it in the paint method. It may work somewhat in the init method, but I still think it's better in the html code that holds and displays the applet. again, i'm no expert on this. Ask me about Swing :)
-
This isn't going to be run in a browser, this is to be a standalone program. Sorry if I didn't state this in the beginning.
-
If a standalone, why create an Applet? From what I know/believe, I think that you'd be far better off using Swing and not AWT and creating a JFrame.
-
Thanks! That is exactly what I am going to do. I remember thinking about doing it via swing, but when I sat down to program that idea got thrown out of the window.. Thank you for not only your help, but the quickness you answered my question
-
You're welcome. One thing I noticed just now about your code is that your MyApplet class has a constructor. Please note that applet extending classes usually don't have their constructors called, but rather the init() method is called instead.
Good luck with your JFrame and please come back if you have a question.
-
Yeah, I'm a little rusty (well more than a little) when it comes to Java. Hell the last time I programmed in Java, projects weren't used.
-
Just to get you started, here's an example similar to what you were trying to create above, but using Swing:
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RectExample extends JPanel
{
private Rectangle2D rect2D = new Rectangle2D.Double(10, 10, 500, 400);
public RectExample()
{
setPreferredSize(new Dimension(700, 500));
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.blue);
g2.fill(rect2D);
}
private static void createAndShowUI()
{
JFrame frame = new JFrame("RectExample");
frame.getContentPane().add(new RectExample());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
the Sun graphics tutorials are great, BTW, and you'd do well to study them.
Again, much luck.