I want to make a background for a game I have created.
I'm having some issues with fillRect.
Does getHeight and getWidth have to be in a certain order or should getX/Y/Height/Width be used at all?
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
//Graphical loop start
Rectangle rect = new Rectangle(0,0,1440,900) ;
g.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
//Graphical loop end
}
Firstly, you are using the Graphics object instead of Graphics2D. You need to set the paint colour first then use the fill method.
g2d.setPaint(Color.BLUE);
g2d.fill(new Rectangle2D.Double(0, 0, screenWidth, screenHeight));
Rectangle2D constructor arguments are: x-coordinate of top-left, y-coordinate of top-left, width, height
It is good practice to use the Graphics2D.fill() method which will accept any object that implements the Shape interface. This makes it easier to change a shape to a different one should you decide to do so.
Related
I am trying to paint a rounded rectangle around a JScrollPane. For the life of me I can't figure out how to do this! No matter what I try, the border is not visible. I have figured out that it is drawing BEHIND the contents and not over them. The only thing inside the scroll pane is a JPanel with some graphics painted onto it. Does anyone know how to fix this?
Here is the code I have tried to paint the border on the scroll pane:
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.black);
g2.setStroke(new BasicStroke(1));
g2.draw(new RoundRectangle2D.Double(0, 0, getWidth() - 1, getHeight() - 1, 10, 10));
}
I have also tried using paint instead of paintComponent but with no such luck!
You are actually painting outside of the components bounds, which is a big no-no, and is why you are having this problem. You should consider creating a custom Border or extending the component insets so that you have room to paint your outline
This needs nothing custom. Simply use a LineBorder(lineColor,thickness,roundedCorners)..
Creates a line border with the specified color, thickness, and corner shape.
Where..
roundedCorners - whether or not border corners should be round
Im making a small program that needs previous graphics to stay "put" and visible, even after a repaint causes a variable location to change.
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.red);
g.fillOval(mouseLocX,mouseLocY,30,30);
}
this is all i have in the paint class, and i want to change the mouseLocX and mouseLocY values, and call repaint without having the previous location there. Ive done this before, and most people want the opposite, but i forgot how. Im calling repaint from a MouseMotionListener using mouseDragged();
If you want to preserve what's already been painted so that you get a trail of red ovals instead of a single red oval as the mouse moves, then you shouldn't paint directly on the Graphics object provided by paint(). Instead, use a BufferedImage to store your state. Then render the BufferedImage onto the Graphic provided by paint().
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
public void paint(Graphics g) {
super.paint(g);
Graphics imageGraphics = image.getGraphics();
imageGraphics.setColor(Color.red);
imageGraphics.fillOval(mouseLocX,mouseLocY,30,30);
g.drawImage(image, 0, 0, null);
}
The BufferedImage provides the persistence for the previous draw operations.
I have painted a panel but when the program starts panel shows with delay. what should I do?
Toolkit kit = Toolkit.getDefaultToolkit();
Image img = kit.getImage(ResourceLoader.class.getResource("wood3.jpg"));
#Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
int width = getWidth();
int height = getHeight();
Graphics2D graphics = (Graphics2D) g;
graphics.setStroke(new BasicStroke(1));
graphics.drawImage(img, 0, 0, width, height, null, null);
this.updateUI();
repaint();
}
You are calling repaint() inside paintComponent(Graphics g) function: you understand that it is going to be a recursive painting-stack(request) call. Try printing a string inside your code and set your eyes on the console.
Use a Thread to read the image and let the swing run in EDT using SwingUtilities.invokeLater(Runnable). That way you won't have to await your application for the image to load.
As MadProgrammer has suggested, use Graphics.drawImage(x, y, width, height, ImageObserver) function. Try to set this as the Observer instead of null. #AndrewThompson had an example to show the usecase of ImageObserver. i have forgot the link however :P
I know how to fill a rectangle in Swing with a solid color:
Graphics2D g2d = bi.createGraphics();
g2d.setColor(Color.RED);
g2d.fillRect(0,0,100,100);
I know how to fill it with an image:
BufferedImage bi;
Graphics2D g2d = bi.createGraphics();
g2d.setPaint (new Color(r, g, b));
g2d.fillRect (0, 0, bi.getWidth(), bi.getHeight());
But how to fill rectangle of size 950x950 with some tiled pattern of size 100x100?
(pattern image should be used 100 times)
You're on the right track with setPaint. However, instead of setting it to a color, you want to set it to a TexturePaint object.
From the Java tutorial:
The pattern for a TexturePaint class is defined by a BufferedImage class. To create a TexturePaint object, you specify the image that contains the pattern and a rectangle that is used to replicate and anchor the pattern. The following image represents this feature:
If you have a BufferedImage for the texture, create a TexturePaint like so:
TexturePaint tp = new TexturePaint(myImage, new Rectangle(0, 0, 16, 16));
where the given rectangle represents the area of the source image you want to tile.
The constructor JavaDoc is here.
Then, run
g2d.setPaint(tp);
and you're good to go.
As #wchargin said, you can use TexturePaint. Here is an example:
public class TexturePanel extends JPanel {
private TexturePaint paint;
public TexturePanel(BufferedImage bi) {
super();
this.paint = new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(paint);
g2.fill(new Rectangle(0, 0, getWidth(), getHeight()));
}
}
I am trying to draw lines on coordinates system in Graphics2D. However, I find out that the part on line in negative area can not be shown. Is there anyway I can make the lines in negative area be seen?
Also, is there anyway I can convert direct of y-axis from downward to upward?
Graphics2D g2 = (Graphics2D) g;
g2.scale(1, -1);
g2.translate(0, -HEIGHT);
Can't work. Object disappears.
Thanks!
Ah, you are using the HEIGHT attribute. You should be using getHeight().
The code below produces this screenshot (g2.drawLine(0, 0, 100, 100)):
Code:
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame("Test");
frame.add(new JComponent() {
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
{
g2.translate(0, getHeight() - 1);
g2.scale(1, -1);
g2.drawLine(0, 0, 100, 100);
}
g2.dispose();
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
frame.setVisible(true);
}
As far as I understand Java2D you can't use negative coordinates. You always operate in the so-called "User Space" in Java2D. The translates coordinates of your position in "Device Space" might be negative, but this is invisible to you in Java. See also Java2D Tutorial - Coordinates and Graphics2D API.
You might be able to achieve what you want by subclassing Graphics2D and doing the those translation yourself.