paint

How to implement an infinite image loop?

对着背影说爱祢 提交于 2019-11-28 14:38:00
I am currently working on a 2D shooter with moving background. The background is just a simple image. I want to implement that the image is moving endless from top to bottom. I thought about determining the part of the image that is at the bottom outside the visible part of the frame and paint it on the top again. @Override public void paint(Graphics go) { Graphics2D g = (Graphics2D) go; g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.drawImage(this.imgBackground, 0, this.yBackground, this); } yBackground is the coordinate where the upper left corner of the image is drawn. It is altered

How to show a line being drawn from one point to another?

走远了吗. 提交于 2019-11-28 14:37:47
I used canvas.drawLine to show a line but I want user to be able to see it being drawn from one point to another and also, if possible, control the duration of the animation. (something like progress bar but this is my custom widget) You can use an AnimationController to control the animation duration. To draw the line "step by step" you can use a Tween (linear interpolation between a beginning and ending value). Then you just need to pass the current progress to your line painter and calculate the new width/height on each paint() when you call canvas.drawLine . Working example : import

why is my code executing paintComponent(Graphics page) twice?

流过昼夜 提交于 2019-11-28 14:33:49
This is getting on my nerves and it probably something silly on my part but I can't figure out why my paintComponent is being called twice, if you run my code it outputs REPEAT? REPEAT? twice, I don't want it to do that.. So why does it do it and how can I fix it? import java.util.Random; import javax.swing.JFrame; import javax.swing.JPanel; import java.awt.*; public class Main extends JPanel { public Main() { /*code here*/ } public void paintComponent(Graphics page) { clear(page); /*code here*/ System.out.println("REpEAT?"); } protected void clear(Graphics page) { super.paintComponent(page);

Displaying an ImageIcon

女生的网名这么多〃 提交于 2019-11-28 14:03:58
I am trying to display an image on a JPanel . I'm using an ImageIcon for rendering the image, and the image is in the same directory as the class file. However, the image is not being displayed, and there are no errors occuring. Could anyone please assist in working out what's wrong with my code... package ev; import java.awt.Graphics; import javax.swing.ImageIcon; import javax.swing.JPanel; public class Image extends JPanel { ImageIcon image = new ImageIcon("peanut.jpg"); int x = 10; int y = 10; public void paintComponent(Graphics g) { super.paintComponent(g); image.paintIcon(this, g, x, y);

Java 2D Game: repaint(); makes window grey

时间秒杀一切 提交于 2019-11-28 13:13:55
I'm trying to make a 2D game in Java, but when I call the repaint() method in a thread there's an odd grey-only window. Here's the source code I have so far: Spaceshooter.java package spaceshooter; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Spaceshooter extends JFrame implements KeyListener, Runnable { private Player player = new Player(5, 186, this); private boolean up, down; public Spaceshooter(String title) { super(title); this.setFocusable(true); this.addKeyListener(this); } @Override public void paint(Graphics

Gradually speeding a sprite

故事扮演 提交于 2019-11-28 13:11:17
I'm trying to make the sprite speed-up gradually on button press and not to move constant speed only. Also set a max-speed limit. I hope you understand what i mean. timer = new Timer(5, this); timer.start(); public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D)g; g2d.drawImage(image, x, y, this); //x,y = position Toolkit.getDefaultToolkit().sync(); g.dispose(); } private class TAdapter extends KeyAdapter { public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (key == KeyEvent.VK_LEFT) { dx = -1; } if (key == KeyEvent.VK_RIGHT) { dx = 1; } if (key == KeyEvent

Slow movement using paintComponent method

女生的网名这么多〃 提交于 2019-11-28 12:57:04
问题 I decided to re-write my game using Swing's painting technique paintComponent() method(someone on SO actually told me to use this method). I decided to use JPanel as the game's base instead of Canvas. My previous written game uses a Canvas but the game could not show up on my 64 bit desktop but could show up on my 32 bit labtop which is why this game had to be re-written. Problem now is, while the ship's movement works, the drawing seems awfully slow(unless it is my laptop problem?) compare

java JFrame graphics

心不动则不痛 提交于 2019-11-28 12:55:15
I have the following simple code in a JFrame constructor super(name); setBounds(0,0,1100,750); setLayout(null); setVisible(true); g = this.getGraphics(); int[] x =new int[]{65, 122, 77, 20, }; int[] y =new int[]{226, 258, 341, 310}; g.setColor(Color.RED); g.drawPolygon (x, y, x.length); System.out.println(g); I get the output on console as: sun.java2d.SunGraphics2D[font=java.awt.Font[family=Dialog,name=Dialog,style=plain,size=12],color=java.awt.Color[r=255,g=0,b=0]] But no red polygon drawn on JFrame but just the blank JFrame. Why ?? Dont override paint(..) in JFrame Rather add custom JPanel

Java drawing a line between the centre of components

烈酒焚心 提交于 2019-11-28 12:26:37
问题 I am trying to have a line drawn between the centre of two JLabels when the user clicks on one label, drags and releases on top of another. Which should work no matter what size the window is. But the lines are not centre, how can I fix it? The following example is working, but the lines seem to be offset by the boundaries of the JFrame, so they are not centre. I do not want to try to remove the JFrame border from the point calculation, since the real interface is more complex than the

Blur Effect (Wet in Wet effect) in Paint Application Using OpenGL-ES

末鹿安然 提交于 2019-11-28 11:49:31
I am developing Paint application using OpenGL-ES for iPhone and i want to implement Gaussian blur effect(Wet in Wet) for painting. Please have look at the image describing my requirement for Blur effect : I tried to search how for OpenGL function but did not get anything. Can anyone guide me to a right direction in this problem.. Any kind of help or suggestion will be highly appreciated.. Thanks.. You should be able to render the same brush stroke many times pixels apart to get the effect you want. If you jitter the renders with a Gaussian distribution you will get a Gaussian blur. This would