Graphics2d methods produce no output - java

I've been trying to get the Graphics2d object to work without success. I've searched for an answer on both the Oracle tutorial site and Stackoverflow without finding an answer.
The problem I have is that when I call the methods lineTo, fill, and drawRect, I get a blank grey square in my window, instead of the shapes that I want.
package main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class GraphicsTesting extends JPanel {
private static final long serialVersionUID = 6096199371167913312L;
static BufferedImage buffImag = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
static Graphics2D graff = buffImag.createGraphics();
Point2D.Double point = new Point2D.Double(10, 10);
static Graphics gra = buffImag.createGraphics();
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
GeneralPath gp = new GeneralPath(GeneralPath.WIND_EVEN_ODD, 4);
gp.moveTo(30, 55);
gp.lineTo(168, 384);
gp.lineTo(462, 81);
gp.lineTo(321, 423);
gp.lineTo(269, 243);
g2.setColor(new Color(112, 150, 134));
g2.fill(gp);
g2.setColor(new Color(56, 112, 232));
g2.draw(gp);
g2.setColor(new Color(152, 1, 210));
g2.drawRect(25, 152, 380, 405);
g2.drawImage(buffImag, 0, 0, 500, 0, 0, 500, 500, 500, null);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
GraphicsTesting gT = new GraphicsTesting();
frame.setContentPane(gT);
gT.paint(gra);
}
}

Your code seems fine and I tried to run it on my machine. It produces what you can see on the following screen shot. I think this is what you expect to get, right? Your problem might be coming from a faulty Java installation or an os-related issue. Which virtual machine are you using and on which operating system?
As a side note, your code is not complete though, as the following import is missing
import java.awt.geom.Point2D;

There are several issues with your code, but the main one causing your problem is that you are making the frame visible before adding the panel to it.
Move your setVisible(true) line to here:
frame.setContentPane(gT);
frame.setVisible(true);
gT.paint(gra);

Related

java how to draw a G-clef and F-clef using points or curves

I am making a music app using Graphics 2D, and I have managed to draw the stave and the music notes. I am now trying to draw a G-clef
and an F-clef
If there is another possible way to do it, I will appreciate.
NB: I have looked around for two days and I have seen questions that are similar but I haven't yet seen a solution.
Using points and curves is not the best way. Thanks to #David, I found out that unicodes work best. To use a unicode in printing a string,
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import javax.swing.JFrame;
public class UnicodeText {
public static void main(String[] args) {
JFrame f = new JFrame() {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
Font font = new Font("Bravura", Font.PLAIN, 32);
g2.setFont(font);
g2.drawString("\uD834\uDD1E", 40, 80);// Gclef
g2.drawString("\uD834\uDD22", 40, 80);// Fclef
}
};
f.setSize(200,200);
f.setVisible(true);
}
}
Download the "bravura" font here and find the unicode standard chart here
Hope it helps someone.

Java: Program doesn't save old graphics

So, I have been doing Java for a couple of months, and I'm now learing about Graphics. So, I am trying to make a paint kind of thing, where I can just draw with my mouse. Nothing really fancy, just something to get me started. The program just paints a small dot whenever I drag the mouse. It sort of works, except for the fact that it doesn't save my old dots. It just creates a new one! It would be kind if someone could help me with this problem:
Main class:
import javax.swing.JFrame;
public class Test{
public static void main(String args[]){
Ploofer ploof = new Ploofer();
PumpkinPie f = new PumpkinPie(ploof);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(1000,1000);
f.setResizable(false);
ploof.setSize(1000,1000);
f.add(ploof);
f.setVisible(true);
}
}
"Ploofer" class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Ploofer extends JPanel{
PumpkinPie pObj = new PumpkinPie(this);
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
this.setBackground(Color.WHITE);
g2d.setColor(new Color(190, 50, 0));
if(pObj.draw==true){
g2d.fillRect(pObj.x, pObj.y, 2, 2);
pObj.draw = false;
}
}
#Override
public void update(Graphics g){
paint(g);
}
//I tried to override update, but it didn't really help
}
"PumpkinPie" class:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Ploofer extends JPanel{
PumpkinPie pObj = new PumpkinPie(this);
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
this.setBackground(Color.WHITE);
g2d.setColor(new Color(190, 50, 0));
/* g2d.fillRect(475, 475, 50, 50);
g2d.drawString("^Red^", 484, 540); */
if(pObj.draw==true){
g2d.fillRect(pObj.x, pObj.y, 2, 2);
pObj.draw = false;
}
}
#Override
public void update(Graphics g){
paint(g);
}
//I tried to override update, but it didn't really help
}
It sort of works, except for the fact that it doesn't save my old dots. It just creates a new one!
Check out Custom Painting Approaches for the two common ways to do custom painting:
add objects to paint to an ArrayList and iterate through the list to paint all objects
draw to a BufferedImage and repaint the BufferedImage
In your case I would recommend approach 2.
//I tried to override update, but it didn't really help
Don't override update(). There is no reason to do that. That is an old AWT approach with is not needed in Swing.

Problems With Timer Animations

What I Want To Do: Animate the rectangles so that they go from the right of the screen to the left side of the screen. Here's the code for painting:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.Timer;
public class graphics extends JPanel{
public static Timer a;
public static int animation = 0;
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(new Color(40,40,40));
g.setColor(new Color(197,255,172));
g.fillRect(animation, 0, 800, 35);
g.setColor(new Color(141,229,123));
g.fillRect(animation, 35, 800, 35);
g.setColor(new Color(112,183,98));
g.fillRect(animation, 70, 800, 35);
g.setColor(new Color(84,137,73));
g.fillRect(animation, 105, 800, 35);
g.setColor(new Color(42,68,36));
g.fillRect(animation, 140, 800, 35);
g.setFont(new Font("Dekar Light", Font.PLAIN, 30));
g.setColor(Color.WHITE);
g.drawString("Graphics Test", 326, 300);
a = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
repaint();
int velx = 5;
animation = animation - velx;
System.out.println(animation);
}
});
a.start();
}
}
And here is the frame:
My Problem: As you can see, it seems that the rectangles move double the distance than what they last moved.
My Question: What am I doing wrong? I need to know if it's something with either the timer or the equation I'm using.
I need to know if it's something with either the timer
A painting method should only ever do painting!
It should NOT start the Timer. Every time you paint the component you start another Timer so you end up generating multiple repaint() requests. The RepaintManager will then combine multiple requests into a single repaint of the component.
The Timer should be started in the constructor of the class or you should create a startAnimation() method method and add it to your panel. Then that method can be invoked after the frame is made visible (or as required).
Also, class names should ALWAYS start with an upper case character. However you should NOT use "Graphics" since there is already a Java class with that name. Make your class name more descriptive.

Applet Shows Blank Screen in Browser

I have an applet that is packaged into a jar which although it runs (tested with print statements showing in console) it displays only a blank screen.
Here is the applet code:
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
public class Test extends JApplet {
JLayeredPane frame = new JLayeredPane();
JButton button = new JButton("Test");
JLabel backgroundLabel;
public void init() {
button.setBounds(10, 10, 100, 40);
backgroundLabel = new JLabel(){
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint(new GradientPaint(
new Point(0, 0),
new Color(90, 207, 233),
new Point(0, getHeight()),
Color.white));
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
};
backgroundLabel.setBounds(0, 0, getWidth(), getHeight());
frame.add(backgroundLabel, new Integer(0));
frame.add(button, new Integer(1));
add(frame);
}
}
And this is the html code:
<applet
id="clientApplet"
codebase="test"
code="Test.class"
archive="test.jar"
width="820" height="600">
Your browser does not support the <code>applet</code> tag.
</applet>
The applet works fine in Eclipse and no errors show up in the console. Can anyone please say what the problem might be?
It seems that an update has stopped the <applet> tag from functioning. I replaced it with <embed> and all is fine now.
<embed id="test"
type="application/x-java-applet;version=1.6"
width="256" height="256"
archive="test.jar"
code="Test.class"
codebase="test"
pluginspage="http://java.com/download/"
myParam="" />

Java Swing transparency drawing issues

Edit:
I submitted a bug for the below (it may take a a few days to become approved though):
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7043319
Some more details:
It works with Windows Sun JDK 1.6 versions 13 and 17
It fails on Ubuntu 11.04 x64 with both OpenJDK 1.6.0_22 and Sun JDK 1.6.0_24
What I want is to make a background image panel with additional panels on top of it (with additional components - e.g. JButtons, custom shapes, etc. - in them) and draw all that correctly. I'm using JLayeredPane for that purpose in my app, but for the sake of an example the below code should suffice. I'm open to suggestions about how to do what I want regardless of the below problem.
I'm running into the issue that the painting is behaving really weird. It doesn't repaint fully (e.g. only the top part above the image), it repaints in - from what I've noticed increasingly spaced - steps (e.g. 1st paint, 3rd paint, 9th paint, 21st paint, 64th paint, etc.). My guess is that I'm going too much into implementation here - is there anything obviously wrong with the below?
On a separate note, there are three commented lines below. Interestingly enough, uncommenting any of them and commenting the following line solves the problem. The images are with the following attributes (and it seems it doesn't matter which image - just the size):
cat.jpg JPEG 640x533 640x533+0+0 8-bit DirectClass 110KB 0.000u 0:00.000
cat-small.jpg JPEG 200x167 200x167+0+0 8-bit DirectClass 7.99KB 0.000u 0:00.000
Here's the Java code I'm having issues with:
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class SwingDrawingPrb {
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final JFrame frame = new JFrame("SwingDrawingPrb");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Container contentPane = frame.getContentPane();
frame.setLocation(550, 50);
frame.setSize(1000, 800);
frame.setVisible(true);
// ImageIcon image = new ImageIcon(SwingDrawingPrb.class.getResource("/cat-small.jpg"));
ImageIcon image = new ImageIcon(SwingDrawingPrb.class.getResource("/cat.jpg"));
final JPanel imagePanel = new JPanel() {
// Color trans = new Color(255, 0, 0, 255);
Color trans = new Color(255, 0, 0, 64);
protected void paintComponent(Graphics g) {
System.out.println("painting");
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(trans);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.blue);
g.drawLine(0, 0, 1000, 1000);
}
};
imagePanel.setBounds(0, 0, image.getIconWidth() + 200, image.getIconHeight() + 200);
imagePanel.setLayout(null);
// JLabel imageLabel = new JLabel("Hello, world!");
JLabel imageLabel = new JLabel(image);
imageLabel.setBounds(100, 100, image.getIconWidth(), image.getIconHeight());
imageLabel.addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
System.out.println("mouseMoved");
imagePanel.repaint();
}
});
imagePanel.add(imageLabel);
contentPane.add(imagePanel);
}
}
You need to add:
imagePanel.setOpaque(false);
See Backgrounds With Transparency for more information.

Categories