java, when i use doublebuffering, painted image is expanded - java

when i use doublebuffering, painted image is expanded with white background.
Is there something wrong?
enter code here
private Image image_buffer;
private Graphics graphics_buffer
public void paint(Graphics g) {
super.paint(g);
buffering(img1, x1, 40, g);
}
public void buffering(Image img, int x, int y, Graphics g){
image_buffer = createImage(100,100);
graphics_buffer = image_buffer.getGraphics();
buffer.drawImage(img, x, y, this);
g.drawImage(image_buffer, x, y, this);
}

Why not replace your code with just:-
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img1, x1, 40, this);
}
The reason you have a white background is because you're creating a 100x100 "canvas", and drawing on that.

Related

Can't write animation for JComponent to smoothly change rectangle'a colors to grey

I faced the problem that I can't write an animation for color change at the Rectangle of JComponent. I use Java 8 and Swing. This animation is supposed to be slow, for any amount of seconds I would like to set. Nit: It feels like the base which I need to wrote his thing is definetly timer.
Here is the code snippet which describes my component's state:
class PBtn extends JComponent {
private LinkedList<MyBtn> myBtns = new LinkedList<>();
LinkedList<MyBtn> getMyBtns(){
return myBtns;
}
static class MyBtn extends Rectangle {
String name;
Color color;
MyBtn(int x, int y, int width, int height, String name, Color color) {
this.setBounds(x, y, width, height);
this.color = color;
}
}
void addBtn(int x, int y, int width, int height, String name, Color color) {
MyBtn myBtn = new MyBtn(x, y, width, height, name, color);
this.addMouseMotionListener(new MouseMotionListener() {
#Override
public void mouseDragged(MouseEvent e) {}
#Override
public void mouseMoved(MouseEvent e) {
if (myBtn.contains(e.getPoint())) {
setToolTipText(name);
}
ToolTipManager.sharedInstance().mouseMoved(e);
}
});
myBtns.add(myBtn);
repaint();
}
void clearBtns() {
myBtns.clear();
repaint();
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D) g;
myBtns.forEach(n -> {
graphics2D.setColor(n.color);
graphics2D.draw(n);
graphics2D.fill(n);
});
}
I tried to use the interpolation-based animation library bu it didn't help. Can you please give me suggestions how to write this code from scratch? Thanks!

Is it possible to write paint on screen program in java/swing?

I'm writing paint on screen program using Java Swing. It working on ubuntu linux. But windows shows black screen instead of transparent panel. I included similar example code. What is wrong in my code?
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Example {
public static final Color COLOR_TRANSPARENT = new Color(0,0,0,0);
public Example() {
Canvas drawArea = new Canvas();
drawArea.setBackground(COLOR_TRANSPARENT);
drawArea.setOpaque(true);
JWindow drawingFrame = new JWindow();
drawingFrame.setBackground(COLOR_TRANSPARENT);
drawingFrame.setContentPane(drawArea);
drawingFrame.pack();
drawingFrame.setSize(640, 460);
drawingFrame.setVisible(true);
drawingFrame.setLocationRelativeTo(null);
drawingFrame.setAlwaysOnTop(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(Example::new);
}
class Canvas extends JPanel{
private Image image;
private Graphics2D g2;
public Canvas() {
super();
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
g2.setPaint(Color.RED);
g2.fillOval(x-10, y-10, 20, 20);
repaint(x-10, y-10, 20, 20);
}
});
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image == null){
image = createImage(getWidth(), getHeight());
g2 = (Graphics2D) image.getGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(COLOR_TRANSPARENT);
clear();
}
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(image, 0,0, null);
}
public void clear(){
System.out.println("clearing canvas ");
g2.setComposite(AlphaComposite.Clear);
g2.setBackground(COLOR_TRANSPARENT);
g2.setColor(COLOR_TRANSPARENT);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.clearRect(0, 0, getWidth(), getHeight());
g2.setPaint(Color.RED);
g2.setComposite(AlphaComposite.SrcOver);
repaint();
}
}
}
Here is screenshot what I wanted.
Example code updated. Now code should work without any other additional code.
For windows I made a couple of changes:
image = createImage(getWidth(), getHeight());
image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
I used a BufferedImage to you can set the alpha values of the image to be transparent.
//public static final Color COLOR_TRANSPARENT = new Color(0,0,0,0);
public static final Color COLOR_TRANSPARENT = new Color(0,0,0,1);
I made the alpha value non-zero, because a value of zero means the Java application won't receive the MouseEvent because it is passed to the application under the window.

how to reflect an image in java

I am creating a program where I have to reflect an image horizontally and vertically. I have created a geometric shape image, but I am having a hard time figuring out how to flip my picture. I was wondering if someone could help me and tell me what to do to flip a picture. Thanks
My code so far is :
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
class DrawingDemoV3
{
Picture canvas = null;
Graphics g = null;
Graphics2D g2 = null;
DrawingDemoV3(int length, int height, Color color)
{
canvas = new Picture(length, height);
canvas.setAllPixelsToAColor(color);
g = canvas.getGraphics();
g2 = (Graphics2D)g;
g2.setColor(color);
}
public void drawAFilledOval(Color color, int x1, int y1, int width, int height)
{
g2.setColor(color);
g2.fillOval(x1, y1, width, height);
}
public void drawARectangle(Color color, int x1, int y1, int width, int height)
{
g2.setColor(color);
g2.drawRect(x1, y1, width, height);
}
public void drawAFilledRectangle(Color color, int x1, int y1, int width, int height)
{
g2.setColor(color);
g2.fillRect(x1,y1, width, height);
}
public void drawALine(Color color, int x1, int y1, int x2, int y2)
{
g2.setColor(color);
g2.drawLine(x1,y1,x2,y2);
}
public Picture getDrawing()
{
return canvas;
}
}
public class DrawingDemoTesterV3
{
public static void main(String[] args)
{
DrawingDemoV3 drawing1 = new DrawingDemoV3(200, 200, Color.BLACK);
drawing1.drawAFilledRectangle(Color.PINK, 90, 0, 20, 200);
drawing1.drawAFilledRectangle(Color.PINK, 0, 90, 200, 20);
drawing1.drawARectangle(Color.CYAN, 40, 40, 120, 120);
drawing1.drawALine(Color.ORANGE, 0,0, 200, 200);
drawing1.drawALine(Color.ORANGE, 200,0, 0, 200);
drawing1.drawAFilledOval(Color.YELLOW, 80, 80, 38, 36);
Picture picture1 = drawing1.getDrawing();
picture1.show();
}
}
Flipping/Reflection on an image.
The goal is to use Affine Transform for sequences of translations, scales, flips, rotations, and shears applied to 2D points and mapping.
While link only answers are typically frowned on, I don't want to just copy paste and rip off a perfectly explained answer and fill in his words with mine.

Drawing graphics2D java

I need to draw a circle between 2 lines. When I click on the panel, a line is drawn. When I click a second time, another line is drawn and the color of the line has changed. Also, at the 2nd click, a circle is drawn between the 2 lines. Then, there is a little animation with the circle. Someone helped me yesterday with this code :
List<Integer> yClicks = new ArrayList<>(); {
addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
yClicks.add(e.getY());
repaint();
}
});
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g.create();
for(int y : yClicks) {
g2d.draw(new Line2D.Double(0, y, getWidth(), y));
}
g2d.dispose();
}
I added this:
for(int y : yClicks) {
line1= new Line2D.Double(0, y, getWidth(), y);
g2d.setColor(Color.blue);
g2d.draw(line1);
int l2= (yClicks.get(1));
int Diameter=l2-y;
g2d.setColor(Color.yellow);
g2d.draw(new Line2D.Double(0,l2,getWidth(),l2));
g2d.draw(new Ellipse2D.Double(getWidth()/2,(Diameter),10,10));
}
g2d.dispose();
}
But there is a problem withe the code and maybe circle are drawn. I never used this form of a for loop, so i don't know. how to add something on the loop. I also tried if(y==2) but it doesn't do anything.

How to create a Rectangle object in Java using g.fillRect method

I need to create a rectangle object and then paint it to the applet using paint(). I tried
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
Then tried to paint it to the applet using
g.draw(r);
It didn't work. Is there a way to do this in java? I have scoured google to within an inch of its life for an answer, but I haven't been able to find an answer. Please help!
Try this:
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos,yPos,width,height);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
[edit]
// With explicit casting
public void paint (Graphics g) {
Rectangle r = new Rectangle(xPos, yPos, width, height);
g.fillRect(
(int)r.getX(),
(int)r.getY(),
(int)r.getWidth(),
(int)r.getHeight()
);
}
You may try like this:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
g.drawRect (x, y, width, height); //can use either of the two//
g.fillRect (x, y, width, height);
g.setColor(color);
}
}
where x is x co-ordinate
y is y cordinate
color=the color you want to use eg Color.blue
if you want to use rectangle object you could do it like this:
import java.applet.Applet;
import java.awt.*;
public class Rect1 extends Applet {
public void paint (Graphics g) {
Rectangle r = new Rectangle(arg,arg1,arg2,arg3);
g.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
g.setColor(color);
}
}
Note:drawRect and fillRect are different.
Draws the outline of the specified rectangle:
public void drawRect(int x,
int y,
int width,
int height)
Fills the specified rectangle. The rectangle is filled using the graphics context's current color:
public abstract void fillRect(int x,
int y,
int width,
int height)

Categories