Drawing a simple circle with Java swing does not work - java

I think I am missing something really obvious but somehow this code does give me an empty window but it does not paint the red oval. What am I missing?
public class Test extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
g = this.getGraphics();
Graphics2D g2 = (Graphics2D) g;
// Anti-aliasing
g2.setColor(new Color(255, 0, 0));
g2.fillOval(0, 0, 20, 20);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Ball");
Test panel = new Test();
frame.getContentPane().add(panel);
frame.setPreferredSize(new Dimension(250, 200));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}

the paintComponent is not correct, remove this g = this.getGraphics();
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
Ellipse2D.Double circle = new Ellipse2D.Double(xR, yR, diameter, diameter);
g2d.fill(circle);
...
}

Related

Draw in jPanel inside a jDialog

I have jDialog, it is in BorderLayout, in "south", "north" and "center" I have jPanel with elements (with nothing in the "center"'s jPanel.
"center" jPanel is called Map.
I'm thing things like :
In the main
Graphics t = Map.getGraphics();
paintComponent(t);
Not in the main.
public void paintComponent(Graphics g){
super.paintComponents(g);
g.drawLine(0, 0, 50, 150);
}
I cannot draw anything. What is the mistake I have done?
JPanel Map = new JPanel() {
public void paintComponent( Graphics g ) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
Line2D line = new Line2D.Double(10, 10, 40, 40);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(10));
g2.draw(line);
}
};
getContentPane().add(Map, java.awt.BorderLayout.CENTER);
Map.add(new JLabel(pseudo));
pack();
in the dlg constructor.
thanks for you nohelp it worked

Java Increase Rectangle Thickness

How do I increase the thickness of the rectangle when using g.drawRect? I want the blue rectangle to have a thick line.
This is my code -
if(selectedApartment == 0)
{
g.setColor(Color.red);
g.fillRect (150, 120,aptWidth, aptHeight);
**g.setColor(Color.blue);
g.drawRect(150, 120, aptWidth, aptHeight);**
g.setColor(Color.black);
g.setFont(new Font("BoldFont",Font.BOLD,14));
g.drawString("1", 150+15, 120+25);
}
else if (selectedApartment != 0)
{
g.setColor(Color.red);
g.fillRect (150, 120,aptWidth, aptHeight);
g.setColor(Color.black);
g.drawRect(150, 120, aptWidth, aptHeight);
g.setColor(Color.black);
g.setFont(new Font("BoldFont",Font.BOLD,14));
g.drawString("1", 150+15, 120+25);
}
You should use setStroke to set a stroke of the Graphics2D object.
Here's an example of this code segment in a program:
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
cp.add(new JComponent() {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.draw(new Line2D.Float(30, 20, 80, 90));
}
});
jf.setSize(300, 200);
jf.setVisible(true);
}
}
(Note that the setStroke method is not available in the Graphics object. You have to cast it to a Graphics2D object.)

How to decrease a height of a rectangle? (Java, AWT)

I'm beginner in Java. So, please help me with my problem.
I can do animation when a rectangle's height increases. But I have problem with decreasing rectangle's height. Please look at this code:
public class Animation extends JPanel implements ActionListener {
Timer timer;
int i = 100;
public Animation() {
timer = new Timer(10, this);
timer.start();
}
public void paint(Graphics g) {
Graphics2D g2d1 = (Graphics2D) g;
g2d1.fillRect(0, 100, 30, i);
}
public static void main(String[] args) {
JFrame frame = new JFrame("animation");
frame.add(new Animation());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 800);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
--i;
repaint();
}
}
Please help me.
Best regards
Pawel
It isn't clearing the screen between draws, so it draws over the old larger rectangle.
Try this:
public void paint(Graphics g) {
Graphics2D g2d1 = (Graphics2D) g;
g.setColor(getBackground());
g.fillRect(0,0,getWidth(),getHeight()); // draw a rectangle over the display area in the bg color
g.setColor(Color.BLACK);
g2d1.fillRect(0, 100, 30, i);
}
Or:
public void paint(Graphics g) {
super.paint(g); // call superclass method, which does clear the screen
Graphics2D g2d1 = (Graphics2D) g;
g2d1.fillRect(0, 100, 30, i);
}
And as camickr pointed out below, custom painting should be done in paintComponent not paint, so you should change the name of the method to paintComponent.

JPanel on JWindow is not transparent if `set setOpaque(false);` - why?

How to make JPanel to be transparent in this example? The gradient background is not visible:
package test;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JWindow;
public class PaintJPanelOnJWindow extends JWindow {
public PaintJPanelOnJWindow() {
JPanel panel = new JPanel();
panel.setPreferredSize(new Dimension(350, 120));
panel.setMinimumSize(new Dimension(350, 120));
panel.setMaximumSize(new Dimension(350, 120));
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(false);
JLabel someText = new JLabel("I'm not transparent and my JPanel too :(");
someText.setOpaque(false);
panel.add(someText);
add(panel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
#Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
try {
int w = getWidth(), h = getHeight();
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
g2d.fillRect(0, 0, w, h);
} finally {
g2d.dispose();
}
super.paint(g);
}
}
The immediate problem is that
super.paint(g);
is being called after the custom painting code in the paint method which will cause any previous painting to be lost. Calling panel.setOpaque(false) has no effect what is done in the paint method. Calling setOpaque for any of the components in the question is unnecessary - by default the backgrounds are displayed when custom painting is correctly implemented.
This should be done by overriding the paintComponent method. This means creating a new JPanel and placing the custom painting functionality there rather than in a top level container such as a JWindow.
Example:
public class PaintJPanelApp {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Gradient App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setUndecorated(true);
frame.setLocationRelativeTo(null);
JLabel someText = new JLabel("I AM transparent and my JPanel too :)");
GradientPanel gradientPanel = new GradientPanel();
gradientPanel.add(someText);
frame.add(gradientPanel);
frame.pack();
frame.setVisible(true);
}
});
}
static class GradientPanel extends JPanel {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int w = getWidth();
int h = getHeight();
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
g2d.fillRect(0, 0, w, h);
}
}
}
From JavaDocs:
create(): Creates a new Graphics object that is a copy of this Graphics object.
Which means that the Graphics object g2d is not referring to the JWindow Graphics object, it is referring to a copy of the JWindow Graphics object.
You need to change
Graphics2D g2d = (Graphics2D) g.create(); //creates a copy, wrong object
To
Graphics2D g2d = (Graphics2D) g; //refers to the right Graphics object
UPDATE
However, this is not the right way to do it. You should override JPanel's paintComponent method instead of breaking the window's paint process. First, remove the paint() method override from your class. Then, initialize the JPanel as follows:
JPanel panel = new JPanel() {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
int w = getWidth(), h = getHeight();
g2d.setPaint(new GradientPaint(0, 0, Color.RED, 0, h, Color.WHITE));
g2d.fillRect(0, 0, w, h);
}
};

How to smooth out corners in rounded rectangle, Swing?

I'm getting extremely pixilated corners when i try to make a rounded rectangle. Is there any way to smooth them out?
Here's an image (Notice the corners):
Here is the code for the Button that I subclass and override the paint method (The one with the pixilated corner):
public class ControlButton extends JButton {
public final static Color BUTTON_TOP_GRADIENT = new Color(176, 176, 176);
public final static Color BUTTON_BOTTOM_GRADIENT = new Color(156, 156, 156);
public ControlButton(String text) {
setText(text);
}
public ControlButton() {
}
#Override
protected void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g.create();
g2.setPaint(new GradientPaint(
new Point(0, 0),
BUTTON_TOP_GRADIENT,
new Point(0, getHeight()),
BUTTON_BOTTOM_GRADIENT));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
g2.dispose();
}
}
Try this:
RenderingHints qualityHints = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON );
qualityHints.put(
RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY );
g2.setRenderingHints( qualityHints );
Take a look at the documentation:
http://docs.oracle.com/javase/tutorial/2d/advanced/quality.html
Code:
import javax.swing.*;
import java.awt.*;
public class ControlButton extends JButton {
public final static Color BUTTON_TOP_GRADIENT = new Color(176, 176, 176);
public final static Color BUTTON_BOTTOM_GRADIENT = new Color(156, 156, 156);
public ControlButton(String text) {
setText(text);
}
public ControlButton() {
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
RenderingHints qualityHints =
new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(qualityHints);
g2.setPaint(new GradientPaint(new Point(0, 0), BUTTON_TOP_GRADIENT, new Point(0, getHeight()),
BUTTON_BOTTOM_GRADIENT));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
g2.dispose();
}
public static void main(String args[]) {
JFrame frame = new JFrame("test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ControlButton("Hello, World"));
frame.pack();
frame.setVisible(true);
}
}
I did this:
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
I feel it gave me smoother edges than Dave Jarvis' method but I could be wrong.

Categories

Resources