stacked JPanel repaint not correctly - java

I tried to make game with 2 panel stack each other, first panel is for canvas where all tiles and all game object is drawing on it and the second one is for the top layer frame where I have png image with transparency for the background and will contain JTextBox, JTabbedPanel and JLabel
My problems is when i repaint the canvas, part of the top frame becoming covered by a canvas.
Why is that? and also when i add JButton on canvas, the JButton is draw on top of my top frame panel.
Edit:
I have tried using JLayeredPane but still give me same result.
// initComponent from MyGame
private void initComponent() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Game");
this.setSize(320, 240);
this.setLayout(null);
this.setUndecorated(true);
this.setResizable(false);
JLayeredPane layered = this.getLayeredPane();
layered.add(new Canvas(), 0);
layered.add(new TopFrame(), 1);
}
This top frame and my canvas:
What I get | what I want
Main
#SuppressWarnings("serial")
public class MyGame extends JFrame {
public MyGame() {
this.initComponent();
}
private void initComponent() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Game");
this.setSize(320, 240);
this.setLayout(null);
this.setUndecorated(true);
this.setResizable(false);
this.add(new TopFrame());
this.add(new Canvas());
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame game = new MyGame();
game.setVisible(true);
}
});
}
}
TopFrame
#SuppressWarnings("serial")
public class TopFrame extends JPanel {
private static final int FRAME_WIDTH = 320;
private static final int FRAME_HEIGHT = 240;
private BufferedImage bgImage;
public TopFrame() {
this.initComponent();
}
private void initComponent() {
this.setLayout(null);
this.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
bgImage = new BufferedImage(FRAME_WIDTH, FRAME_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
try {
bgImage = ImageIO.read(new File("FRAME.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
}
Canvas
#SuppressWarnings("serial")
public class Canvas extends JPanel {
private static final int CANVAS_WIDTH = 227;
private static final int CANVAS_HEIGHT = 240;
BufferedImage image;
BufferedImage imgBuffer;
Graphics2D g2d;
public Canvas() {
this.initComponent();
}
private void initComponent() {
Timer timer = new Timer();
this.setBounds(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
imgBuffer = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
g2d = imgBuffer.createGraphics();
try {
image = ImageIO.read(new File("tile.png"));
} catch (IOException e) {
e.printStackTrace();
}
timer.schedule(new TimerTask(){
#Override
public void run() {
drawComponent();
repaint();
}
}, 0, 100);
}
public void drawComponent() {
/* will do tile drawing */
g2d.drawImage(image, 0, 0, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imgBuffer, 0, 0, this);
}
}
Is it possible to get my canvas with all contained object drawn at the bottom?

[Solved] using JLayeredPane and make TopFrame setOpaque(false);
I have wrong code when try using JLayeredPane.
My final code working code now:
Main
#SuppressWarnings("serial")
public class MyGame extends JFrame {
public MyGame() {
this.initComponent();
}
private void initComponent() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Game");
this.setSize(320, 240);
this.setLayout(null);
this.setUndecorated(true);
this.setResizable(false);
JLayeredPane layered = this.getLayeredPane();
layered.add(new Canvas(), new Integer(0));
layered.add(new TopFrame(), new Integer(1));
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame game = new MyGame();
game.setVisible(true);
}
});
}
}
TopFrame
#SuppressWarnings("serial")
public class TopFrame extends JPanel {
private static final int FRAME_WIDTH = 320;
private static final int FRAME_HEIGHT = 240;
private BufferedImage bgImage;
public TopFrame() {
this.initComponent();
}
private void initComponent() {
this.setLayout(null);
this.setBounds(0, 0, FRAME_WIDTH, FRAME_HEIGHT);
this.setOpaque(false);
bgImage = new BufferedImage(FRAME_WIDTH, FRAME_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
try {
bgImage = ImageIO.read(new File("FRAME.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paintComponent(Graphics g) {
// super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
}
Canvas
#SuppressWarnings("serial")
public class Canvas extends JPanel {
private static final int CANVAS_WIDTH = 227;
private static final int CANVAS_HEIGHT = 240;
BufferedImage image;
BufferedImage imgBuffer;
Graphics2D g2d;
public Canvas() {
this.initComponent();
}
private void initComponent() {
Timer timer = new Timer();
this.setBounds(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
imgBuffer = new BufferedImage(CANVAS_WIDTH, CANVAS_HEIGHT, BufferedImage.TYPE_4BYTE_ABGR_PRE);
g2d = imgBuffer.createGraphics();
try {
image = ImageIO.read(new File("tile.png"));
} catch (IOException e) {
e.printStackTrace();
}
timer.schedule(new TimerTask(){
#Override
public void run() {
drawComponent();
repaint();
}
}, 0, 100);
}
public void drawComponent() {
/* will do tile drawing */
g2d.drawImage(image, 0, 0, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(imgBuffer, 0, 0, this);
}
}

Related

How to clip a BufferedImage with a shape created with Graphics2D?(Java)

Currently, I'm working on my first Java application with which a user can look through photos, cut them and rotate. I have an issue with clipping an image. What I want to achieve is the following:
User clicks on the "Cut" option
Rectangle shape called by repaint method appears on the image
By stretching the rectangle user chooses the area for cutting
When the user releases the mouse(which stops stretching the rectangle) the area that is surrounded with the rectangle is left and all the rest of the image is cut.
As of for now I have several issues:
My image is centralized on JLabel which in its turn is added to JPanel and the last is added to JFrame, so now, when I want to add a rectangle above JLable (so it is to be located on the picture) it's invisible and is added only on JPanel directly.
I drew an image with paintComponent but can't figure out how to move and stretch it and repaint the rectangle again.
Below is the part of my code which (I hope) will describe my problems more precisely:
public class GraphicalUserInterface {
static JPanel background;
static JLabel labelIcon;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GraphicalUserInterface().go();
}
});
}
public void go() {
buildGui();
}
public void buildGui() {
frame = new JFrame("PicMove");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
background = new JPanel(layout);
/**To center picture on the background**/
labelIcon = new JLabel();
labelIcon.setHorizontalAlignment(JLabel.CENTER);
labelIcon.setVerticalAlignment(JLabel.CENTER);
background.add(BorderLayout.SOUTH, bottom);
background.add(BorderLayout.PAGE_START, bar);
background.add(BorderLayout.CENTER, labelIcon);
background.add(BorderLayout.EAST, chatPanel);
frame.getContentPane().add(BorderLayout.CENTER, background);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
frame.setSize(1300, 1200);}
static class CutImage extends JPanel implements Runnable {
boolean clip;
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if (clip) {
BasicStroke bs = new BasicStroke(50, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
10, null, 0);
g2d.setStroke(bs);
QuadCurve2D.Float qc = new QuadCurve2D.Float(20, 50, 100, 140, 460, 170);
g2d.setClip(qc);
}
BasicStroke bs = new BasicStroke(5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
10, new float[]{10}, 0);
g2d.setStroke(bs);
g2d.drawRect(260, 50, 80, 120);
}
#Override
public void run() {
CutImage cutPanel = new CutImage();
GraphicalUserInterface.background.add(cutPanel).repaint();
}
}
public class PicChanges implements Runnable{
static BufferedImage newImage;
static File [] selectedFile;
static int currentImage;
FileNameExtensionFilter filter;
JFileChooser fileChooser;
public void openPic() {
currentImage = 0;
try {
fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new java.io.File((System.getProperty("user.home"))));
filter = new FileNameExtensionFilter("*.images", "jpg", "gif", "png");
fileChooser.addChoosableFileFilter(filter);
fileChooser.setMultiSelectionEnabled(true);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.OPEN_DIALOG) {
selectedFile = fileChooser.getSelectedFiles();
for (File image : selectedFile) {
if ((image.isFile()) && (selectedFile.length > 0)){
newImage = ImageIO.read(selectedFile[0]);
GraphicalUserInterface.labelIcon.setIcon(new ImageIcon(
new ImageIcon(newImage).getImage().getScaledInstance(
450, 620, Image.SCALE_DEFAULT)));
} else if (result == JFileChooser.CANCEL_OPTION) {
System.out.println("No Pics Selected");
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void run() {
Thread.currentThread().interrupt();
openPic();
}
public static void nextPic() {
currentImage++;
try {
newImage = ImageIO.read(selectedFile[currentImage]);
} catch (IOException e) {
e.printStackTrace();
System.out.println("No pictures left");
System.out.println("next"+currentImage);
}
GraphicalUserInterface.labelIcon.setIcon(new ImageIcon(
new ImageIcon(newImage).getImage().getScaledInstance(
450, 620, Image.SCALE_DEFAULT)));
}
static class NextPicture implements Runnable{
#Override
public void run() {
Thread.currentThread().interrupt();
nextPic();
}
}
public static void previousPic () {
currentImage--;
try {
newImage = ImageIO.read(selectedFile[currentImage]);
} catch (IOException e) {
e.printStackTrace();
System.out.println("previous "+currentImage);
}
GraphicalUserInterface.labelIcon.setIcon(new ImageIcon(
new ImageIcon(newImage).getImage().getScaledInstance(
450, 620, Image.SCALE_DEFAULT)));
}
static class PreviousPic implements Runnable{
#Override
public void run() {
Thread.currentThread().interrupt();
previousPic();
}
}
}
My idea was to add MouseListeners but can I add it to the shape created with Graphics2D?
I would be greatful for the help :)
Thank you
Being in search of finding a solution to this question I've asked two more questions(maybe it will be helpful for someone) :
1) Why BufferedImage is not cut according to the drawn in paintComponent method rectangle(its height is calculated wrong)?
2) Repaint() method doesn't invoke paint() & paintComponent() methods one by one, only paintComponent () method is working
that further helped me to find out the way.
My solution is to create a separate frame for displaying copied BufferedImage in it and on this frame to draw a rectangle with the help of MouseListeners. This is the piece of code:
public class ImageScreenShot extends JFrame implements MouseListener, MouseMotionListener {
#Override
public Dimension getPreferredSize() {
return super.getPreferredSize();
}
private static Thread screenShotThread;
public static Thread getScreenShotThread() {
return screenShotThread;
}
public static void setScreenShotThread(Thread screenShot) {
ImageScreenShot.screenShotThread = screenShot;
}
private int drag_status = 0, c1, c2, c3, c4;
public int getC1() {
return c1;
}
public int getC2() {
return c2;
}
public int getC3() {
return c3;
}
public int getC4() {
return c4;
}
class AdditionalPanel extends JLabel {
private BufferedImage img;
public BufferedImage getImg() {
return img;
}
public AdditionalPanel(BufferedImage img) {
this.img = img;
setPreferredSize(new Dimension(2560, 1600));
getPreferredSize();
setLayout(null);
}
#Override
public Dimension getPreferredSize() {
return super.getPreferredSize();
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
System.out.println("Additional panel class paint method was invoked");
}
}
public void cut() {
AdditionalPanel apanel = new AdditionalPanel(PicChanges.getNewImage());
JScrollPane scrollPane = new JScrollPane(apanel);
scrollPane.addMouseMotionListener(this);
scrollPane.addMouseListener(this);
getContentPane().add(scrollPane, BorderLayout.CENTER);
setPreferredSize(new Dimension(2560, 1600));
getPreferredSize();
pack();
setVisible(true);
}
private void draggedScreen() throws Exception {
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(c1, c2, w, h));
File save_path = new File("screen1.jpg");
ImageIO.write(img, "JPG", save_path);
GraphicalUserInterface.getLabelIcon().setIcon(new ImageIcon(new ImageIcon(img).getImage().getScaledInstance(img.getWidth(), img.getHeight(), Image.SCALE_SMOOTH)));
JOptionPane.showConfirmDialog(this, "Would you like to save your cropped Pic?");
System.out.println("Cropped image saved successfully.");
}
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent arg0) {
repaint();
c1 = arg0.getXOnScreen();
c2 = arg0.getYOnScreen();
System.out.println("pressed");
}
#Override
public void mouseReleased(MouseEvent arg0) {
repaint();
if (drag_status == 1) {
c3 = arg0.getXOnScreen();
c4 = arg0.getYOnScreen();
try {
repaint();
draggedScreen();
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void mouseDragged(MouseEvent arg0) {
repaint();
drag_status = 1;
c3 = arg0.getXOnScreen();
c4 = arg0.getYOnScreen();
}
#Override
public void mouseMoved(MouseEvent arg0) {
}
#Override
public void paint(Graphics g) {
super.paint(g);
int w = c1 - c3;
int h = c2 - c4;
w = w * -1;
h = h * -1;
if (w < 0)
w = w * -1;
g.setColor(Color.RED);
g.drawRect(c1, c2, w, h);
System.out.println("Paint component was invoked in imagescreenshot class");
}
P.S. I know that adding JFrame is not the best solution but I'm still in search for the best way to implement it so do not hesitate to comment on my code and tell what is wrong or what is good)

PaintComponent() not called despite setPreferredSize of JPanel

I am trying to understand why the following short piece code does not work.
I understand that when there are no Layout or the size of the component is 0, the paint component method isn't called.
But this isn't the case here.
Can you explain why I can't set the background for this?
public class Login extends JPanel {
private BufferedImage bgImage;
public Login() {
super();
initImages();
setLayout(new BorderLayout());
setPreferredSize(new Dimension(600, 600));
add(new JLabel("Hi"), BorderLayout.CENTER);
}
private void initImages() {
try {
bgImage = ImageIO.read(new File("images/login.jpg"));
System.out.println("image loaded");
} catch (IOException e) {
e.printStackTrace();
System.out.println("image not loaded");
}
}
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.drawImage(bgImage, 0, 0, null);
System.out.println("repaint");
}
public static void createAndShowGui() {
JFrame frame = new JFrame();
Login login = new Login();
frame.add(login, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
If you want this to work, then you will need to change...
#Override
public void paintComponents(Graphics g) {
super.paintComponents(g);
g.drawImage(bgImage, 0, 0, null);
System.out.println("repaint");
}
to something more like...
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
paintComponent is responsible for painting the "bottom" layer of the component, paintComponents is responsible for painting the children

Java Canvas when I resize the JFrame, the canvas stops drawing

In more detail, I have a componentResized event attached to the JFrame (which contains the canvas, and nothing else), and in that event a call a method which sets the bounds of the canvas accordingly. This works fine, except that while I'm resizing the canvas, it doesn't show anything. I just see the back of the JFrame. Once I've stopped resizing the JFrame, the canvas paints fine again.
public class MyCanvas implements ComponentListener {
public static void main(String[] args) {
new MyCanvas("MyCanvas",new Dimension(300,300));
}
private static final int frameRate = 30;
private JFrame frame;
private JPanel panel;
private Canvas canvas;
private BufferStrategy strategy;
private int delta;
private boolean running = false;
private int frameCount = 0;
public MyCanvas(String name, Dimension size) {
frame = new JFrame(name);
panel = (JPanel) frame.getContentPane();
canvas = new Canvas();
frame.setSize(size);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(size);
panel.setLayout(null);
canvas.setBounds(0,0,size.width,size.height);
panel.add(canvas);
canvas.setIgnoreRepaint(true);
frame.setResizable(true);
frame.pack();
frame.addComponentListener(this);
canvas.createBufferStrategy(2);
strategy = canvas.getBufferStrategy();
running = true;
frame.setVisible(true);
long lastLoopTime = 0;
while (running) {
frameCount++;
delta = (int) (System.currentTimeMillis() - lastLoopTime);
lastLoopTime = System.currentTimeMillis();
Graphics2D graphics = (Graphics2D) strategy.getDrawGraphics();
graphics.setColor(Color.black);
graphics.fillRect(0,0,getSize().width,getSize().height);
graphics.dispose();
strategy.show();
try {
Thread.sleep(1000/frameRate);
} catch (InterruptedException e) {}
}
}
public final Dimension getSize() {
return frame.getSize();
}
public final void setSize(Dimension size) {
frame.setSize(size);
canvas.setBounds(0,0,size.width,size.height);
}
public synchronized void componentResized(ComponentEvent e) {
setSize(frame.getSize());
}
public synchronized void componentHidden(ComponentEvent e) {
// unused
}
public synchronized void componentShown(ComponentEvent e) {
// unused
}
public synchronized void componentMoved(ComponentEvent e) {
// unused
}
}
Edit
After tweaking with the code for a while, I have come up with a solution:
public class MyCanvas {
public static void main(String[] args) {
new MyCanvas("MyCanvas",new Dimension(400,400));
}
private static final int frameRate = 1000 / 30;
private JFrame frame;
private JPanel panel;
private int delta;
private long lastLoopTime;
private boolean running = false;
private int frameCount = 0;
private BufferedImage backBuffer = null;
private int lastPaintFrame = -1;
public MyCanvas(String name, Dimension size) {
frame = new JFrame(name);
panel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
redraw(g);
}
};
frame.setContentPane(panel);
frame.setSize(size);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(size);
panel.setLayout(null);
frame.setResizable(true);
running = true;
frame.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
backBuffer = new BufferedImage(screenSize.width,screenSize.height,BufferedImage.TYPE_INT_ARGB);
lastLoopTime = System.nanoTime();
while (running) {
long thisLoopTime = System.nanoTime();
delta = (int) ((thisLoopTime - lastLoopTime) / 1000000);
draw(backBuffer.getGraphics());
frameCount++;
lastLoopTime = thisLoopTime;
redraw(panel.getGraphics());
try {
Thread.sleep(1000/30);
} catch (InterruptedException e) {}
}
}
private final void redraw(Graphics g) {
if (g != null && backBuffer != null) {
g.drawImage(backBuffer,0,0,null);
}
}
int x = 30;
public final void draw(Graphics g) {
g.setColor(Color.darkGray);
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.gray);
g.fillRect(0,0,500,500);
g.setColor(Color.blue);
g.fillRect(x,30,300,300);
x++;
}
public final Dimension getSize() {
return frame.getSize();
}
public final void setSize(Dimension size) {
frame.setSize(size);
}
}
However, this is not quite solved yet, because it still has odd little graphical glitches which only seem to appear when redraw is called from panel's paintComponent method, though not consistently. Those glitches manifest themselves as odd rectangles of color (usually black or grey) which promptly disappear again. I'm really not sure of what it could be... maybe problems with the double buffering? BTW, if I threw a runtime exception in paintComponent, it worked perfectly.
If this should be moved to a new question, please let me know.
I found the solution: different loops for draw and update:
public class MyCanvas {
public static void main(String[] args) {
new MyCanvas("MyCanvas",new Dimension(400,400));
}
private static final int frameRate = 1000 / 30;
private JFrame frame;
private JPanel panel;
private int delta;
private long lastLoopTime;
private volatile boolean running = false;
private int frameCount = 0;
public MyCanvas(String name, Dimension size) {
frame = new JFrame(name);
panel = new JPanel() {
protected void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
if (running) repaint();
}
};
frame.setContentPane(panel);
frame.setSize(size);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(size);
panel.setLayout(null);
frame.setResizable(true);
running = true;
frame.setVisible(true);
lastLoopTime = System.nanoTime();
new Thread(()->{
while (running) {
update();
frameCount++;
try {
Thread.sleep(frameRate);
} catch (InterruptedException e) {}
}
},"Game Loop").start();
}
int x = 30;
public final void update() {
x++;
}
public final void draw(Graphics g) {
g.setColor(Color.darkGray);
g.fillRect(0,0,getSize().width,getSize().height);
g.setColor(Color.gray);
g.fillRect(0,0,500,500);
g.setColor(Color.blue);
g.fillRect(x,30,300,300);
}
public final Dimension getSize() {
return frame.getSize();
}
public final void setSize(Dimension size) {
frame.setSize(size);
}
}

How to draw on the JFrame?

I have a pdf file, which is rendered as an image and added as a JLabel in the JFrame. Now i want to draw on the contents in the JFrame by mouseclick and keystrokes. And these drawings should appear on the image(JLabel). I have pasted my code below. Can anyone please help me to figure it out!!!
public class LinePanel extends JPanel{
public static final String RESOURCE = "G:/resource/A0TestFile.pdf";
private MouseHandler mouseHandler = new MouseHandler();
private Point p1;
private Point p2;
private Point p3;
private Point p4;
ArrayList<Point> points = new ArrayList<Point>();
public LinePanel() {
this.addMouseListener(mouseHandler);
this.addMouseMotionListener(mouseHandler);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setStroke(new BasicStroke(1,
BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL));
crop(g);
}
public void crop(Graphics g){
if(points != null && points.size()>0){
for(int i=0;i<points.size();i++){
p1 = points.get(i);
g.drawLine(p1.x, p1.y, p1.x-20, p1.y);
g.drawLine(p1.x, p1.y, p1.x, p1.y+20);
}
for( i=1;i<points.size();i++){
p2 = points.get(i);
g.drawLine(p2.x, p2.y, p2.x-20, p2.y);
g.drawLine(p2.x, p2.y, p2.x, p2.y-20);
}
for( i=2;i<points.size();i++){
p3 = points.get(i);
g.drawLine(p3.x, p3.y, p3.x+20, p3.y);
g.drawLine(p3.x, p3.y, p3.x, p3.y-20);
}
for( i=3;i<points.size();i++){
p4 = points.get(i);
g.drawLine(p4.x, p4.y, p4.x+20, p4.y);
g.drawLine(p4.x, p4.y, p4.x, p4.y+20);
}
}
}
private class MouseHandler extends MouseAdapter {
#Override
public void mousePressed(MouseEvent e) {
if(points.size()<4){
points.add(e.getPoint());
repaint();
}
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseDragged(MouseEvent e) {
}
}
private class ControlPanel extends JPanel {
private static final int DELTA = 1;
public ControlPanel() {
new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0);
new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA);
new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0);
new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA);
}
private class MoveButton extends JButton {
KeyStroke k;
int dx, dy;
public MoveButton(String name, int code,
final int dx, final int dy) {
super(name);
this.k = KeyStroke.getKeyStroke(code, 0);
this.dx = dx;
this.dy = dy;
this.setAction(new AbstractAction(this.getText()) {
#Override
public void actionPerformed(ActionEvent e) {
if(points.size()==1){
LinePanel.this.p1.translate(dx, dy);
LinePanel.this.repaint();
}
if(points.size()==2){
LinePanel.this.p2.translate(dx, dy);
LinePanel.this.repaint();
}
if(points.size()==3){
LinePanel.this.p3.translate(dx, dy);
LinePanel.this.repaint();
}
if(points.size()==4){
LinePanel.this.p4.translate(dx, dy);
LinePanel.this.repaint();
}
}
});
ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW)
.put(k, k.toString());
ControlPanel.this.getActionMap()
.put(k.toString(), new AbstractAction() {
#Override
public void actionPerformed(ActionEvent e) {
MoveButton.this.doClick();
}
});
}
}
}
private void display() throws IOException{
File file = new File(RESOURCE);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE,
0, channel.size());
PDFFile pdffile = new PDFFile(buf);
PDFPage page = pdffile.getPage(1);
Rectangle rect = new Rectangle(0,0,(int)page.getBBox().getWidth(),(int)page.getBBox().getHeight());
Image img = page.getImage(rect.width, rect.height,rect,null,true,true);
JFrame f = new JFrame("LinePanel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setPreferredSize(new Dimension(800, 800));
JPanel panel = new JPanel();
JLabel image = new JLabel(new ImageIcon(img));
panel.add(image);
JScrollPane jspane=new JScrollPane(panel);
f.add(jspane, BorderLayout.CENTER);
f.add(this);
f.add(new ControlPanel(),BorderLayout.SOUTH);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
new LinePanel().display();
} catch (IOException ex) {
Logger.getLogger(LinePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
}
Try this. It's a Panel with an Image. Add the Panel to the Frame;
public class ImagePanel extends JPanel{
private BufferedImage image;
public ImagePanel() {
try {
image = ImageIO.read(new File("image name and path"));
} catch (IOException ex) {
// handle exception...
}
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
// draw other stuff
}
}
If you want your drawing to be painted on top of any component added to the panel/frame, you need to override the paint() method.
Take a look at A Closer Look at the Paint Mechanism

How to make a JPanel that gets more transparent and more transparent and finally disappear?

I want to make a panel which extends JPanel and when it's going to be visible, it starts to get more transparent and more transparent and finally gets disappeared. what is the problem of my code?
public class BaloonPanel extends JPanel
{
private float transparency = 1f;
Timer timer;
public BaloonPanel()
{
setBackground(Color.white);
ActionListener action = new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
transparency = transparency - 0.01f;
if (transparency < 0.0f)
{
timer.stop();
}
repaint();
}
};
timer = new Timer(100, action);
timer.start();
}
#Override
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
super.paint(g2);
g2.dispose();
}
}
Because the BallonPanel is opaque, the repaint manager isn't bothering to paint underneath it. This is an optimization of the paint process, why paint that which doesn't need to be painted.
You need to "persuade" the repaint manger to paint underneath your component, while still painting its background.
Set the BallonPanel to transparent (setOpaque(false)) and update the paint method to fill the background.
public class FadePane {
public static void main(String[] args) {
new FadePane();
}
public FadePane() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.BLUE);
frame.setBackground(Color.BLUE);
frame.add(new BaloonPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class BaloonPanel extends JPanel {
private float transparency = 1f;
Timer timer;
public BaloonPanel() {
setBackground(Color.white);
ActionListener action = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
transparency = transparency - 0.1f;
if (transparency < 0.1f) {
transparency = 0;
timer.stop();
}
invalidate();
repaint();
}
};
timer = new Timer(100, action);
timer.setRepeats(true);
setOpaque(false);
final JButton fade = new JButton("Fade");
fade.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
fade.setEnabled(false);
}
});
setLayout(new GridBagLayout());
add(fade);
}
#Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
System.out.println(transparency);
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, transparency));
g2.setColor(getBackground());
g2.fillRect(0, 0, getWidth(), getHeight());
super.paint(g2);
g2.dispose();
}
}
}

Categories

Resources