A voir également:
- Bugs à l'affichage avec set Bounds [java]
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel football - Télécharger - Jeux vidéo
- Java apk - Télécharger - Langages
- Jeux java itel 5360 ✓ - Forum Jeux vidéo
- Télécharger jeux java gameloft gratuit - Forum Mobile
2 réponses
Et voici le code quand on redimensionne dynamiquement la fenêtre :
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.WindowConstants; public class ScrollBar extends JFrame { public ScrollBar (String titre) { super(titre); this.add(new P2(this)); //this.add(new P2(this.getWidth(),this.getHeight())); //this.setMinimumSize(new Dimension(400, 300)); this.setPreferredSize(new Dimension(400, 300)); pack(); this.setLocationRelativeTo(null); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setVisible(true); } public void reDim () { boolean dd = (getExtendedState()==this.MAXIMIZED_BOTH); int getWidth = this.getWidth(); int getHeight = this.getHeight(); resize(new Dimension(getWidth+1,getHeight+1)); resize(new Dimension(getWidth,getHeight)); if (dd) { this.setExtendedState(JFrame.MAXIMIZED_BOTH); } } public void reDim1 () { } public static void main(String [] args) { ScrollBar f = new ScrollBar("Fenetre 2"); } } class P1 extends JPanel { ScrollBar f; int x; int y; public P1 (ScrollBar f) { super(); this.f = f; setLayout(null); //this.setPreferredSize(new Dimension(f.getWidth(),f.getHeight())); } public void paintComponent (Graphics g) { g.setColor(new Color(-7960954)); g.fillRect(0,0,this.getWidth(),this.getHeight()); } } class P2 extends JPanel { ScrollBar f; P1 p1; int x; int y; boolean pressed; boolean pressed0; ActionListenerSB [] alsb; ActionSouris [] as; public P2 (ScrollBar f) { super(); int i; setLayout(null); this.f = f; //x=0; //y=0; pressed = false; p1 = new P1(f); this.setPreferredSize(new Dimension(f.getWidth(),f.getHeight())); alsb = new ActionListenerSB [2]; as = new ActionSouris[2]; for (i=0; i<2; i++) { alsb [i] = new ActionListenerSB(i); as [i] = new ActionSouris(i); } p1.addMouseListener(alsb[0]); p1.addMouseMotionListener(as[0]); this.addMouseListener(alsb[1]); this.addMouseMotionListener(as[1]); add(p1); } class ActionListenerSB implements MouseListener { int n; public ActionListenerSB (int n) { this.n = n; } public void mousePressed(MouseEvent e) { if (n==1) pressed = true; if (n==0) pressed0 = true; int getWidth = getWidth(); int getHeight = getHeight(); if (pressed) { if(n==1) { x = (int)(e.getPoint().getX()); y = (int)(e.getY()); System.out.println("x = "+x+"\ny = "+y); repaint(); f.reDim(); } } if (pressed0) { if(n==1) { if (x>x-getWidth/15/2) x = x-(x-getWidth/15/2)-(int)(e.getPoint().getX()); else x = x+(int)(e.getPoint().getX()); if (y>y-getHeight/3/2) y = y-(y-getHeight/3/2)-(int)(e.getY()); else y = y+(int)(e.getY()); System.out.println("x = "+x+"\ny = "+y); repaint(); f.reDim(); } } } public void mouseClicked(MouseEvent e) { System.out.print(""); } public void mouseEntered(MouseEvent e) { System.out.print(""); } public void mouseExited(MouseEvent e) { System.out.print(""); } public void mouseReleased(MouseEvent e) { if (n==1) pressed = false; if (n==0) pressed0 = false; } } class ActionSouris implements MouseMotionListener { int n; public ActionSouris (int n) { this.n = n; } public void mouseDragged(MouseEvent e) { int getWidth = getWidth(); int getHeight = getHeight(); if (pressed) { if(n==1) { x = (int)(e.getPoint().getX()); y = (int)(e.getY()); System.out.println("x = "+x+"\ny = "+y); repaint(); f.reDim(); } } if (pressed0) { if(n==1) { if (x>x-getWidth/15/2) x = x-(x-getWidth/15/2)-(int)(e.getPoint().getX()); else x = x+(int)(e.getPoint().getX()); if (y>y-getHeight/3/2) y = y-(y-getHeight/3/2)-(int)(e.getY()); else y = y+(int)(e.getY()); System.out.println("x = "+x+"\ny = "+y); repaint(); f.reDim(); } } System.out.print("blabla"); } public void mouseMoved(MouseEvent e) { System.out.print(""); } } public void paintComponent (Graphics g) { int getWidth = this.getWidth(); int getHeight = this.getHeight(); /*if (x>getWidth) x=getWidth; if (y>getHeight) y=getHeight; if (x<getWidth) x=0; if (y<getHeight) y=0;*/ g.setColor(new Color(-7960954)); p1.setBounds(x-getWidth/15/2,y-getHeight/3/2,getWidth/15,getHeight/3); } }