JFrame not displaying Canvas - java

I've recently been working with BufferStrategy and JFrame for preliminary graphics. However, when attempting to draw a simple rectangle to the frame, nothing appears. Here is the code:
import java.awt.*;
import java.awt.image.BufferStrategy;
import javax.swing.*;
public class GTestingMain {
public static void main(String[] args) {
JFrame myFrame = new JFrame();
Canvas myCanvas = new Canvas();
Graphics2D g;
BufferStrategy strategy;
myFrame.setSize(500, 500);
myFrame.setResizable(false);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myCanvas.setPreferredSize(new Dimension(500, 500));
myFrame.add(myCanvas);
myFrame.pack();
strategy = myCanvas.getBufferStrategy();
if(strategy == null) {
myCanvas.createBufferStrategy(3);
}
strategy = myCanvas.getBufferStrategy();//Throwing in again so strategy != null
do {
do {
g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.red);
g.fillRect(10, 40, 50, 70);
g.dispose();
}while(strategy.contentsRestored());
strategy.show();
}while(strategy.contentsLost());
}
}
Now the problem is a little more unique. When I go into the debugger and step through the code, it works just fine. If I run normally, blank. If I click on the Frame before it is drawn to while in the debugger - blank.
I know I can 'solve' this issue with a thread, but i'd like to know why it works* in debug mode but not in a regular run. Thanks!

Before you start off with bufferStrategies, go through java doc
It says that,
A buffer strategy gives you two all-purpose methods for drawing: getDrawGraphics and show. When you want to start drawing, get a draw graphics and use it. When you are finished drawing and want to present your information to the screen, call show. These two methods are designed to fit rather gracefully into a rendering loop:
What you are missing is a rendering loop basically. Also since it uses volatile images for its buffer, we never know when our video memory is lost. That explains why you were getting the rectangle in debug mode. Its like a callback system with contentLost and contentsRestored methods.
Just wrap your do while loops as below.
while(isHappy()){ // your logic goes here.
do {
do {
g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, 500, 500);
g.setColor(Color.red);
g.fillRect(10, 40, 50, 70);
g.dispose();
}while(strategy.contentsRestored());
strategy.show();
}while(strategy.contentsLost());
}

Related

I'm trying to move an object but i want it to leave a trail

So basically it won't leave a trail. I tried to remove super.paint and i've tried to make multiple but it either creates an error or it doesn't. I've gone throught it atleast 10 times which is why i went here. Thanks in advance!
import javax.swing.*;
import java.awt.*;
public class Grafik extends JPanel {
private int x = 0;
private void moveBall()
{
x += 1;
}
public void paint(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRect(x, 50, 20, 80);
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
Grafik grafik = new Grafik();
frame.setSize(700, 800);
frame.setLocation(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(grafik);
frame.setTitle("Mitt spel");
frame.setResizable(false);
frame.setVisible(true);
while(true)
{
grafik.repaint();
grafik.moveBall();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Problems/Solutions:
Swing painting is done in the paintComponent method override, not the paint method.
Be sure to call the `super's paintComponent method first thing in your own override method, so the JPanel can do house-keeping painting
Your animation loop should be a Swing Timer, not a while (true) loop as the latter risks running afoul of Swing threading rules
If you want to create an animation but leave persisting images within the drawing then
Either create an ArrayList of objects, perhaps Points, that represent the trail, and in your paintComponent method draw the trail using a for loop that iterates through the ArrayList, or
use a BufferedImage as a background image, one that is drawn within the paintComponent method immediately after calling super.paintComponent(g) but before drawing the moving sprint. Draw your trail into this BufferedImage by getting a Graphics object out of it, by calling getGraphics() on the BufferedImage.

How to draw vertical line in Swing

I am able to draw a horizontal line but unable to draw a vertical line. Please help me.
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
class Success extends JFrame{
public Success(){
JPanel panel=new JPanel();
getContentPane().add(panel);
setSize(450,450);
JButton button =new JButton("press");
panel.add(button);
}
public void paint(Graphics g) {
super.paint(g); // fixes the immediate problem.
Graphics2D g2 = (Graphics2D) g;
Line2D lin = new Line2D.Float(20, 40, 850, 40);
g2.draw(lin);
}
public static void main(String []args){
Success s=new Success();
s.setVisible(true);
}
}
Thanks in advance.
Keep the x co-ordinates the same and change value of y co-ordinates as shown below
Line2D lin = new Line2D.Float(20, 40, 20, 150);
First two values are the (x1,y1) value of the starting point of the line and last two values (x2,y2) end point of the line. Now I hope you understand why your code produced a horizontal line and what needs to be done to draw vertical line.
I noticed a couple things, some of them were already pointed out:
To answer your question directly, this is what the (x, y) coordinates look like for Swing components
keep x coordinates the same for a vertical line. If you don't know where the x coordinates are in your line constructor when you create it, look at the documentation for the constructor. If you're using Eclipse, this means you should just hover your mouse over the code that contains the constructor.
Your line goes outside the range of your JFrame; instead, if you want it to go from end to end, use the getWidth() and getHeight() methods.
You shouldn't be creating a new line every time you repaint your components. Instead, you should create the line somewhere in you Success class, implement ActionListener so you can update your code every frame, and in that update, resize your line, then leave just the repainting to paintComponent.
You shouldn't override JFrame in this case, and you usually shouldn't have to.
You should override the paintComponent method, not the paint method.
I don't think you're double-buffering correctly, but I can't help you there.
Overriding the getPreferredSize method of JPanel is handy if you want to control its size, but it's not even necessary in this case, because adding it to the JFrame will automatically size it for you.
There's a lot of stuff that goes on in Swing behind the scenes, and it can get confusing because normally you have to say stuff explicitly, but keep playing with this example, and you should be safe for a while.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.*;
class Success extends JPanel implements ActionListener{
private final Timer timer = new Timer(20, this); // Create a timer that will go off every 20 ms
Line2D horizontalLine; // Declare your variables here, but don't initialize them
Line2D verticalLine; // That way, they can be accessed later in actionPerformed and repaint
// You might want to try frame.setResizable(false) if you want your frame
// and your panel to stay the same size.
private final Dimension prefPanelSize = new Dimension(450, 450);
public Success(){
super(); // Call the constructor of JPanel, the class this subclasses.
JButton button =new JButton("press");
this.add(button);
this.setSize(prefPanelSize);
horizontalLine = new Line2D.Float(0, 40, prefPanelSize.width, 40);
verticalLine = new Line2D.Float(prefPanelSize.width / 2, 0,
prefPanelSize.width / 2, prefPanelSize.height);
timer.start(); // Start the timer
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // fixes the immediate problem.
Graphics2D g2 = (Graphics2D) g;
g2.draw(horizontalLine);
g2.draw(verticalLine);
}
#Override
public Dimension getPreferredSize()
{
return prefPanelSize;
}
public static void main(String []args){
Success s = new Success();
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(450, 450));
frame.add(s);
}
// This method is called ever 20 ms because of the timer.
#Override
public void actionPerformed(ActionEvent e) {
int currWidth = getWidth();
int currHeight = getHeight();
horizontalLine.setLine(0, 40, currWidth, 40);
verticalLine.setLine(currWidth / 2, 0, currWidth / 2, currHeight);
}
}

Embedding TTF and using with g2d

I'm trying to embed a TTF font and then use draw it with Grapics2D. I've been able to create the font, but I'm not exactly sure how to pass the font to setFont. I make a new font here, which throws no exceptions:
private Font pixel = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font/amora.ttf"));
But I can't figure out how to draw it with setFont();
Here's my code:
private static final long serialVersionUID = 1L;
private Timer timer;
private Char Char;
private Font pixel = Font.createFont(Font.TRUETYPE_FONT, this.getClass().getResourceAsStream("font/amora.ttf")); <<<--------
public Board() throws FontFormatException, IOException {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
setDoubleBuffered(true);
Char = new Char();
timer = new Timer(5, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(Char.getImage(), Char.getX(), Char.getY(), this);
g.setColor(Color.white);
g.setFont( What goes here? ); // <------------
g.drawString("Amora Engine rev25 (acetech09)", 10, 20);
g.drawString(Char.getDebugStats(0), 10, 40);
g.drawString(Char.getDebugStats(1), 10, 60);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e) {
Char.move();
repaint();
}
}
Any help would be greatly appreciated. Thanks.
You could just do...
g.setFont(pixel);
But you might have better sucess with
g.setFont(pixel.deriveFont(Font.BOLD, 36f));
Are variations of....
Also, don't dispose of a Graphics context you did not create...
Graphics2D g2d = (Graphics2D)g;
/*...*/
// g.dispose();
Or
Graphics2D g2d = (Graphics2D)g.create();
/*...*/
g.dispose();
I'd also be loathed to override the paint method. Assuming you're using something like JComponent or JPanel, you should use paintComponent. If you're rendering directly yo a top level container (like JFrame), then I wouldn't. There are issues with double buffering and frame borders that won't make your life fun...
I'm also concerned about new Timer(5, this) - 5 milliseconds is close enough to 0 to make little difference. You'd be better of with something like 40, which should give you something like 25fps or 17 which will give you roughly 60fps...
It should be
g.setFont( this.pixel );
If this is not working, try:
Commenting out the setFont instruction.
Replacing Font.createFont with a reference to a Java standard font.
to rule out possible issues.

draw buffered image ontop of another buffered image

my goal is to draw some bufferedimage onto another. then all this stuff draw onto some other bufferedimage and so on. And finally draw this on top of a panel.
For now i'm trying to draw bufferedimage onto panel and nothing works. My bufferedimage looks completely white:
public class Main2 {
public static void main(String[] args) {
JFrame frame = new JFrame("asdf");
final JPanel panel = (JPanel) frame.getContentPane();
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
panel.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
somepaint(panel);
}
});
}
private static void somepaint(JPanel panel) {
BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
image.getGraphics().setColor(Color.red);
image.getGraphics().fillRect(0, 0, 200, 200);
Graphics2D graphics = (Graphics2D) panel.getGraphics();
graphics.setColor(Color.magenta);
graphics.fillRect(0, 0, 500, 500);
graphics.drawImage(image, null, 0, 0); // draws white square instead of red one
}
}
thanks
Re:
private static void somepaint(JPanel panel) {
BufferedImage image = new BufferedImage(200,200,BufferedImage.TYPE_INT_ARGB);
image.getGraphics().setColor(Color.red);
image.getGraphics().fillRect(0, 0, 200, 200);
Graphics2D graphics = (Graphics2D) panel.getGraphics();
This is not how you draw inside of a JPanel or JComponent.
Don't call getGraphics() on a component as the Graphics object returned will be short-lived, and anything drawn with it will not persist. Instead do your JPanel's drawing inside of its paintComponent(Graphics G) method override. You will need to create a class that extends JPanel in order to override paintComponent(...).
Most importantly, to see how to do Swing graphics correctly, don't guess. You'll want to read the Swing Graphics Tutorials first as it will require you to toss out some incorrect assumptions (I know that this is what I had to do to get it right).
You need to rectify your parameters in the drawImage() call. Change this:
graphics.drawImage(image, null, 0, 0);
to
graphics.drawImage(image, 0, 0,null);
Check the Java docs for more details.

Background Image in JTextPane

How do I set a background image to a JTextPane - some sort of a watermark.
I tried this option - creating a child class of JTextPane and use the paint method to draw the image.
But then the text is displayed "below" the image than above.
Is there any "standard" or "well known" way to do this?
(BTW, I tried (something silly?) making the content type "text/html", and setting the image as the background image of a <div> but it did not help.)
Here's a working example:
import javax.swing.*;
import java.awt.*;
public class ScratchSpace {
public static void main(String[] args) {
JFrame frame = new JFrame("");
final MyTextPane textPane = new MyTextPane();
frame.add(textPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static class MyTextPane extends JTextPane {
public MyTextPane() {
super();
setText("Hello World");
setOpaque(false);
// this is needed if using Nimbus L&F - see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6687960
setBackground(new Color(0,0,0,0));
}
#Override
protected void paintComponent(Graphics g) {
// set background green - but can draw image here too
g.setColor(Color.GREEN);
g.fillRect(0, 0, getWidth(), getHeight());
// uncomment the following to draw an image
// Image img = ...;
// g.drawImage(img, 0, 0, this);
super.paintComponent(g);
}
}
}
The important things to note:
your component must not be opaque...
so setOpaque(false);
override paintComponent(Graphics g), not paint.
paint your background, with an image
or drawing BEFORE calling
super.paintComponent(g);
If you want to master this stuff, I recommend reading "Filthy Rich Clients", a book all about how to bend Swing to your will.
Try changing the paint code to this.
public void paint(Graphics g)
{
g.setXORMode(Color.white);
g.drawImage(image,0, 0, this);
super.paint(g);
}
This would make your image to be painted before the text is rendered by the actual component's paint method.
Hmm., put a background image to the JFrame/JPanel containg the JTextPane,.. and keep the JTextPane transparent to some level.

Categories