I have a JPanel with a Grid Layout. In the "cells" of the grid I can put different elements (for example JButtons). There is no problems with that. But now I want to put a filled circle in some of the cells. I also would like to relate an ActionListener with these circles. In more details, if I click the circle it disappears from the current cell and appears in another one. How can I do it in Java? I am using Swing.
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// Assume x, y, and diameter are instance variables.
Ellipse2D.Double circle = new Ellipse2D.Double(x, y, diameter, diameter);
g2d.fill(circle);
...
}
Here are some docs about paintComponent (link).
You should override that method in your JPanel and do something similar to the code snippet above.
In your ActionListener you should specify x, y, diameter and call repaint().
/***Your Code***/
public void paintComponent(Graphics g){
/***Your Code***/
g.setColor(Color.RED);
g.fillOval(50,50,20,20);
}
g.fillOval(x-axis,y-axis,width,height);
Related
Im trying to draw a lightning bolt on my canvas but the line is only one pixel wide. How can I make the line thicker?
canvas.drawLine(lightningBoltArray[i].startXArray[k],lightningBoltArray[i].startYArray[k],lightningBoltArray[i].endXArray[k],lightningBoltArray[i].endYArray[k],paintHalfAlpha);
Is the trick somewhere in the settings of the applied paint?
If you're not doing something that allows you do to this you're probably painting incorrectly. Assuming that this is in a JPanel instance. It will also work for paint(Graphics g) in Canvas but most painting in Java is done using Swing
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// other stuff
float width = 3f;
BasicStroke stroke = new BasicStroke(width);
g2d.setStroke(stroke);
g2d.drawLine(...);
// more stuff
}
I have a problem in Java. I'm trying to create a 2D shape and rotate it but if I try to rotate it with the Graphics2D's rotate method, the whole canvas gets rotated.
I would like to only rotate a specific shape, not the whole canvas.
My current render method looks like this:
#Override
public void render(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(super.color);
g2d.rotate(Math.toRadians(5));
g2d.fillRect(100, 100, 50, 50);
}
How should I go about doing this?
In java awt or swing when you want to change painting of some component you usually have to override the method paint(Graphics g) (in awt) or paintComponent(Graphics g) (in swing).
This is usually (maybe allways - I'm not sure) done when you are creating the component for example:
JPanel jPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//... my implementation of paint, some transfromations, rotation, etc
}
};
Imagine that you have container of components which could for example consists of some JLabels, some JTextFields, some image. Which will be all put on one component.
By container I mean you have some list or map with ids or some similar structure in which are all components you will put on one JFrame.
The question is if I can change the painting method after creating with all of the components which are in this list in the moment when all of them are already created. For example I want do the rotation action (rotate), which is defined in Graphisc2D, with all of them.
So basicaly what I want is that I throught the list of componets I have and say:
"All of you (components) which are in the list will be rotated by some angle". Is that possible? If yes how?
Edit:
This is my not correctly working solution:
graphicalDisplayPanel = new JPanel() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g2d = (Graphics2D) g;
g2d.rotate(Math.PI, anchorx, anchory);
}
#Override
public void paintChildren(Graphics g) {
super.paintChildren(g);
Graphics2D g2d2 = (Graphics2D) g;
g2d2.rotate(Math.PI, anchorx, anchory);
}
};
JFrame jFrame = JFrame();
// ... setting dimension, position, visible etc for JFrame, it works correctly nonrotated
jFrame.setContentPane(graphicalDisplayPanel);
I have not tested this, but it seems like it would work. A JComponent's paint() method calls:
paintComponent(co);
paintBorder(co);
paintChildren(co);
where co is a Graphics object. In theory you create an image, retrieve the graphics object and then pass that into paintChildren(). you will have to call paintComponent() and paintBorder() yourself, if you do this. Then, just rotate the image and draw it into your component. You may have to crop the image or resize your component accordingly for this to work. It might look something like this:
BufferedImage myImage;
#Override
public void paint(Graphics g){
myImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
//using a transparent BufferedImage might not be efficient in your case
Graphics myGraphics = myImage.getGraphics();
super.paintComponent(g);
super.paintBorder(g);
super.paintChildren(myGraphics);
//rotation code here
// ...
//draw children onto your component
g.drawImage(myImage, 0, 0,getWidth(), getHeight(), null);
}
I hope I didn't make any mistakes, please let me know if this works.
So basicaly what I want is that I throught the list of componets I have and say: "All of you (components) which are in the list will be rotated by some angle".
If you want to rotate panel and therefore all the components on the panel as a single using then you need to do the custom painting in the paintComponent() method.
If you want to rotate, for example, individual images that each have a different angle of rotation then you can again do this in the paintComponent(...) method and change the angle for each component.
Or, in this second case you can use the Rotated Icon class. In this case the Icon is just added to a JLabel. Then you can change the degrees of rotation and repaint the label, so there is no custom painting (except in the Icon itself).
I have drawn some Graphics in a JPanel, like circles, rectangles, etc.
But I want to draw some Graphics rotated a specific degree amount, like a rotated ellipse. What should I do?
If you are using plain Graphics, cast to Graphics2D first:
Graphics2D g2d = (Graphics2D)g;
To rotate an entire Graphics2D:
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
To reset the rotation (so you only rotate one thing):
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated
Example:
class MyPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
AffineTransform old = g2d.getTransform();
g2d.rotate(Math.toRadians(degrees));
//draw shape/image (will be rotated)
g2d.setTransform(old);
//things you draw after here will not be rotated
}
}
In your paintComponent() overridden method, cast the Graphics argument to Graphics2D, call rotate() on this Graphics2D, and draw your ellipse.
In the code below I have simply used a mouse listener to get the XY coordinates of the the mouse, and then call for a repaint. Within the paint method I've drawn a rectangle using the same XY coordinates for position. The rectangle does follow but at a distance from the mouse pointer. I'd expect the top left corner of the rectangle to touch the mouse pointer.
Am I doing something wrong?
Why is there a distance between my mouse pointer and the Rectangle object?
public void mouseMoved(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
public class Canvas extends JPanel{
Canvas(){}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.red);
g2.fillRect(x, y, 50, 50);
}
}
Don't call your class Canvas, there is an AWT component by that name so it becomes confusing.
Custom painting is done by overriding the paintComponent() method of the JPanel, not the paint() method.
You don't show where you add the MouseListener to the panel. You are probably adding it to the frame instead.
If you need more help then post your SSCCE that demonstrates the problem.